aboutsummaryrefslogtreecommitdiff
path: root/doc/examples/SoundRecorder/src/soundmixer.c
diff options
context:
space:
mode:
Diffstat (limited to 'doc/examples/SoundRecorder/src/soundmixer.c')
-rw-r--r--doc/examples/SoundRecorder/src/soundmixer.c104
1 files changed, 104 insertions, 0 deletions
diff --git a/doc/examples/SoundRecorder/src/soundmixer.c b/doc/examples/SoundRecorder/src/soundmixer.c
new file mode 100644
index 00000000..4532d06c
--- /dev/null
+++ b/doc/examples/SoundRecorder/src/soundmixer.c
@@ -0,0 +1,104 @@
1/* by Luis Figueiredo (stdio@netc.pt)
2 *
3 * file: soundmixer.c
4 *
5 * description: Sound mixer using (soundbuffers.c)
6 *
7 * date: 17:13,13-13-2002
8 */
9
10
11#include "soundmixer.h"
12
13
14int SDMAX=_SDMAX;
15#define ADJUST_VOLUME(s, v) (s = (s*v)/128)
16
17int soundout_i=-1;
18void sound_process(int soundfd,struct sound_buf *list) {
19 int soundin_i;
20 struct sound_buf *sbuf;
21 unsigned char soundin[_SDMAX+1];
22 unsigned char soundout[_SDMAX+1];
23 int playit=0;
24 int i;
25 short src1,src2;
26 int dst_sample;
27 const int max_audioval =((1<<(16-1))-1);
28 const int min_audioval =-(1<<(16-1));
29 soundin_i=read(soundfd,soundin,_SDMAX);
30 sbuf=list->next;
31 while(sbuf!=NULL) {
32 if(soundin_i>0) {
33 if(sbuf->mode==2) {
34 sbuf->data=realloc(sbuf->data,sbuf->data_i+soundin_i);
35 memcpy(sbuf->data+sbuf->data_i,soundin,soundin_i);
36 sbuf->data_i+=soundin_i;
37 };
38 };
39 if(sbuf->mode==1 && soundout_i==-1) {
40 for(i=0;i<SDMAX && (sbuf->play_i+i)<sbuf->data_i;i+=4) {
41 // LEFT
42 src1=((sbuf->data[sbuf->play_i+i+1]<<8) | sbuf->data[sbuf->play_i+i]);
43 ADJUST_VOLUME(src1,sbuf->volume);
44 src2=((soundout[i+1]<<8) | soundout[i]);
45 dst_sample=src1+src2;
46 if ( dst_sample > max_audioval ) {
47 dst_sample = max_audioval;
48 } else if ( dst_sample < min_audioval ) {
49 dst_sample = min_audioval;
50 }
51 soundout[i]=dst_sample &0xFF;
52 dst_sample>>=8;
53 soundout[i+1]=dst_sample & 0xFF;
54 // RIGHT
55 src1=((sbuf->data[sbuf->play_i+i+1+2]<<8) | sbuf->data[sbuf->play_i+i+2]);
56 ADJUST_VOLUME(src1,sbuf->volume);
57 if(sbuf->channels==2) {
58 src2=((soundout[i+1+2]<<8) | soundout[i+2]); // join left to right
59 } else {
60 src2=((soundout[i+1]<<8) | soundout[i]); // separate
61 };
62 dst_sample=src1+src2;
63 if ( dst_sample > max_audioval ) {
64 dst_sample = max_audioval;
65 } else if ( dst_sample < min_audioval ) {
66 dst_sample = min_audioval;
67 }
68 soundout[i+2]=dst_sample &0xFF;
69 dst_sample>>=8;
70 soundout[i+2+1]=dst_sample & 0xFF;
71
72 };
73 sbuf->play_i+=SDMAX;
74 if(sbuf->play_i>sbuf->data_i){
75 if(sbuf->mode==1) {
76 sbuf->mode=0; // end,stop it
77 }
78 if(sbuf->mode==3) {
79 sbuf->play_i=0;
80 };
81 }
82 playit=1;
83 };
84 sbuf=sbuf->next;// next buffer
85 };
86 if(playit) {
87 soundout_i=0;
88 playit=0;
89 };
90 if(soundout_i<SDMAX && soundout_i!=-1) {
91 i=write(soundfd,soundout+soundout_i,SDMAX-soundout_i);
92 if(i>0) {
93 soundout_i+=i;
94 };
95 } else {
96 memset(soundout,0,SDMAX);
97 soundout_i=-1;
98 };
99
100}
101
102
103
104