1 /* MikMod sound library
2 (c) 2004, Raphael Assenat
3 (c) 1998, 1999, 2000, 2001 Miodrag Vallat and others - see file AUTHORS
6 This library is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of
9 the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 /*==============================================================================
26 ==============================================================================*/
29 FIXME: Stereo .WAV files are not yet supported as samples.
42 #include "mikmod_internals.h"
45 extern int fprintf(FILE *, const char *, ...);
48 static void extract_channel(const char *src, char *dst, int num_chan, int num_samples, int samp_size, int channel);
60 ULONG nAvgBytesPerSec;
62 UWORD nFormatSpecific;
65 static BOOL isWaveFile(MREADER* reader)
69 _mm_fseek(reader, SEEK_SET, 0);
72 _mm_read_string(wh.rID,4,reader);
73 wh.rLen = _mm_read_I_ULONG(reader);
74 _mm_read_string(wh.wID,4,reader);
76 /* check for correct header */
77 if(_mm_eof(reader)|| memcmp(wh.rID,"RIFF",4) || memcmp(wh.wID,"WAVE",4)) {
83 static SAMPLE* Sample_LoadGeneric_internal_wav(MREADER* reader)
89 _mm_fseek(reader, SEEK_SET, 0);
92 _mm_read_string(wh.rID,4,reader);
93 wh.rLen = _mm_read_I_ULONG(reader);
94 _mm_read_string(wh.wID,4,reader);
96 /* check for correct header */
97 if(_mm_eof(reader)|| memcmp(wh.rID,"RIFF",4) || memcmp(wh.wID,"WAVE",4)) {
98 _mm_errno = MMERR_UNKNOWN_WAVE_TYPE;
102 /* scan all RIFF blocks until we find the sample data */
107 _mm_read_string(dID,4,reader);
108 len = _mm_read_I_ULONG(reader);
109 /* truncated file ? */
110 if (_mm_eof(reader)) {
111 _mm_errno=MMERR_UNKNOWN_WAVE_TYPE;
114 start = _mm_ftell(reader);
116 /* sample format block
117 should be present only once and before a data block */
118 if(!memcmp(dID,"fmt ",4)) {
119 wh.wFormatTag = _mm_read_I_UWORD(reader);
120 wh.nChannels = _mm_read_I_UWORD(reader);
121 wh.nSamplesPerSec = _mm_read_I_ULONG(reader);
122 wh.nAvgBytesPerSec = _mm_read_I_ULONG(reader);
123 wh.nBlockAlign = _mm_read_I_UWORD(reader);
124 wh.nFormatSpecific = _mm_read_I_UWORD(reader);
127 fprintf(stderr,"\rwavloader : wFormatTag=%04x blockalign=%04x nFormatSpc=%04x\n",
128 wh.wFormatTag,wh.nBlockAlign,wh.nFormatSpecific);
131 if((have_fmt)||(wh.nChannels>1)) {
132 _mm_errno=MMERR_UNKNOWN_WAVE_TYPE;
138 should be present only once and after a format block */
139 if(!memcmp(dID,"data",4)) {
141 _mm_errno=MMERR_UNKNOWN_WAVE_TYPE;
144 if(!(si=(SAMPLE*)MikMod_malloc(sizeof(SAMPLE)))) return NULL;
145 si->speed = wh.nSamplesPerSec/wh.nChannels;
148 if(wh.nBlockAlign == 2) {
149 si->flags = SF_16BITS | SF_SIGNED;
152 si->inflags = si->flags;
153 SL_RegisterSample(si,MD_SNDFX,reader);
156 /* skip any other remaining blocks - so in case of repeated sample
157 fragments, we'll return the first anyway instead of an error */
160 /* onto next block */
161 _mm_fseek(reader,start+len,SEEK_SET);
169 static SAMPLE* Sample_LoadRawGeneric_internal(MREADER* reader, ULONG rate, ULONG channel, ULONG flags)
175 if(!(si=(SAMPLE*)MikMod_malloc(sizeof(SAMPLE)))) return NULL;
178 _mm_fseek(reader, 0, SEEK_END);
179 len = _mm_ftell(reader);
181 si->panning = PAN_CENTER;
189 si->inflags = si->flags = flags;
190 if (si->flags & SF_16BITS) {
197 if (si->flags & SF_STEREO)
199 char *data, *channel_data;
200 int num_samp = si->length/samp_size/2;
203 data = (char*)MikMod_malloc(si->length);
204 if (!data) { MikMod_free(si); return NULL; }
206 channel_data = (char*)MikMod_malloc(si->length/2);
207 if (!channel_data) { MikMod_free(data); MikMod_free(si); return NULL; }
209 /* load the raw samples completely, and fully extract the
210 * requested channel. Create a memory reader pointing to
211 * the channel data. */
212 _mm_fseek(reader, 0, SEEK_SET);
213 reader->Read(reader, data, si->length);
215 extract_channel(data, channel_data, 2, num_samp, samp_size, channel);
216 chn_reader = _mm_new_mem_reader(channel_data, num_samp * samp_size);
218 MikMod_free(channel_data);
224 /* half of the samples were in the other channel */
227 si->loopend=num_samp;
229 SL_RegisterSample(si, MD_SNDFX, chn_reader);
232 _mm_delete_mem_reader(chn_reader);
233 MikMod_free(channel_data);
239 _mm_fseek(reader, 0, SEEK_SET);
240 SL_RegisterSample(si, MD_SNDFX, reader);
246 static SAMPLE* Sample_LoadGeneric_internal(MREADER* reader, const char *options)
248 if (isWaveFile(reader)) {
249 return Sample_LoadGeneric_internal_wav(reader);
255 MIKMODAPI SAMPLE* Sample_LoadRawGeneric(MREADER* reader, ULONG rate, ULONG channel, ULONG flags)
260 result = Sample_LoadRawGeneric_internal(reader, rate, channel, flags);
266 MIKMODAPI SAMPLE *Sample_LoadRawMem(const char *buf, int len, ULONG rate, ULONG channel, ULONG flags)
271 if (!buf || len <= 0) return NULL;
272 if ((reader=_mm_new_mem_reader(buf, len)) != NULL) {
273 result=Sample_LoadRawGeneric(reader, rate, channel, flags);
274 _mm_delete_mem_reader(reader);
279 MIKMODAPI SAMPLE* Sample_LoadRawFP(FILE *fp, ULONG rate, ULONG channel, ULONG flags)
284 if(fp && (reader=_mm_new_file_reader(fp)) != NULL) {
285 result=Sample_LoadRawGeneric(reader, rate, channel, flags);
286 _mm_delete_file_reader(reader);
291 MIKMODAPI SAMPLE* Sample_LoadRaw(const CHAR* filename, ULONG rate, ULONG channel, ULONG flags)
296 if(!(md_mode & DMODE_SOFT_SNDFX)) return NULL;
297 if((fp=_mm_fopen(filename,"rb")) != NULL) {
298 si = Sample_LoadRawFP(fp, rate, channel, flags);
304 MIKMODAPI SAMPLE* Sample_LoadGeneric(MREADER* reader)
309 result=Sample_LoadGeneric_internal(reader, NULL);
315 MIKMODAPI SAMPLE *Sample_LoadMem(const char *buf, int len)
320 if (!buf || len <= 0) return NULL;
321 if ((reader=_mm_new_mem_reader(buf, len)) != NULL) {
322 result=Sample_LoadGeneric(reader);
323 _mm_delete_mem_reader(reader);
328 MIKMODAPI SAMPLE* Sample_LoadFP(FILE *fp)
333 if(fp && (reader=_mm_new_file_reader(fp)) != NULL) {
334 result=Sample_LoadGeneric(reader);
335 _mm_delete_file_reader(reader);
340 MIKMODAPI SAMPLE* Sample_Load(const CHAR* filename)
345 if(!(md_mode & DMODE_SOFT_SNDFX)) return NULL;
346 if((fp=_mm_fopen(filename,"rb")) != NULL) {
347 si = Sample_LoadFP(fp);
353 MIKMODAPI void Sample_Free(SAMPLE* si)
356 if (si->onfree) si->onfree(si->ctx);
357 MD_SampleUnload(si->handle);
362 void Sample_Free_internal(SAMPLE *si)
369 static void extract_channel(const char *src, char *dst, int num_chan, int num_samples, int samp_size, int channel)
373 fprintf(stderr,"Extract channel: %p %p, num_chan=%d, num_samples=%d, samp_size=%d, channel=%d\n",
374 src,dst,num_chan,num_samples,samp_size,channel);
376 src += channel * samp_size;
378 while (num_samples--)
380 for (i=0; i<samp_size; i++) {
383 src += samp_size * num_chan;