Line 0
Link Here
|
|
|
1 |
--- media/libcubeb/src/cubeb_sndio.c.orig 2018-06-30 15:17:00.132114000 +0200 |
2 |
+++ media/libcubeb/src/cubeb_sndio.c |
3 |
@@ -49,17 +49,33 @@ struct cubeb_stream { |
4 |
cubeb_data_callback data_cb; /* cb to preapare data */ |
5 |
cubeb_state_callback state_cb; /* cb to notify about state changes */ |
6 |
void *arg; /* user arg to {data,state}_cb */ |
7 |
+ float volume; /* current volume */ |
8 |
}; |
9 |
|
10 |
static void |
11 |
-float_to_s16(void *ptr, long nsamp) |
12 |
+s16_setvol(void *ptr, long nsamp, float volume) |
13 |
+{ |
14 |
+ int16_t *dst = ptr; |
15 |
+ int32_t mult = volume * 32768; |
16 |
+ int32_t s; |
17 |
+ |
18 |
+ while (nsamp-- > 0) { |
19 |
+ s = *dst; |
20 |
+ s = (s * mult) >> 15; |
21 |
+ *(dst++) = s; |
22 |
+ } |
23 |
+} |
24 |
+ |
25 |
+static void |
26 |
+float_to_s16(void *ptr, long nsamp, float volume) |
27 |
{ |
28 |
int16_t *dst = ptr; |
29 |
float *src = ptr; |
30 |
+ float mult = volume * 32768; |
31 |
int s; |
32 |
|
33 |
while (nsamp-- > 0) { |
34 |
- s = lrintf(*(src++) * 32768); |
35 |
+ s = lrintf(*(src++) * mult); |
36 |
if (s < -32768) |
37 |
s = -32768; |
38 |
else if (s > 32767) |
39 |
@@ -167,8 +183,12 @@ sndio_mainloop(void *arg) |
40 |
if (prime > 0) |
41 |
prime--; |
42 |
|
43 |
- if ((s->mode & SIO_PLAY) && s->conv) |
44 |
- float_to_s16(s->pbuf, nfr * s->pchan); |
45 |
+ if (s->mode & SIO_PLAY) { |
46 |
+ if (s->conv) |
47 |
+ float_to_s16(s->pbuf, nfr * s->pchan, s->volume); |
48 |
+ else |
49 |
+ s16_setvol(s->pbuf, nfr * s->pchan, s->volume); |
50 |
+ } |
51 |
|
52 |
if (s->mode & SIO_REC) |
53 |
rstart = 0; |
54 |
@@ -362,6 +382,7 @@ sndio_stream_init(cubeb * context, |
55 |
if (s->rbuf == NULL) |
56 |
goto err; |
57 |
} |
58 |
+ s->volume = 1.; |
59 |
*stream = s; |
60 |
DPR("sndio_stream_init() end, ok\n"); |
61 |
(void)context; |
62 |
@@ -466,7 +487,11 @@ sndio_stream_set_volume(cubeb_stream *s, float volume) |
63 |
{ |
64 |
DPR("sndio_stream_set_volume(%f)\n", volume); |
65 |
pthread_mutex_lock(&s->mtx); |
66 |
- sio_setvol(s->hdl, SIO_MAXVOL * volume); |
67 |
+ if (volume < 0.) |
68 |
+ volume = 0.; |
69 |
+ else if (volume > 1.0) |
70 |
+ volume = 1.; |
71 |
+ s->volume = volume; |
72 |
pthread_mutex_unlock(&s->mtx); |
73 |
return CUBEB_OK; |
74 |
} |