]> code.delx.au - pulseaudio/blob - polyp/sample-util.c
51b22fbe1f69d3cc619843e3114034cfda524813
[pulseaudio] / polyp / sample-util.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <assert.h>
29
30 #include "sample-util.h"
31
32 struct pa_memblock *pa_silence_memblock(struct pa_memblock* b, const struct pa_sample_spec *spec) {
33 assert(b && b->data && spec);
34 pa_silence_memory(b->data, b->length, spec);
35 return b;
36 }
37
38 void pa_silence_memchunk(struct pa_memchunk *c, const struct pa_sample_spec *spec) {
39 assert(c && c->memblock && c->memblock->data && spec && c->length);
40 pa_silence_memory((uint8_t*) c->memblock->data+c->index, c->length, spec);
41 }
42
43 void pa_silence_memory(void *p, size_t length, const struct pa_sample_spec *spec) {
44 char c = 0;
45 assert(p && length && spec);
46
47 switch (spec->format) {
48 case PA_SAMPLE_U8:
49 c = 127;
50 break;
51 case PA_SAMPLE_S16LE:
52 case PA_SAMPLE_S16BE:
53 case PA_SAMPLE_FLOAT32:
54 c = 0;
55 break;
56 case PA_SAMPLE_ALAW:
57 case PA_SAMPLE_ULAW:
58 c = 80;
59 break;
60 default:
61 assert(0);
62 }
63
64 memset(p, c, length);
65 }
66
67 size_t pa_mix(struct pa_mix_info channels[], unsigned nchannels, void *data, size_t length, const struct pa_sample_spec *spec, pa_volume_t volume) {
68 unsigned c, d;
69 assert(channels && data && length && spec);
70 assert(spec->format == PA_SAMPLE_S16NE);
71
72 for (d = 0;; d += sizeof(int16_t)) {
73 int32_t sum = 0;
74
75 if (d >= length)
76 return d;
77
78 for (c = 0; c < nchannels; c++) {
79 int32_t v;
80 uint32_t volume = channels[c].volume;
81
82 if (d >= channels[c].chunk.length)
83 return d;
84
85 if (volume == PA_VOLUME_MUTED)
86 v = 0;
87 else {
88 v = *((int16_t*) ((uint8_t*) channels[c].chunk.memblock->data + channels[c].chunk.index + d));
89
90 if (volume != PA_VOLUME_NORM)
91 v = (int32_t) ((float)v*volume/PA_VOLUME_NORM);
92 }
93
94 sum += v;
95 }
96
97 if (volume == PA_VOLUME_MUTED)
98 sum = 0;
99 else if (volume != PA_VOLUME_NORM)
100 sum = (int32_t) ((float) sum*volume/PA_VOLUME_NORM);
101
102 if (sum < -0x8000) sum = -0x8000;
103 if (sum > 0x7FFF) sum = 0x7FFF;
104
105 *((int16_t*) data) = sum;
106 data = (uint8_t*) data + sizeof(int16_t);
107 }
108 }
109
110
111 void pa_volume_memchunk(struct pa_memchunk*c, const struct pa_sample_spec *spec, pa_volume_t volume) {
112 int16_t *d;
113 size_t n;
114 assert(c && spec && (c->length % pa_frame_size(spec) == 0));
115 assert(spec->format == PA_SAMPLE_S16NE);
116
117 if (volume == PA_VOLUME_NORM)
118 return;
119
120 if (volume == PA_VOLUME_MUTED) {
121 pa_silence_memchunk(c, spec);
122 return;
123 }
124
125 for (d = (int16_t*) ((uint8_t*) c->memblock->data+c->index), n = c->length/sizeof(int16_t); n > 0; d++, n--) {
126 int32_t t = (int32_t)(*d);
127
128 t *= volume;
129 t /= PA_VOLUME_NORM;
130
131 if (t < -0x8000) t = -0x8000;
132 if (t > 0x7FFF) t = 0x7FFF;
133
134 *d = (int16_t) t;
135 }
136 }
137