remove const from TPL_OpenTPLFromMemory since the memory is altered
[libogc.git] / libmodplay / mixer.c
blobda65d3bdd9ba448e3cbf9dfe4aa8abe9a8e05836
1 /*
2 Copyright (c) 2002,2003, Christian Nowak <chnowak@web.de>
3 All rights reserved.
5 Modified by Francisco Mu�oz 'Hermes' MAY 2008
7 Redistribution and use in source and binary forms, with or without modification, are
8 permitted provided that the following conditions are met:
10 - Redistributions of source code must retain the above copyright notice, this list of
11 conditions and the following disclaimer.
12 - Redistributions in binary form must reproduce the above copyright notice, this list
13 of conditions and the following disclaimer in the documentation and/or other
14 materials provided with the distribution.
15 - The names of the contributors may not be used to endorse or promote products derived
16 from this software without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
19 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /* mixer.c */
31 #include "defines.h"
32 #include "modplay.h"
34 #if defined(DREAMCAST)
35 #define PREFILL_WORD 0
36 #elif defined(MINGW)
37 #define PREFILL_WORD 32768
38 #elif defined(ALLEGRO)
39 #define PREFILL_WORD 32768
40 #elif defined(GP32)
41 #define PREFILL_WORD 32768
42 #elif defined(GEKKO)
43 #define PREFILL_WORD 0
44 #else
45 #define PREFILL_WORD 32768
46 #endif
49 #define MIX_SAMPLES \
50 accum = (s32)b[i] + (s32)((((s32)data[playpos.aword.high]*volume)>>6) << shiftval); \
51 if(accum<-32768) accum = -32768; if(accum>32767) accum = 32767; \
52 b[i] = accum; \
53 playpos.adword+=incval; \
54 if ( playpos.adword>=loop_end ) \
55 { \
56 if (mod->instrument[mod->instnum[voice]].looped) \
57 playpos.adword -= mod->instrument[mod->instnum[voice]].loop_length<<16; \
58 else \
59 { \
60 playpos.adword = (mod->instrument[mod->instnum[voice]].loop_end-1)<<16; \
61 mod->channel_active[voice] = FALSE; \
62 break; \
63 } \
66 s32 mix_mono_16bit ( MOD * mod, s16 * buf, s32 numSamples )
68 s32 accum;
69 s32 voice, i, j;
70 s16 * b = buf;
71 s32 shiftval = mod->shiftval;
72 s32 numIterations;
73 s32 numIterationsRest;
75 numIterations = numSamples>>4;
76 numIterationsRest = numSamples&((1<<4)-1);
79 for (i=j=0;j<numIterations;j++)
81 buf[i] = PREFILL_WORD;i++;
82 buf[i] = PREFILL_WORD;i++;
83 buf[i] = PREFILL_WORD;i++;
84 buf[i] = PREFILL_WORD;i++;
85 buf[i] = PREFILL_WORD;i++;
86 buf[i] = PREFILL_WORD;i++;
87 buf[i] = PREFILL_WORD;i++;
88 buf[i] = PREFILL_WORD;i++;
89 buf[i] = PREFILL_WORD;i++;
90 buf[i] = PREFILL_WORD;i++;
91 buf[i] = PREFILL_WORD;i++;
92 buf[i] = PREFILL_WORD;i++;
93 buf[i] = PREFILL_WORD;i++;
94 buf[i] = PREFILL_WORD;i++;
95 buf[i] = PREFILL_WORD;i++;
96 buf[i] = PREFILL_WORD;i++;
98 for (j=0;j<numIterationsRest;j++,i++)
99 buf[i] = PREFILL_WORD;
102 for (voice=0;voice<mod->num_channels;++voice)
104 if (mod->instrument[mod->instnum[voice]].data != NULL && mod->channel_active[voice])
106 u32 incval,noteidx;
108 noteidx = (mod->chanfreq[voice] - mod->chanfreq[voice]*2*(mod->instrument[mod->instnum[voice]].finetune-8)/256);
109 incval = mod->inctab[noteidx];
111 s8 * data = mod->instrument[mod->instnum[voice]].data;
112 union_dword playpos;
113 u32 loop_end = mod->instrument[mod->instnum[voice]].loop_end<<16;
114 s32 volume = mod->volume[voice];
115 playpos.adword = mod->playpos[voice];
117 if ( voice<mod->num_voices )
118 volume = (volume*(s32)mod->musicvolume)>>6;
119 else
120 volume = (volume*(s32)mod->sfxvolume)>>6;
122 if (mod->freq==32000 || mod->freq==48000)
123 incval >>= 2;
125 for (j=i=0;j<numIterations;j++)
127 MIX_SAMPLES;i++;
128 MIX_SAMPLES;i++;
129 MIX_SAMPLES;i++;
130 MIX_SAMPLES;i++;
131 MIX_SAMPLES;i++;
132 MIX_SAMPLES;i++;
133 MIX_SAMPLES;i++;
134 MIX_SAMPLES;i++;
135 MIX_SAMPLES;i++;
136 MIX_SAMPLES;i++;
137 MIX_SAMPLES;i++;
138 MIX_SAMPLES;i++;
139 MIX_SAMPLES;i++;
140 MIX_SAMPLES;i++;
141 MIX_SAMPLES;i++;
142 MIX_SAMPLES;i++;
144 for (j=0;j<numIterationsRest;j++,i++)
146 MIX_SAMPLES;
148 mod->playpos[voice] = playpos.adword;
151 return numSamples;
154 s32 mix_stereo_16bit ( MOD * mod, s16 * buf, s32 numSamples )
156 s32 accum;
157 s32 voice, i, j;
158 s16 * b = buf;
159 s32 shiftval = mod->shiftval+1;
160 s32 numIterations, numIterationsRest;
162 numIterations = (numSamples)>>2;
163 numIterationsRest = (numSamples)&((1<<2)-1);
165 for (i=j=0;j<numIterations;j++)
167 buf[i] = buf[i+1] = PREFILL_WORD;i+=2;
168 buf[i] = buf[i+1] = PREFILL_WORD;i+=2;
169 buf[i] = buf[i+1] = PREFILL_WORD;i+=2;
170 buf[i] = buf[i+1] = PREFILL_WORD;i+=2;
172 for (j=0;j<numIterationsRest;j++,i+=2)
173 buf[i] = buf[i+1] = PREFILL_WORD;
175 /* for (j=0;j<numSamples*2;j+=2)
176 buf[j] = buf[j+1] = PREFILL_WORD;*/
178 for (voice=0;voice<mod->num_channels;++voice)
180 s32 lrofs = (((voice-1)>>1)&1)^1;
181 if (mod->instrument[mod->instnum[voice]].data != NULL && mod->channel_active[voice])
183 u32 incval,noteidx;
185 noteidx = (mod->chanfreq[voice] - mod->chanfreq[voice]*2*(mod->instrument[mod->instnum[voice]].finetune-8)/256);
186 incval = mod->inctab[noteidx];
188 s8 * data = mod->instrument[mod->instnum[voice]].data;
189 union_dword playpos;
190 u32 loop_end = mod->instrument[mod->instnum[voice]].loop_end<<16;
191 s32 volume = mod->volume[voice];
193 if ( voice<mod->num_voices )
194 volume = (volume*(s32)mod->musicvolume)>>6;
195 else
196 volume = (volume*(s32)mod->sfxvolume)>>6;
198 playpos.adword = mod->playpos[voice];
199 if (mod->freq==32000 || mod->freq==48000)
200 incval >>= 2;
202 i = lrofs;
203 for (j=0;j<numIterations;j++)
205 MIX_SAMPLES;i+=2;
206 MIX_SAMPLES;i+=2;
207 MIX_SAMPLES;i+=2;
208 MIX_SAMPLES;i+=2;
210 for (j=0;j<numIterationsRest;j++)
212 MIX_SAMPLES;i+=2;
214 /* for (i=0;i<numSamples*2;i+=2)
216 MIX_SAMPLES;
218 mod->playpos[voice] = playpos.adword;
222 return numSamples;