summaryrefslogtreecommitdiffstats
path: root/noatun/library/noatunarts/StereoVolumeControl_impl.cpp
blob: c0019ede14171e2d975477565154ad0ad4a50ed2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <artsflow.h>
#include <stdsynthmodule.h>
#include <flowsystem.h>
#include "noatunarts.h"

using namespace Arts;

namespace Noatun
{

class StereoVolumeControl_impl : virtual public StereoVolumeControl_skel,
                                 virtual public StdSynthModule
{
	float mPercent;
	float level;
public:
	StereoVolumeControl_impl() : mPercent(1.0), level(0.0)
	{ }

	/*attribute float scaleFactor;*/
	void percent(float p) { mPercent=p; }
	float percent() { return mPercent; }

	void calculateBlock(unsigned long samples)
	{
		float *left=inleft;
		float *right=inright;
		float *oleft=outleft;
		float *oright=outright;

		level = *right + *left;
		
		float p=mPercent;

		float *end=left+samples;

		while (left<end)
		{
			*oleft=*left * p;
			*oright=*right * p;

			++left;
			++right;
			++oleft;
			++oright;
		}
	}
	
	AutoSuspendState autoSuspend()
	{
		return (level < 0.001) ? asSuspend : asNoSuspend;
	}
};
	
class StereoVolumeControlSSE_impl : virtual public Noatun::StereoVolumeControlSSE_skel,
                                    virtual public StdSynthModule
{
	float mPercent;
	float level;
	
public:
	StereoVolumeControlSSE_impl() : mPercent(1.0), level(0.0)
	{ }

	/*attribute float scaleFactor;*/
	void percent(float p) { mPercent=p; }
	float percent() { return mPercent; }

	void calculateBlock(unsigned long samples) __attribute__((noinline))
	{
#ifdef HAVE_X86_SSE
		float *left=inleft;
		float *right=inright;
		float *oleft=outleft;
		float *oright=outright;
		
		level = *right + *left;

		// need to copy the data members to locals to get enough
		// spare registers (malte)
		
		long p = (long)(mPercent*100.0);
		__asm__ __volatile__(
			"pushl $100                       \n"
			"fildl (%%esp)                    \n"
#if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 2))
			"addl $4, %%esp                   \n"
#endif
			"fildl %5                         \n"
			"fdivp                            \n" // percent / 100.0
#if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 2))
			"pushl $100                       \n"
#endif
			"fstps (%%esp)                    \n"
			"movss (%%esp), %%xmm1            \n"
			"shufps $0x00, %%xmm1, %%xmm1     \n" // percentage in all of xmm1
			"addl $4, %%esp                   \n"
#if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 2))
			"subl $4, %4                      \n"
			"jl .l2                           \n" // samples < 4
#else
			"pushl %4                         \n" // save sample count
			"shrl $2, %4                      \n"
			"jz .l2                           \n" // samples < 4
#endif
			"xorl %%ecx, %%ecx                \n"

			".l1:                             \n"
			// left
			"movups (%0, %%ecx, 8), %%xmm0    \n"
			"mulps %%xmm1, %%xmm0             \n"
			"movl %2, %%eax                   \n"
			"movups %%xmm0, (%%eax, %%ecx, 8) \n"
			// right
			"movups (%1, %%ecx, 8), %%xmm0    \n"
			"mulps %%xmm1, %%xmm0             \n"
			"movl %3, %%eax                   \n"
			"movups %%xmm0, (%%eax, %%ecx, 8) \n"
		
			"incl %%ecx                       \n"
			"incl %%ecx                       \n"
#if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 2))
			"subl $4, %4                      \n"
			"jge .l1                          \n"
			".l2:                             \n"
			"addl $4, %4                      \n"
#else
			"decl %4                          \n"
			"jnz .l1                          \n"
			".l2:                             \n"
			"popl %4                          \n" // restore sample count
			"andl $3, %4                      \n"
#endif
			"jz .l4                           \n"

			// calculate remaining samples for samples % 4 != 0
			"shll $1, %%ecx                   \n"
			".l3:                             \n"
			"movss (%0, %%ecx, 4), %%xmm0     \n" // load left
			"movss (%1, %%ecx, 4), %%xmm2     \n" // load right
			"shufps $0x00, %%xmm2, %%xmm0     \n" // both channels in xmm0
			"mulps %%xmm1, %%xmm0             \n"
			"movl %2, %%eax                   \n"
			"movss %%xmm0, (%%eax, %%ecx, 4)  \n" // store left
			"shufps $0x02, %%xmm0, %%xmm0     \n"
			"movl %3, %%eax                   \n"
			"movss %%xmm0, (%%eax, %%ecx, 4)  \n" // store right
			"incl %%ecx                       \n"	
			"decl %4                          \n"
			"jnz .l3                          \n"
			
			".l4:                             \n"
			"emms                             \n"
			:
			: "r" (left),     // %0
			  "r" (right),    // %1
			  "m" (oleft),    // %2
			  "m" (oright),   // %3
			  "r" (samples),  // %4
			  "m" (p)         // %5
			: "eax", "ecx"
		);
#endif
	}

	AutoSuspendState autoSuspend()
	{
		return (level < 0.001) ? asSuspend : asNoSuspend;
	}
};

REGISTER_IMPLEMENTATION(StereoVolumeControlSSE_impl);
REGISTER_IMPLEMENTATION(StereoVolumeControl_impl);

}