(Metux) autogen.sh: not running ./configure anymore (breaks certain distro builders)
[mirror-ossqm-audiofile.git] / libaudiofile / wave.c
blobb6581dccebda1d2e7c357003500c449f08c7105c
1 /*
2 Audio File Library
3 Copyright (C) 1998-2000, Michael Pruett <michael@68k.org>
4 Copyright (C) 2000-2001, Silicon Graphics, Inc.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library 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 GNU
14 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
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307 USA.
23 wave.c
25 This file contains code for parsing RIFF WAVE format sound files.
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
32 #include <assert.h>
33 #include <math.h>
34 #include <stdint.h>
35 #include <stdlib.h>
36 #include <string.h>
38 #include "audiofile.h"
39 #include "util.h"
40 #include "afinternal.h"
41 #include "byteorder.h"
42 #include "wave.h"
43 #include "track.h"
44 #include "setup.h"
45 #include "marker.h"
47 const int _af_wave_compression_types[_AF_WAVE_NUM_COMPTYPES] =
49 AF_COMPRESSION_G711_ULAW,
50 AF_COMPRESSION_G711_ALAW
53 const _InstParamInfo _af_wave_inst_params[_AF_WAVE_NUM_INSTPARAMS] =
55 { AF_INST_MIDI_BASENOTE, AU_PVTYPE_LONG, "MIDI base note", {60} },
56 { AF_INST_NUMCENTS_DETUNE, AU_PVTYPE_LONG, "Detune in cents", {0} },
57 { AF_INST_MIDI_LOVELOCITY, AU_PVTYPE_LONG, "Low velocity", {1} },
58 { AF_INST_MIDI_HIVELOCITY, AU_PVTYPE_LONG, "High velocity", {127} },
59 { AF_INST_MIDI_LONOTE, AU_PVTYPE_LONG, "Low note", {0} },
60 { AF_INST_MIDI_HINOTE, AU_PVTYPE_LONG, "High note", {127} },
61 { AF_INST_NUMDBS_GAIN, AU_PVTYPE_LONG, "Gain in dB", {0} }
64 _AFfilesetup _af_wave_default_filesetup =
66 _AF_VALID_FILESETUP, /* valid */
67 AF_FILE_WAVE, /* fileFormat */
68 true, /* trackSet */
69 true, /* instrumentSet */
70 true, /* miscellaneousSet */
71 1, /* trackCount */
72 NULL, /* tracks */
73 0, /* instrumentCount */
74 NULL, /* instruments */
75 0, /* miscellaneousCount */
76 NULL /* miscellaneous */
79 static status ParseFrameCount (AFfilehandle filehandle, AFvirtualfile *fp,
80 uint32_t id, size_t size)
82 uint32_t totalFrames;
83 _Track *track;
85 track = _af_filehandle_get_track(filehandle, AF_DEFAULT_TRACK);
87 af_read_uint32_le(&totalFrames, fp);
89 track->totalfframes = totalFrames;
91 return AF_SUCCEED;
94 static status ParseFormat (AFfilehandle filehandle, AFvirtualfile *fp,
95 uint32_t id, size_t size)
97 _Track *track;
98 uint16_t formatTag, channelCount;
99 uint32_t sampleRate, averageBytesPerSecond;
100 uint16_t blockAlign;
101 _WAVEInfo *wave;
103 assert(filehandle != NULL);
104 assert(fp != NULL);
105 assert(!memcmp(&id, "fmt ", 4));
107 track = _af_filehandle_get_track(filehandle, AF_DEFAULT_TRACK);
109 assert(filehandle->formatSpecific != NULL);
110 wave = (_WAVEInfo *) filehandle->formatSpecific;
112 af_read_uint16_le(&formatTag, fp);
113 af_read_uint16_le(&channelCount, fp);
114 af_read_uint32_le(&sampleRate, fp);
115 af_read_uint32_le(&averageBytesPerSecond, fp);
116 af_read_uint16_le(&blockAlign, fp);
118 track->f.channelCount = channelCount;
119 track->f.sampleRate = sampleRate;
120 track->f.byteOrder = AF_BYTEORDER_LITTLEENDIAN;
122 /* Default to uncompressed audio data. */
123 track->f.compressionType = AF_COMPRESSION_NONE;
125 switch (formatTag)
127 case WAVE_FORMAT_PCM:
129 uint16_t bitsPerSample;
131 af_read_uint16_le(&bitsPerSample, fp);
133 track->f.sampleWidth = bitsPerSample;
135 if (bitsPerSample == 0 || bitsPerSample > 32)
137 _af_error(AF_BAD_WIDTH,
138 "bad sample width of %d bits",
139 bitsPerSample);
140 return AF_FAIL;
143 if (bitsPerSample <= 8)
144 track->f.sampleFormat = AF_SAMPFMT_UNSIGNED;
145 else
146 track->f.sampleFormat = AF_SAMPFMT_TWOSCOMP;
148 break;
150 case WAVE_FORMAT_MULAW:
151 case IBM_FORMAT_MULAW:
152 track->f.sampleWidth = 16;
153 track->f.sampleFormat = AF_SAMPFMT_TWOSCOMP;
154 track->f.compressionType = AF_COMPRESSION_G711_ULAW;
155 break;
157 case WAVE_FORMAT_ALAW:
158 case IBM_FORMAT_ALAW:
159 track->f.sampleWidth = 16;
160 track->f.sampleFormat = AF_SAMPFMT_TWOSCOMP;
161 track->f.compressionType = AF_COMPRESSION_G711_ALAW;
162 break;
164 case WAVE_FORMAT_IEEE_FLOAT:
166 uint16_t bitsPerSample;
168 af_read_uint16_le(&bitsPerSample, fp);
170 if (bitsPerSample == 64)
172 track->f.sampleWidth = 64;
173 track->f.sampleFormat = AF_SAMPFMT_DOUBLE;
175 else
177 track->f.sampleWidth = 32;
178 track->f.sampleFormat = AF_SAMPFMT_FLOAT;
181 break;
183 case WAVE_FORMAT_ADPCM:
185 uint16_t bitsPerSample, extraByteCount,
186 samplesPerBlock, numCoefficients;
187 int i;
188 AUpvlist pv;
189 long l;
190 void *v;
192 if (track->f.channelCount != 1 &&
193 track->f.channelCount != 2)
195 _af_error(AF_BAD_CHANNELS,
196 "WAVE file with MS ADPCM compression "
197 "must have 1 or 2 channels");
200 af_read_uint16_le(&bitsPerSample, fp);
201 af_read_uint16_le(&extraByteCount, fp);
202 af_read_uint16_le(&samplesPerBlock, fp);
203 af_read_uint16_le(&numCoefficients, fp);
205 /* numCoefficients should be at least 7. */
206 assert(numCoefficients >= 7 && numCoefficients <= 255);
208 for (i=0; i<numCoefficients; i++)
210 int16_t a0, a1;
212 af_read_uint16_le(&a0, fp);
213 af_read_uint16_le(&a1, fp);
215 wave->msadpcmCoefficients[i][0] = a0;
216 wave->msadpcmCoefficients[i][1] = a1;
219 track->f.sampleWidth = 16;
220 track->f.sampleFormat = AF_SAMPFMT_TWOSCOMP;
221 track->f.compressionType = AF_COMPRESSION_MS_ADPCM;
222 track->f.byteOrder = _AF_BYTEORDER_NATIVE;
224 /* Create the parameter list. */
225 pv = AUpvnew(4);
226 AUpvsetparam(pv, 0, _AF_MS_ADPCM_NUM_COEFFICIENTS);
227 AUpvsetvaltype(pv, 0, AU_PVTYPE_LONG);
228 l = numCoefficients;
229 AUpvsetval(pv, 0, &l);
231 AUpvsetparam(pv, 1, _AF_MS_ADPCM_COEFFICIENTS);
232 AUpvsetvaltype(pv, 1, AU_PVTYPE_PTR);
233 v = wave->msadpcmCoefficients;
234 AUpvsetval(pv, 1, &v);
236 AUpvsetparam(pv, 2, _AF_FRAMES_PER_BLOCK);
237 AUpvsetvaltype(pv, 2, AU_PVTYPE_LONG);
238 l = samplesPerBlock;
239 AUpvsetval(pv, 2, &l);
241 AUpvsetparam(pv, 3, _AF_BLOCK_SIZE);
242 AUpvsetvaltype(pv, 3, AU_PVTYPE_LONG);
243 l = blockAlign;
244 AUpvsetval(pv, 3, &l);
246 track->f.compressionParams = pv;
248 break;
250 case WAVE_FORMAT_DVI_ADPCM:
252 AUpvlist pv;
253 long l;
255 uint16_t bitsPerSample, extraByteCount,
256 samplesPerBlock;
258 af_read_uint16_le(&bitsPerSample, fp);
259 af_read_uint16_le(&extraByteCount, fp);
260 af_read_uint16_le(&samplesPerBlock, fp);
262 if (bitsPerSample != 4)
264 _af_error(AF_BAD_NOT_IMPLEMENTED,
265 "IMA ADPCM compression supports only 4 bits per sample");
268 int bytesPerBlock = (samplesPerBlock + 14) / 8 * 4 * channelCount;
269 if (bytesPerBlock > blockAlign || (samplesPerBlock % 8) != 1)
271 _af_error(AF_BAD_CODEC_CONFIG,
272 "Invalid samples per block for IMA ADPCM compression");
275 track->f.sampleWidth = 16;
276 track->f.sampleFormat = AF_SAMPFMT_TWOSCOMP;
277 track->f.compressionType = AF_COMPRESSION_IMA;
278 track->f.byteOrder = _AF_BYTEORDER_NATIVE;
280 /* Create the parameter list. */
281 pv = AUpvnew(2);
282 AUpvsetparam(pv, 0, _AF_FRAMES_PER_BLOCK);
283 AUpvsetvaltype(pv, 0, AU_PVTYPE_LONG);
284 l = samplesPerBlock;
285 AUpvsetval(pv, 0, &l);
287 AUpvsetparam(pv, 1, _AF_BLOCK_SIZE);
288 AUpvsetvaltype(pv, 1, AU_PVTYPE_LONG);
289 l = blockAlign;
290 AUpvsetval(pv, 1, &l);
292 track->f.compressionParams = pv;
294 break;
296 case WAVE_FORMAT_YAMAHA_ADPCM:
297 case WAVE_FORMAT_OKI_ADPCM:
298 case WAVE_FORMAT_CREATIVE_ADPCM:
299 case IBM_FORMAT_ADPCM:
300 _af_error(AF_BAD_NOT_IMPLEMENTED, "WAVE ADPCM data format 0x%x is not currently supported", formatTag);
301 return AF_FAIL;
302 break;
304 case WAVE_FORMAT_MPEG:
305 _af_error(AF_BAD_NOT_IMPLEMENTED, "WAVE MPEG data format is not supported");
306 return AF_FAIL;
307 break;
309 case WAVE_FORMAT_MPEGLAYER3:
310 _af_error(AF_BAD_NOT_IMPLEMENTED, "WAVE MPEG layer 3 data format is not supported");
311 return AF_FAIL;
312 break;
314 default:
315 _af_error(AF_BAD_NOT_IMPLEMENTED, "WAVE file data format 0x%x not currently supported", formatTag);
316 return AF_FAIL;
317 break;
320 _af_set_sample_format(&track->f, track->f.sampleFormat, track->f.sampleWidth);
322 return AF_SUCCEED;
325 static status ParseData (AFfilehandle filehandle, AFvirtualfile *fp,
326 uint32_t id, size_t size)
328 _Track *track;
330 assert(filehandle != NULL);
331 assert(fp != NULL);
332 assert(!memcmp(&id, "data", 4));
334 track = _af_filehandle_get_track(filehandle, AF_DEFAULT_TRACK);
336 track->fpos_first_frame = af_ftell(fp);
337 track->data_size = size;
339 return AF_SUCCEED;
342 static status ParsePlayList (AFfilehandle filehandle, AFvirtualfile *fp,
343 uint32_t id, size_t size)
345 _Instrument *instrument;
346 uint32_t segmentCount;
347 int segment;
349 af_read_uint32_le(&segmentCount, fp);
351 if (segmentCount == 0)
353 filehandle->instrumentCount = 0;
354 filehandle->instruments = NULL;
355 return AF_SUCCEED;
358 for (segment=0; segment<segmentCount; segment++)
360 uint32_t startMarkID, loopLength, loopCount;
362 af_read_uint32_le(&startMarkID, fp);
363 af_read_uint32_le(&loopLength, fp);
364 af_read_uint32_le(&loopCount, fp);
367 return AF_SUCCEED;
370 static status ParseCues (AFfilehandle filehandle, AFvirtualfile *fp,
371 uint32_t id, size_t size)
373 _Track *track;
374 uint32_t markerCount;
375 int i;
377 track = _af_filehandle_get_track(filehandle, AF_DEFAULT_TRACK);
379 af_read_uint32_le(&markerCount, fp);
380 track->markerCount = markerCount;
382 if (markerCount == 0)
384 track->markers = NULL;
385 return AF_SUCCEED;
388 if ((track->markers = _af_marker_new(markerCount)) == NULL)
389 return AF_FAIL;
391 for (i=0; i<markerCount; i++)
393 uint32_t id, position, chunkid;
394 uint32_t chunkByteOffset, blockByteOffset;
395 uint32_t sampleFrameOffset;
396 _Marker *marker = &track->markers[i];
398 af_read_uint32_le(&id, fp);
399 af_read_uint32_le(&position, fp);
400 af_read_uint32_le(&chunkid, fp);
401 af_read_uint32_le(&chunkByteOffset, fp);
402 af_read_uint32_le(&blockByteOffset, fp);
405 sampleFrameOffset represents the position of
406 the mark in units of frames.
408 af_read_uint32_le(&sampleFrameOffset, fp);
410 marker->id = id;
411 marker->position = sampleFrameOffset;
412 marker->name = _af_strdup("");
413 marker->comment = _af_strdup("");
416 return AF_SUCCEED;
419 /* Parse an adtl sub-chunk within a LIST chunk. */
420 static status ParseADTLSubChunk (AFfilehandle filehandle, AFvirtualfile *fp,
421 uint32_t id, size_t size)
423 _Track *track;
424 AFfileoffset endPos=af_ftell(fp)+size;
426 track = _af_filehandle_get_track(filehandle, AF_DEFAULT_TRACK);
428 while (af_ftell(fp) < endPos)
430 char chunkID[4];
431 uint32_t chunkSize;
433 af_fread(chunkID, 4, 1, fp);
434 af_read_uint32_le(&chunkSize, fp);
436 if (memcmp(chunkID, "labl", 4)==0 || memcmp(chunkID, "note", 4)==0)
438 _Marker *marker=NULL;
439 uint32_t id;
440 long length=chunkSize-4;
441 char *p=_af_malloc(length);
443 af_read_uint32_le(&id, fp);
444 af_fread(p, length, 1, fp);
446 marker = _af_marker_find_by_id(track, id);
448 if (marker != NULL)
450 if (memcmp(chunkID, "labl", 4)==0)
452 free(marker->name);
453 marker->name = p;
455 else if (memcmp(chunkID, "note", 4)==0)
457 free(marker->comment);
458 marker->comment = p;
460 else
461 free(p);
463 else
464 free(p);
467 If chunkSize is odd, skip an extra byte
468 at the end of the chunk.
470 if ((chunkSize % 2) != 0)
471 af_fseek(fp, 1, SEEK_CUR);
473 else
475 /* If chunkSize is odd, skip an extra byte. */
476 af_fseek(fp, chunkSize + (chunkSize % 2), SEEK_CUR);
479 return AF_SUCCEED;
482 /* Parse an INFO sub-chunk within a LIST chunk. */
483 static status ParseINFOSubChunk (AFfilehandle filehandle, AFvirtualfile *fp,
484 uint32_t id, size_t size)
486 AFfileoffset endPos=af_ftell(fp)+size;
488 while (af_ftell(fp) < endPos)
490 int misctype = AF_MISC_UNRECOGNIZED;
491 uint32_t miscid, miscsize;
493 af_fread(&miscid, 4, 1, fp);
494 af_read_uint32_le(&miscsize, fp);
496 if (memcmp(&miscid, "IART", 4) == 0)
497 misctype = AF_MISC_AUTH;
498 else if (memcmp(&miscid, "INAM", 4) == 0)
499 misctype = AF_MISC_NAME;
500 else if (memcmp(&miscid, "ICOP", 4) == 0)
501 misctype = AF_MISC_COPY;
502 else if (memcmp(&miscid, "ICMT", 4) == 0)
503 misctype = AF_MISC_ICMT;
504 else if (memcmp(&miscid, "ICRD", 4) == 0)
505 misctype = AF_MISC_ICRD;
506 else if (memcmp(&miscid, "ISFT", 4) == 0)
507 misctype = AF_MISC_ISFT;
509 if (misctype != AF_MISC_UNRECOGNIZED)
511 char *string = _af_malloc(miscsize);
513 af_fread(string, miscsize, 1, fp);
515 filehandle->miscellaneousCount++;
516 filehandle->miscellaneous = _af_realloc(filehandle->miscellaneous, sizeof (_Miscellaneous) * filehandle->miscellaneousCount);
518 filehandle->miscellaneous[filehandle->miscellaneousCount-1].id = filehandle->miscellaneousCount;
519 filehandle->miscellaneous[filehandle->miscellaneousCount-1].type = misctype;
520 filehandle->miscellaneous[filehandle->miscellaneousCount-1].size = miscsize;
521 filehandle->miscellaneous[filehandle->miscellaneousCount-1].position = 0;
522 filehandle->miscellaneous[filehandle->miscellaneousCount-1].buffer = string;
524 else
526 af_fseek(fp, miscsize, SEEK_CUR);
529 /* Make the current position an even number of bytes. */
530 if (miscsize % 2 != 0)
531 af_fseek(fp, 1, SEEK_CUR);
533 return AF_SUCCEED;
536 static status ParseList (AFfilehandle filehandle, AFvirtualfile *fp,
537 uint32_t id, size_t size)
539 uint32_t typeID;
541 af_fread(&typeID, 4, 1, fp);
542 size-=4;
544 if (memcmp(&typeID, "adtl", 4) == 0)
546 /* Handle adtl sub-chunks. */
547 return ParseADTLSubChunk(filehandle, fp, typeID, size);
549 else if (memcmp(&typeID, "INFO", 4) == 0)
551 /* Handle INFO sub-chunks. */
552 return ParseINFOSubChunk(filehandle, fp, typeID, size);
554 else
556 /* Skip unhandled sub-chunks. */
557 af_fseek(fp, size, SEEK_CUR);
558 return AF_SUCCEED;
560 return AF_SUCCEED;
563 static status ParseInstrument (AFfilehandle filehandle, AFvirtualfile *fp,
564 uint32_t id, size_t size)
566 uint8_t baseNote;
567 int8_t detune, gain;
568 uint8_t lowNote, highNote, lowVelocity, highVelocity;
569 uint8_t padByte;
571 af_read_uint8(&baseNote, fp);
572 af_read_uint8(&detune, fp);
573 af_read_uint8(&gain, fp);
574 af_read_uint8(&lowNote, fp);
575 af_read_uint8(&highNote, fp);
576 af_read_uint8(&lowVelocity, fp);
577 af_read_uint8(&highVelocity, fp);
578 af_read_uint8(&padByte, fp);
580 return AF_SUCCEED;
583 bool _af_wave_recognize (AFvirtualfile *fh)
585 uint8_t buffer[8];
587 af_fseek(fh, 0, SEEK_SET);
589 if (af_fread(buffer, 1, 8, fh) != 8 || memcmp(buffer, "RIFF", 4) != 0)
590 return false;
591 if (af_fread(buffer, 1, 4, fh) != 4 || memcmp(buffer, "WAVE", 4) != 0)
592 return false;
594 return true;
597 status _af_wave_read_init (AFfilesetup setup, AFfilehandle filehandle)
599 _Track *track;
600 uint32_t type, size, formtype;
601 uint32_t index = 0;
602 bool hasFormat, hasData, hasCue, hasList, hasPlayList, hasFrameCount,
603 hasINST, hasINFO;
604 _WAVEInfo *wave = _af_malloc(sizeof (_WAVEInfo));
606 assert(filehandle != NULL);
607 assert(filehandle->fh != NULL);
609 hasFormat = false;
610 hasData = false;
611 hasCue = false;
612 hasList = false;
613 hasPlayList = false;
614 hasFrameCount = false;
615 hasINST = false;
616 hasINFO = false;
618 filehandle->formatSpecific = wave;
619 filehandle->instruments = NULL;
620 filehandle->instrumentCount = 0;
621 filehandle->miscellaneous = NULL;
622 filehandle->miscellaneousCount = 0;
624 track = _af_track_new();
625 filehandle->tracks = track;
626 filehandle->trackCount = 1;
628 af_fseek(filehandle->fh, 0, SEEK_SET);
630 af_fread(&type, 4, 1, filehandle->fh);
631 af_read_uint32_le(&size, filehandle->fh);
632 af_fread(&formtype, 4, 1, filehandle->fh);
634 assert(!memcmp(&type, "RIFF", 4));
635 assert(!memcmp(&formtype, "WAVE", 4));
637 #ifdef DEBUG
638 printf("size: %d\n", size);
639 #endif
641 /* Include the offset of the form type. */
642 index += 4;
644 while (index < size)
646 uint32_t chunkid = 0, chunksize = 0;
647 status result;
649 #ifdef DEBUG
650 printf("index: %d\n", index);
651 #endif
652 af_fread(&chunkid, 4, 1, filehandle->fh);
653 af_read_uint32_le(&chunksize, filehandle->fh);
655 #ifdef DEBUG
656 _af_printid(BENDIAN_TO_HOST_INT32(chunkid));
657 printf(" size: %d\n", chunksize);
658 #endif
660 if (memcmp(&chunkid, "fmt ", 4) == 0)
662 result = ParseFormat(filehandle, filehandle->fh, chunkid, chunksize);
663 if (result == AF_FAIL)
664 return AF_FAIL;
666 hasFormat = true;
668 else if (memcmp(&chunkid, "data", 4) == 0)
670 /* The format chunk must precede the data chunk. */
671 if (!hasFormat)
673 _af_error(AF_BAD_HEADER, "missing format chunk in WAVE file");
674 return AF_FAIL;
677 result = ParseData(filehandle, filehandle->fh, chunkid, chunksize);
678 if (result == AF_FAIL)
679 return AF_FAIL;
681 hasData = true;
683 else if (memcmp(&chunkid, "inst", 4) == 0)
685 result = ParseInstrument(filehandle, filehandle->fh, chunkid, chunksize);
686 if (result == AF_FAIL)
687 return AF_FAIL;
689 else if (memcmp(&chunkid, "fact", 4) == 0)
691 hasFrameCount = true;
692 result = ParseFrameCount(filehandle, filehandle->fh, chunkid, chunksize);
693 if (result == AF_FAIL)
694 return AF_FAIL;
696 else if (memcmp(&chunkid, "cue ", 4) == 0)
698 hasCue = true;
699 result = ParseCues(filehandle, filehandle->fh, chunkid, chunksize);
700 if (result == AF_FAIL)
701 return AF_FAIL;
703 else if (memcmp(&chunkid, "LIST", 4) == 0 || memcmp(&chunkid, "list", 4) == 0)
705 hasList = true;
706 result = ParseList(filehandle, filehandle->fh, chunkid, chunksize);
707 if (result == AF_FAIL)
708 return AF_FAIL;
710 else if (memcmp(&chunkid, "INST", 4) == 0)
712 hasINST = true;
713 result = ParseInstrument(filehandle, filehandle->fh, chunkid, chunksize);
714 if (result == AF_FAIL)
715 return AF_FAIL;
717 else if (memcmp(&chunkid, "plst", 4) == 0)
719 hasPlayList = true;
720 result = ParsePlayList(filehandle, filehandle->fh, chunkid, chunksize);
721 if (result == AF_FAIL)
722 return AF_FAIL;
725 index += chunksize + 8;
727 /* All chunks must be aligned on an even number of bytes */
728 if ((index % 2) != 0)
729 index++;
731 af_fseek(filehandle->fh, index + 8, SEEK_SET);
734 /* The format chunk and the data chunk are required. */
735 if (!hasFormat || !hasData)
737 return AF_FAIL;
741 At this point we know that the file has a format chunk
742 and a data chunk, so we can assume that track->f and
743 track->data_size have been initialized.
745 if (!hasFrameCount)
747 track->totalfframes = track->data_size /
748 (int) _af_format_frame_size(&track->f, false);
751 if (track->f.compressionType != AF_COMPRESSION_NONE &&
752 (track->f.compressionType == AF_COMPRESSION_G711_ULAW ||
753 track->f.compressionType == AF_COMPRESSION_G711_ALAW))
755 track->totalfframes = track->data_size / track->f.channelCount;
759 A return value of AF_SUCCEED indicates successful parsing.
761 return AF_SUCCEED;
764 AFfilesetup _af_wave_complete_setup (AFfilesetup setup)
766 AFfilesetup newsetup;
767 _TrackSetup *track;
769 if (setup->trackSet && setup->trackCount != 1)
771 _af_error(AF_BAD_NUMTRACKS, "WAVE file must have 1 track");
772 return AF_NULL_FILESETUP;
775 track = _af_filesetup_get_tracksetup(setup, AF_DEFAULT_TRACK);
777 if (track->sampleFormatSet)
779 switch (track->f.sampleFormat)
781 case AF_SAMPFMT_FLOAT:
782 if (track->sampleWidthSet &&
783 track->f.sampleWidth != 32)
785 _af_error(AF_BAD_WIDTH,
786 "Warning: invalid sample width for floating-point WAVE file: %d (must be 32 bits)\n",
787 track->f.sampleWidth);
788 _af_set_sample_format(&track->f, AF_SAMPFMT_FLOAT, 32);
790 break;
792 case AF_SAMPFMT_DOUBLE:
793 if (track->sampleWidthSet &&
794 track->f.sampleWidth != 64)
796 _af_error(AF_BAD_WIDTH,
797 "Warning: invalid sample width for double-precision floating-point WAVE file: %d (must be 64 bits)\n",
798 track->f.sampleWidth);
799 _af_set_sample_format(&track->f, AF_SAMPFMT_DOUBLE, 64);
801 break;
803 case AF_SAMPFMT_UNSIGNED:
804 if (track->sampleWidthSet)
806 if (track->f.sampleWidth < 1 || track->f.sampleWidth > 32)
808 _af_error(AF_BAD_WIDTH, "invalid sample width for WAVE file: %d (must be 1-32 bits)\n", track->f.sampleWidth);
809 return AF_NULL_FILESETUP;
811 if (track->f.sampleWidth > 8)
813 _af_error(AF_BAD_SAMPFMT, "WAVE integer data of more than 8 bits must be two's complement signed");
814 _af_set_sample_format(&track->f, AF_SAMPFMT_TWOSCOMP, track->f.sampleWidth);
817 else
819 If the sample width is not set but the user requests
820 unsigned data, set the width to 8 bits.
822 _af_set_sample_format(&track->f, track->f.sampleFormat, 8);
823 break;
825 case AF_SAMPFMT_TWOSCOMP:
826 if (track->sampleWidthSet)
828 if (track->f.sampleWidth < 1 || track->f.sampleWidth > 32)
830 _af_error(AF_BAD_WIDTH, "invalid sample width %d for WAVE file (must be 1-32)", track->f.sampleWidth);
831 return AF_NULL_FILESETUP;
833 else if (track->f.sampleWidth <= 8)
835 _af_error(AF_BAD_SAMPFMT, "Warning: WAVE format integer data of 1-8 bits must be unsigned; setting sample format to unsigned");
836 _af_set_sample_format(&track->f, AF_SAMPFMT_UNSIGNED, track->f.sampleWidth);
839 else
841 If no sample width was specified, we default to 16 bits
842 for signed integer data.
844 _af_set_sample_format(&track->f, track->f.sampleFormat, 16);
845 break;
849 Otherwise set the sample format depending on the sample
850 width or set completely to default.
852 else
854 if (!track->sampleWidthSet)
856 track->f.sampleWidth = 16;
857 track->f.sampleFormat = AF_SAMPFMT_TWOSCOMP;
859 else
861 if (track->f.sampleWidth < 1 || track->f.sampleWidth > 32)
863 _af_error(AF_BAD_WIDTH, "invalid sample width %d for WAVE file (must be 1-32)", track->f.sampleWidth);
864 return AF_NULL_FILESETUP;
866 else if (track->f.sampleWidth > 8)
867 /* Here track->f.sampleWidth is in {1..32}. */
868 track->f.sampleFormat = AF_SAMPFMT_TWOSCOMP;
869 else
870 /* Here track->f.sampleWidth is in {1..8}. */
871 track->f.sampleFormat = AF_SAMPFMT_UNSIGNED;
875 if (track->f.compressionType != AF_COMPRESSION_NONE &&
876 track->f.compressionType != AF_COMPRESSION_G711_ULAW &&
877 track->f.compressionType != AF_COMPRESSION_G711_ALAW)
879 _af_error(AF_BAD_NOT_IMPLEMENTED, "compression format not supported in WAVE format");
880 return AF_NULL_FILESETUP;
883 if (track->byteOrderSet &&
884 track->f.byteOrder != AF_BYTEORDER_LITTLEENDIAN &&
885 track->f.compressionType == AF_COMPRESSION_NONE)
887 _af_error(AF_BAD_BYTEORDER, "WAVE format only supports little-endian data");
888 return AF_NULL_FILESETUP;
891 if (track->f.compressionType == AF_COMPRESSION_NONE)
892 track->f.byteOrder = AF_BYTEORDER_LITTLEENDIAN;
893 else
894 track->f.byteOrder = AF_BYTEORDER_BIGENDIAN;
896 if (track->aesDataSet)
898 _af_error(AF_BAD_FILESETUP, "WAVE files cannot have AES data");
899 return AF_NULL_FILESETUP;
902 if (setup->instrumentSet)
904 if (setup->instrumentCount > 1)
906 _af_error(AF_BAD_NUMINSTS, "WAVE files can have 0 or 1 instrument");
907 return AF_NULL_FILESETUP;
909 else if (setup->instrumentCount == 1)
911 if (setup->instruments[0].loopSet &&
912 setup->instruments[0].loopCount > 0 &&
913 (!track->markersSet || track->markerCount == 0))
915 _af_error(AF_BAD_NUMMARKS, "WAVE files with loops must contain at least 1 marker");
916 return AF_NULL_FILESETUP;
921 /* Make sure the miscellaneous data is of an acceptable type. */
922 if (setup->miscellaneousSet)
924 int i;
925 for (i=0; i<setup->miscellaneousCount; i++)
927 switch (setup->miscellaneous[i].type)
929 case AF_MISC_COPY:
930 case AF_MISC_AUTH:
931 case AF_MISC_NAME:
932 case AF_MISC_ICRD:
933 case AF_MISC_ISFT:
934 case AF_MISC_ICMT:
935 break;
936 default:
937 _af_error(AF_BAD_MISCTYPE, "illegal miscellaneous type [%d] for WAVE file", setup->miscellaneous[i].type);
938 return AF_NULL_FILESETUP;
944 Allocate an AFfilesetup and make all the unset fields correct.
946 newsetup = _af_filesetup_copy(setup, &_af_wave_default_filesetup, false);
948 /* Make sure we do not copy loops if they are not specified in setup. */
949 if (setup->instrumentSet && setup->instrumentCount > 0 &&
950 setup->instruments[0].loopSet)
952 free(newsetup->instruments[0].loops);
953 newsetup->instruments[0].loopCount = 0;
956 return newsetup;
959 bool _af_wave_instparam_valid (AFfilehandle filehandle, AUpvlist list, int i)
961 int param, type, lval;
963 AUpvgetparam(list, i, &param);
964 AUpvgetvaltype(list, i, &type);
965 if (type != AU_PVTYPE_LONG)
966 return false;
968 AUpvgetval(list, i, &lval);
970 switch (param)
972 case AF_INST_MIDI_BASENOTE:
973 return ((lval >= 0) && (lval <= 127));
975 case AF_INST_NUMCENTS_DETUNE:
976 return ((lval >= -50) && (lval <= 50));
978 case AF_INST_MIDI_LOVELOCITY:
979 return ((lval >= 1) && (lval <= 127));
981 case AF_INST_MIDI_HIVELOCITY:
982 return ((lval >= 1) && (lval <= 127));
984 case AF_INST_MIDI_LONOTE:
985 return ((lval >= 0) && (lval <= 127));
987 case AF_INST_MIDI_HINOTE:
988 return ((lval >= 0) && (lval <= 127));
990 case AF_INST_NUMDBS_GAIN:
991 return true;
993 default:
994 return false;
997 return true;