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.
25 This file contains code for parsing RIFF WAVE format sound files.
38 #include "audiofile.h"
40 #include "afinternal.h"
41 #include "byteorder.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 */
69 true, /* instrumentSet */
70 true, /* miscellaneousSet */
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
)
85 track
= _af_filehandle_get_track(filehandle
, AF_DEFAULT_TRACK
);
87 af_read_uint32_le(&totalFrames
, fp
);
89 track
->totalfframes
= totalFrames
;
94 static status
ParseFormat (AFfilehandle filehandle
, AFvirtualfile
*fp
,
95 uint32_t id
, size_t size
)
98 uint16_t formatTag
, channelCount
;
99 uint32_t sampleRate
, averageBytesPerSecond
;
103 assert(filehandle
!= 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
;
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",
143 if (bitsPerSample
<= 8)
144 track
->f
.sampleFormat
= AF_SAMPFMT_UNSIGNED
;
146 track
->f
.sampleFormat
= AF_SAMPFMT_TWOSCOMP
;
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
;
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
;
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
;
177 track
->f
.sampleWidth
= 32;
178 track
->f
.sampleFormat
= AF_SAMPFMT_FLOAT
;
183 case WAVE_FORMAT_ADPCM
:
185 uint16_t bitsPerSample
, extraByteCount
,
186 samplesPerBlock
, numCoefficients
;
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
++)
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. */
226 AUpvsetparam(pv
, 0, _AF_MS_ADPCM_NUM_COEFFICIENTS
);
227 AUpvsetvaltype(pv
, 0, AU_PVTYPE_LONG
);
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
);
239 AUpvsetval(pv
, 2, &l
);
241 AUpvsetparam(pv
, 3, _AF_BLOCK_SIZE
);
242 AUpvsetvaltype(pv
, 3, AU_PVTYPE_LONG
);
244 AUpvsetval(pv
, 3, &l
);
246 track
->f
.compressionParams
= pv
;
250 case WAVE_FORMAT_DVI_ADPCM
:
255 uint16_t bitsPerSample
, extraByteCount
,
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. */
282 AUpvsetparam(pv
, 0, _AF_FRAMES_PER_BLOCK
);
283 AUpvsetvaltype(pv
, 0, AU_PVTYPE_LONG
);
285 AUpvsetval(pv
, 0, &l
);
287 AUpvsetparam(pv
, 1, _AF_BLOCK_SIZE
);
288 AUpvsetvaltype(pv
, 1, AU_PVTYPE_LONG
);
290 AUpvsetval(pv
, 1, &l
);
292 track
->f
.compressionParams
= pv
;
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
);
304 case WAVE_FORMAT_MPEG
:
305 _af_error(AF_BAD_NOT_IMPLEMENTED
, "WAVE MPEG data format is not supported");
309 case WAVE_FORMAT_MPEGLAYER3
:
310 _af_error(AF_BAD_NOT_IMPLEMENTED
, "WAVE MPEG layer 3 data format is not supported");
315 _af_error(AF_BAD_NOT_IMPLEMENTED
, "WAVE file data format 0x%x not currently supported", formatTag
);
320 _af_set_sample_format(&track
->f
, track
->f
.sampleFormat
, track
->f
.sampleWidth
);
325 static status
ParseData (AFfilehandle filehandle
, AFvirtualfile
*fp
,
326 uint32_t id
, size_t size
)
330 assert(filehandle
!= 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
;
342 static status
ParsePlayList (AFfilehandle filehandle
, AFvirtualfile
*fp
,
343 uint32_t id
, size_t size
)
345 _Instrument
*instrument
;
346 uint32_t segmentCount
;
349 af_read_uint32_le(&segmentCount
, fp
);
351 if (segmentCount
== 0)
353 filehandle
->instrumentCount
= 0;
354 filehandle
->instruments
= NULL
;
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
);
370 static status
ParseCues (AFfilehandle filehandle
, AFvirtualfile
*fp
,
371 uint32_t id
, size_t size
)
374 uint32_t markerCount
;
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
;
388 if ((track
->markers
= _af_marker_new(markerCount
)) == NULL
)
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
);
411 marker
->position
= sampleFrameOffset
;
412 marker
->name
= _af_strdup("");
413 marker
->comment
= _af_strdup("");
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
)
424 AFfileoffset endPos
=af_ftell(fp
)+size
;
426 track
= _af_filehandle_get_track(filehandle
, AF_DEFAULT_TRACK
);
428 while (af_ftell(fp
) < endPos
)
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
;
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
);
450 if (memcmp(chunkID
, "labl", 4)==0)
455 else if (memcmp(chunkID
, "note", 4)==0)
457 free(marker
->comment
);
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
);
475 /* If chunkSize is odd, skip an extra byte. */
476 af_fseek(fp
, chunkSize
+ (chunkSize
% 2), SEEK_CUR
);
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
;
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
);
536 static status
ParseList (AFfilehandle filehandle
, AFvirtualfile
*fp
,
537 uint32_t id
, size_t size
)
541 af_fread(&typeID
, 4, 1, fp
);
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
);
556 /* Skip unhandled sub-chunks. */
557 af_fseek(fp
, size
, SEEK_CUR
);
563 static status
ParseInstrument (AFfilehandle filehandle
, AFvirtualfile
*fp
,
564 uint32_t id
, size_t size
)
568 uint8_t lowNote
, highNote
, lowVelocity
, highVelocity
;
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
);
583 bool _af_wave_recognize (AFvirtualfile
*fh
)
587 af_fseek(fh
, 0, SEEK_SET
);
589 if (af_fread(buffer
, 1, 8, fh
) != 8 || memcmp(buffer
, "RIFF", 4) != 0)
591 if (af_fread(buffer
, 1, 4, fh
) != 4 || memcmp(buffer
, "WAVE", 4) != 0)
597 status
_af_wave_read_init (AFfilesetup setup
, AFfilehandle filehandle
)
600 uint32_t type
, size
, formtype
;
602 bool hasFormat
, hasData
, hasCue
, hasList
, hasPlayList
, hasFrameCount
,
604 _WAVEInfo
*wave
= _af_malloc(sizeof (_WAVEInfo
));
606 assert(filehandle
!= NULL
);
607 assert(filehandle
->fh
!= NULL
);
614 hasFrameCount
= 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));
638 printf("size: %d\n", size
);
641 /* Include the offset of the form type. */
646 uint32_t chunkid
= 0, chunksize
= 0;
650 printf("index: %d\n", index
);
652 af_fread(&chunkid
, 4, 1, filehandle
->fh
);
653 af_read_uint32_le(&chunksize
, filehandle
->fh
);
656 _af_printid(BENDIAN_TO_HOST_INT32(chunkid
));
657 printf(" size: %d\n", chunksize
);
660 if (memcmp(&chunkid
, "fmt ", 4) == 0)
662 result
= ParseFormat(filehandle
, filehandle
->fh
, chunkid
, chunksize
);
663 if (result
== AF_FAIL
)
668 else if (memcmp(&chunkid
, "data", 4) == 0)
670 /* The format chunk must precede the data chunk. */
673 _af_error(AF_BAD_HEADER
, "missing format chunk in WAVE file");
677 result
= ParseData(filehandle
, filehandle
->fh
, chunkid
, chunksize
);
678 if (result
== AF_FAIL
)
683 else if (memcmp(&chunkid
, "inst", 4) == 0)
685 result
= ParseInstrument(filehandle
, filehandle
->fh
, chunkid
, chunksize
);
686 if (result
== 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
)
696 else if (memcmp(&chunkid
, "cue ", 4) == 0)
699 result
= ParseCues(filehandle
, filehandle
->fh
, chunkid
, chunksize
);
700 if (result
== AF_FAIL
)
703 else if (memcmp(&chunkid
, "LIST", 4) == 0 || memcmp(&chunkid
, "list", 4) == 0)
706 result
= ParseList(filehandle
, filehandle
->fh
, chunkid
, chunksize
);
707 if (result
== AF_FAIL
)
710 else if (memcmp(&chunkid
, "INST", 4) == 0)
713 result
= ParseInstrument(filehandle
, filehandle
->fh
, chunkid
, chunksize
);
714 if (result
== AF_FAIL
)
717 else if (memcmp(&chunkid
, "plst", 4) == 0)
720 result
= ParsePlayList(filehandle
, filehandle
->fh
, chunkid
, chunksize
);
721 if (result
== AF_FAIL
)
725 index
+= chunksize
+ 8;
727 /* All chunks must be aligned on an even number of bytes */
728 if ((index
% 2) != 0)
731 af_fseek(filehandle
->fh
, index
+ 8, SEEK_SET
);
734 /* The format chunk and the data chunk are required. */
735 if (!hasFormat
|| !hasData
)
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.
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.
764 AFfilesetup
_af_wave_complete_setup (AFfilesetup setup
)
766 AFfilesetup newsetup
;
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);
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);
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
);
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);
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
);
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);
849 Otherwise set the sample format depending on the sample
850 width or set completely to default.
854 if (!track
->sampleWidthSet
)
856 track
->f
.sampleWidth
= 16;
857 track
->f
.sampleFormat
= AF_SAMPFMT_TWOSCOMP
;
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
;
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
;
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
)
925 for (i
=0; i
<setup
->miscellaneousCount
; i
++)
927 switch (setup
->miscellaneous
[i
].type
)
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;
959 bool _af_wave_instparam_valid (AFfilehandle filehandle
, AUpvlist list
, int i
)
961 int param
, type
, lval
;
963 AUpvgetparam(list
, i
, ¶m
);
964 AUpvgetvaltype(list
, i
, &type
);
965 if (type
!= AU_PVTYPE_LONG
)
968 AUpvgetval(list
, i
, &lval
);
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
: