FreeWRL/FreeX3D  3.0.0
mixer.c
1 
2 /****************************************************************************
3  This file is part of the FreeWRL/FreeX3D Distribution.
4 
5  Copyright 2009 CRC Canada. (http://www.crc.gc.ca)
6 
7  FreeWRL/FreeX3D is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Lesser Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  FreeWRL/FreeX3D is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with FreeWRL/FreeX3D. If not, see <http://www.gnu.org/licenses/>.
19 ****************************************************************************/
20 
21 
22 # include <config.h>
23 
24 #include "system.h"
25 #include "soundheader.h"
26 
27 
28 /* record "global" parameters */
29 long int MaxSPS = 0;
30 int MaxAvgBytes = 0;
31 int myBPS;
32 
33 void streamThisChannel(int source, int bytesToStream, int offset) {
34  SNDFILE *wavfile;
35  int readSizeThrowAway;
36 
37  /* //printf ("streamThisChannel %d, bytes %d offset %d\n",source, */
38  /* // bytesToStream, offset); */
39 
40  wavfile = sndfile[source];
41 
42  /* // ok - we should write. Get the next bit of data */
43 
44  /* // Calculate if we are going to go past the EOF marker, */
45  /* // and if so, go back to the beginning. (assume loop=true) */
46  /* // */
47  /* //printf ("wavfile->bytes_remaining %d\n",wavfile->bytes_remaining); */
48  /* // has this file been successfully initialized yet? */
49  if (wavfile->bytes_remaining == UNINITWAV) {
50  /* //printf ("file not opened yet\n"); */
51  rewind_to_beginning(wavfile);
52  }
53 
54  if (wavfile->bytes_remaining <= 0) {
55  /* //printf ("EOF input, lets reset and re-read\n"); */
56  if (loop[source] == 1) {
57  rewind_to_beginning(wavfile);
58  } else {
59  /* // dont loop - just return */
60  return;
61  }
62  }
63 
64  /* //printf ("bytes remaining %ld\n",wavfile->bytes_remaining); */
65 
66  /* // Are we reaching the end of the file? Lets calculate the */
67  /* // size of the WAV file read we are going to do, and adjust */
68  /* // where we are in the read of the WAV file. Note that for */
69  /* // really short files, this works, too! */
70  /* // */
71  /* // */
72 
73  /* // */
74  /* //printf ("step2, bytes remaining %ld\n",wavfile->bytes_remaining); */
75  if (wavfile->bytes_remaining < bytesToStream) {
76  readSize = (int) (wavfile->bytes_remaining);
77  wavfile->bytes_remaining = 0;
78  } else {
79  readSize = bytesToStream;
80  wavfile->bytes_remaining = wavfile->bytes_remaining -
81  (long int) bytesToStream;
82  }
83  /* //printf ("after decrement, %ld\n", wavfile->bytes_remaining); */
84 
85  /* // read and write here. */
86  if ((readSize) > 0) {
87  /* //printf ("reading/writing %d bytes\n",readSize); */
88  readSizeThrowAway = fread(wavfile->data,readSize,1,wavfile->fd);
89  addToCombiningBuffer(source,readSize,offset);
90  }
91 
92  /* // do we need to read some more? */
93  if (readSize < bytesToStream) {
94  streamThisChannel(source, bytesToStream-readSize,
95  offset+readSize);
96  }
97 }
98 
99 
100 
101 void streamMoreData(int bytesToStream) {
102  int count;
103  int writeSizeThrowAway;
104 
105  if (bytesToStream > MAXBUFSIZE) {bytesToStream = MAXBUFSIZE;
106  /* //printf ("reducing bytesToStream\n"); */
107  }
108 
109  for (count = 0; count <= current_max; count++) {
110  if ((active[count] == 1 ) && (registered[count] == 1)) {
111  /* //printf ("channel %d is active\n",count); */
112  if ((sndfile[count]->ampl) > 0) {
113  streamThisChannel(count,bytesToStream,0);
114  }
115 
116  /* // Now, did we loose this source? lets decrement the */
117  /* // amplitude, until we get either to zero, or another */
118  /* // AMPL command. */
119 
120  if (sndfile[count]!=NULL) {
121  sndfile[count]->ampl = (sndfile[count]->ampl) - 5;
122  if ((sndfile[count]->ampl) <= 0) {
123  sndfile[count]->ampl = 0;
124  }
125  }
126  }
127  }
128  /* // Now, stream out the combined data */
129  writeSizeThrowAway = write (dspFile, CombiningBuffer, bytesToStream);
130 }