30 static const char*
const aiffFormatName =
"AIFF file";
43 namespace AiffFileHelpers
48 #pragma pack (push, 1)
57 uint16 startIdentifier;
73 values.
set (
"MidiUnityNote",
String (baseNote));
77 values.
set (
"HighNote",
String (highNote));
78 values.
set (
"LowVelocity",
String (lowVelocity));
79 values.
set (
"HighVelocity",
String (highVelocity));
83 values.
set (
"NumSampleLoops",
String (2));
92 static uint16 getValue16 (
const StringPairArray& values,
const char* name,
const char* def)
97 static int8 getValue8 (
const StringPairArray& values,
const char* name,
const char* def)
102 static void create (MemoryBlock& block,
const StringPairArray& values)
104 if (values.getAllKeys().contains (
"MidiUnityNote",
true))
106 block.setSize ((
sizeof (InstChunk) + 3) & ~(
size_t) 3,
true);
107 auto& inst = *
static_cast<InstChunk*
> (block.getData());
109 inst.baseNote = getValue8 (values,
"MidiUnityNote",
"60");
110 inst.detune = getValue8 (values,
"Detune",
"0");
111 inst.lowNote = getValue8 (values,
"LowNote",
"0");
112 inst.highNote = getValue8 (values,
"HighNote",
"127");
113 inst.lowVelocity = getValue8 (values,
"LowVelocity",
"1");
114 inst.highVelocity = getValue8 (values,
"HighVelocity",
"127");
115 inst.gain = (int16) getValue16 (values,
"Gain",
"0");
117 inst.sustainLoop.type = getValue16 (values,
"Loop0Type",
"0");
118 inst.sustainLoop.startIdentifier = getValue16 (values,
"Loop0StartIdentifier",
"0");
119 inst.sustainLoop.endIdentifier = getValue16 (values,
"Loop0EndIdentifier",
"0");
120 inst.releaseLoop.type = getValue16 (values,
"Loop1Type",
"0");
121 inst.releaseLoop.startIdentifier = getValue16 (values,
"Loop1StartIdentifier",
"0");
122 inst.releaseLoop.endIdentifier = getValue16 (values,
"Loop1EndIdentifier",
"0");
150 input.
read (unknown,
sizeof (unknown));
155 const bool rootNoteSet = rootNote != 0;
167 const char* keyString =
nullptr;
171 case minor: keyString =
"minor";
break;
172 case major: keyString =
"major";
break;
173 case neither: keyString =
"neither";
break;
174 case both: keyString =
"both";
break;
177 if (keyString !=
nullptr)
181 void setBoolFlag (
StringPairArray& values,
const char* name,
bool shouldBeSet)
const
183 values.
set (name, shouldBeSet ?
"1" :
"0");
203 static bool isValidTag (
const char* d) noexcept
210 static bool isAppleGenre (
const String& tag) noexcept
212 static const char* appleGenres[] =
226 for (
int i = 0; i < numElementsInArray (appleGenres); ++i)
227 if (tag == appleGenres[i])
233 static String read (InputStream& input,
const uint32 length)
236 input.skipNextBytes (4);
237 input.readIntoMemoryBlock (mb, (ssize_t) length - 4);
239 StringArray tagsArray;
241 auto* data =
static_cast<const char*
> (mb.getData());
242 auto* dataEnd = data + mb.getSize();
244 while (data < dataEnd)
246 bool isGenre =
false;
248 if (isValidTag (data))
250 auto tag = String (CharPointer_UTF8 (data), CharPointer_UTF8 (dataEnd));
251 isGenre = isAppleGenre (tag);
255 data += isGenre ? 118 : 50;
257 if (data < dataEnd && data[0] == 0)
259 if (data + 52 < dataEnd && isValidTag (data + 50)) data += 50;
260 else if (data + 120 < dataEnd && isValidTag (data + 118)) data += 118;
261 else if (data + 170 < dataEnd && isValidTag (data + 168)) data += 168;
265 return tagsArray.joinIntoString (
";");
272 static bool metaDataContainsZeroIdentifiers (
const StringPairArray& values)
275 const String cueString (
"Cue");
276 const String noteString (
"CueNote");
277 const String identifierString (
"Identifier");
279 for (
auto& key : values.getAllKeys())
281 if (key.startsWith (noteString))
284 if (key.startsWith (cueString) && key.contains (identifierString))
285 if (values.getValue (key,
"-1").getIntValue() == 0)
292 static void create (MemoryBlock& block,
const StringPairArray& values)
294 auto numCues = values.getValue (
"NumCuePoints",
"0").getIntValue();
298 MemoryOutputStream out (block,
false);
299 out.writeShortBigEndian ((
short) numCues);
301 auto numCueLabels = values.getValue (
"NumCueLabels",
"0").getIntValue();
302 auto idOffset = metaDataContainsZeroIdentifiers (values) ? 1 : 0;
305 Array<int> identifiers;
308 for (
int i = 0; i < numCues; ++i)
310 auto prefixCue =
"Cue" + String (i);
311 auto identifier = idOffset + values.getValue (prefixCue +
"Identifier",
"1").getIntValue();
314 jassert (! identifiers.contains (identifier));
315 identifiers.add (identifier);
318 auto offset = values.getValue (prefixCue +
"Offset",
"0").getIntValue();
319 auto label =
"CueLabel" + String (i);
321 for (
int labelIndex = 0; labelIndex < numCueLabels; ++labelIndex)
323 auto prefixLabel =
"CueLabel" + String (labelIndex);
324 auto labelIdentifier = idOffset + values.getValue (prefixLabel +
"Identifier",
"1").getIntValue();
326 if (labelIdentifier == identifier)
328 label = values.getValue (prefixLabel +
"Text", label);
333 out.writeShortBigEndian ((
short) identifier);
334 out.writeIntBigEndian (offset);
336 auto labelLength = jmin ((
size_t) 254, label.getNumBytesAsUTF8());
337 out.writeByte ((
char) labelLength + 1);
338 out.write (label.toUTF8(), labelLength);
341 if ((out.getDataSize() & 1) != 0)
351 static void create (MemoryBlock& block,
const StringPairArray& values)
353 auto numNotes = values.getValue (
"NumCueNotes",
"0").getIntValue();
357 MemoryOutputStream out (block,
false);
358 out.writeShortBigEndian ((
short) numNotes);
360 for (
int i = 0; i < numNotes; ++i)
362 auto prefix =
"CueNote" + String (i);
364 out.writeIntBigEndian (values.getValue (prefix +
"TimeStamp",
"0").getIntValue());
365 out.writeShortBigEndian ((
short) values.getValue (prefix +
"Identifier",
"0").getIntValue());
367 auto comment = values.getValue (prefix +
"Text", String());
368 auto commentLength = jmin (comment.getNumBytesAsUTF8(), (
size_t) 65534);
370 out.writeShortBigEndian ((
short) commentLength + 1);
371 out.write (comment.toUTF8(), commentLength);
374 if ((out.getDataSize() & 1) != 0)
389 using namespace AiffFileHelpers;
397 if (nextType == chunkName (
"AIFF") || nextType == chunkName (
"AIFC"))
399 bool hasGotVer =
false;
400 bool hasGotData =
false;
401 bool hasGotType =
false;
409 if (type == chunkName (
"FVER"))
414 if (ver != 0 && ver != (
int) 0xa2805140)
417 else if (type == chunkName (
"COMM"))
426 unsigned char sampleRateBytes[10];
428 const int byte0 = sampleRateBytes[0];
430 if ((byte0 & 0x80) != 0
431 || byte0 <= 0x3F || byte0 > 0x40
432 || (byte0 == 0x40 && sampleRateBytes[1] > 0x1C))
443 littleEndian =
false;
449 if (compType == chunkName (
"NONE") || compType == chunkName (
"twos"))
451 littleEndian =
false;
453 else if (compType == chunkName (
"sowt"))
457 else if (compType == chunkName (
"fl32") || compType == chunkName (
"FL32"))
459 littleEndian =
false;
469 else if (type == chunkName (
"SSND"))
477 else if (type == chunkName (
"MARK"))
485 for (uint16 i = 0; i < numCues; ++i)
496 if ((stringLength & 1) == 0)
499 auto prefixCue =
"Cue" +
String (i);
503 auto prefixLabel =
"CueLabel" +
String (i);
508 else if (type == chunkName (
"COMT"))
513 for (uint16 i = 0; i < numNotes; ++i)
522 auto prefix =
"CueNote" +
String (i);
528 else if (type == chunkName (
"INST"))
531 inst.
calloc (jmax ((
size_t) length + 1,
sizeof (InstChunk)), 1);
535 else if (type == chunkName (
"basc"))
539 else if (type == chunkName (
"cate"))
542 AiffFileHelpers::CATEChunk::read (*
input, length));
544 else if ((hasGotVer && hasGotData && hasGotType)
545 || chunkEnd < input->getPosition()
561 bool readSamples (
int** destSamples,
int numDestChannels,
int startOffsetInDestBuffer,
562 int64 startSampleInFile,
int numSamples)
override
572 while (numSamples > 0)
574 const int tempBufSize = 480 * 3 * 4;
575 char tempBuffer [tempBufSize];
577 const int numThisTime = jmin (tempBufSize / bytesPerFrame, numSamples);
578 const int bytesRead =
input->
read (tempBuffer, numThisTime * bytesPerFrame);
580 if (bytesRead < numThisTime * bytesPerFrame)
582 jassert (bytesRead >= 0);
583 zeromem (tempBuffer + bytesRead, (
size_t) (numThisTime * bytesPerFrame - bytesRead));
588 destSamples, startOffsetInDestBuffer, numDestChannels,
592 destSamples, startOffsetInDestBuffer, numDestChannels,
595 startOffsetInDestBuffer += numThisTime;
596 numSamples -= numThisTime;
602 template <
typename Endianness>
603 static void copySampleData (
unsigned int numBitsPerSample,
bool floatingPointData,
604 int*
const* destSamples,
int startOffsetInDestBuffer,
int numDestChannels,
605 const void* sourceData,
int numberOfChannels,
int numSamples) noexcept
607 switch (numBitsPerSample)
609 case 8: ReadHelper<AudioData::Int32, AudioData::Int8, Endianness>::read (destSamples, startOffsetInDestBuffer, numDestChannels, sourceData, numberOfChannels, numSamples);
break;
610 case 16: ReadHelper<AudioData::Int32, AudioData::Int16, Endianness>::read (destSamples, startOffsetInDestBuffer, numDestChannels, sourceData, numberOfChannels, numSamples);
break;
611 case 24: ReadHelper<AudioData::Int32, AudioData::Int24, Endianness>::read (destSamples, startOffsetInDestBuffer, numDestChannels, sourceData, numberOfChannels, numSamples);
break;
612 case 32:
if (floatingPointData) ReadHelper<AudioData::Float32, AudioData::Float32, Endianness>::read (destSamples, startOffsetInDestBuffer, numDestChannels, sourceData, numberOfChannels, numSamples);
613 else ReadHelper<AudioData::Int32, AudioData::Int32, Endianness>::read (destSamples, startOffsetInDestBuffer, numDestChannels, sourceData, numberOfChannels, numSamples);
615 default: jassertfalse;
break;
620 int64 dataChunkStart;
624 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AiffAudioFormatReader)
632 unsigned int numChans,
unsigned int bits,
636 using namespace AiffFileHelpers;
656 if ((bytesWritten & 1) != 0)
657 output->writeByte (0);
663 bool write (
const int** data,
int numSamples)
override
665 jassert (numSamples >= 0);
666 jassert (data !=
nullptr && *data !=
nullptr);
672 tempBlock.ensureSize (bytes,
false);
680 default: jassertfalse;
break;
683 if (bytesWritten + bytes >= (
size_t) 0xfff00000
684 || ! output->write (tempBlock.getData(), bytes))
694 bytesWritten += bytes;
700 MemoryBlock tempBlock, markChunk, comtChunk, instChunk;
702 int64 headerPosition = 0;
703 bool writeFailed =
false;
707 using namespace AiffFileHelpers;
709 const bool couldSeekOk = output->setPosition (headerPosition);
710 ignoreUnused (couldSeekOk);
714 jassert (couldSeekOk);
716 auto headerLen = (int) (54 + (markChunk.
getSize() > 0 ? markChunk.
getSize() + 8 : 0)
720 audioBytes += (audioBytes & 1);
722 output->writeInt (chunkName (
"FORM"));
723 output->writeIntBigEndian (headerLen + audioBytes - 8);
724 output->writeInt (chunkName (
"AIFF"));
725 output->writeInt (chunkName (
"COMM"));
726 output->writeIntBigEndian (18);
731 uint8 sampleRateBytes[10] = {};
735 sampleRateBytes[0] = 0x3f;
736 sampleRateBytes[1] = 0xff;
737 sampleRateBytes[2] = 0x80;
741 int mask = 0x40000000;
742 sampleRateBytes[0] = 0x40;
747 sampleRateBytes[1] = 0x1d;
754 for (i = 0; i <= 32 ; ++i)
764 sampleRateBytes[1] = (uint8) (29 - i);
765 sampleRateBytes[2] = (uint8) ((n >> 24) & 0xff);
766 sampleRateBytes[3] = (uint8) ((n >> 16) & 0xff);
767 sampleRateBytes[4] = (uint8) ((n >> 8) & 0xff);
768 sampleRateBytes[5] = (uint8) (n & 0xff);
772 output->write (sampleRateBytes, 10);
776 output->writeInt (chunkName (
"MARK"));
777 output->writeIntBigEndian ((
int) markChunk.
getSize());
778 *output << markChunk;
783 output->writeInt (chunkName (
"COMT"));
784 output->writeIntBigEndian ((
int) comtChunk.
getSize());
785 *output << comtChunk;
790 output->writeInt (chunkName (
"INST"));
791 output->writeIntBigEndian ((
int) instChunk.
getSize());
792 *output << instChunk;
795 output->writeInt (chunkName (
"SSND"));
796 output->writeIntBigEndian (audioBytes + 8);
797 output->writeInt (0);
798 output->writeInt (0);
800 jassert (output->getPosition() == headerLen);
803 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AiffAudioFormatWriter)
813 littleEndian (reader.littleEndian)
817 bool readSamples (
int** destSamples,
int numDestChannels,
int startOffsetInDestBuffer,
818 int64 startSampleInFile,
int numSamples)
override
823 if (map ==
nullptr || ! mappedSection.contains (
Range<int64> (startSampleInFile, startSampleInFile + numSamples)))
830 AiffAudioFormatReader::copySampleData<AudioData::LittleEndian>
832 numDestChannels, sampleToPointer (startSampleInFile), (
int)
numChannels, numSamples);
834 AiffAudioFormatReader::copySampleData<AudioData::BigEndian>
836 numDestChannels, sampleToPointer (startSampleInFile), (
int)
numChannels, numSamples);
841 void getSample (int64 sample,
float* result)
const noexcept
override
845 if (map ==
nullptr || ! mappedSection.contains (sample))
849 zeromem (result, (
size_t) num *
sizeof (
float));
853 float** dest = &result;
854 const void* source = sampleToPointer (sample);
866 default: jassertfalse;
break;
879 default: jassertfalse;
break;
888 if (map ==
nullptr || numSamples <= 0 || ! mappedSection.contains (
Range<int64> (startSampleInFile, startSampleInFile + numSamples)))
890 jassert (numSamples <= 0);
892 for (
int i = 0; i < numChannelsToRead; ++i)
900 case 8: scanMinAndMax<AudioData::UInt8> (startSampleInFile, numSamples, results, numChannelsToRead);
break;
901 case 16: scanMinAndMax<AudioData::Int16> (startSampleInFile, numSamples, results, numChannelsToRead);
break;
902 case 24: scanMinAndMax<AudioData::Int24> (startSampleInFile, numSamples, results, numChannelsToRead);
break;
903 case 32:
if (
usesFloatingPointData) scanMinAndMax<AudioData::Float32> (startSampleInFile, numSamples, results, numChannelsToRead);
904 else scanMinAndMax<AudioData::Int32> (startSampleInFile, numSamples, results, numChannelsToRead);
906 default: jassertfalse;
break;
913 const bool littleEndian;
915 template <
typename SampleType>
916 void scanMinAndMax (int64 startSampleInFile, int64 numSamples,
Range<float>* results,
int numChannelsToRead)
const noexcept
918 for (
int i = 0; i < numChannelsToRead; ++i)
919 results[i] = scanMinAndMaxForChannel<SampleType> (i, startSampleInFile, numSamples);
922 template <
typename SampleType>
923 Range<float> scanMinAndMaxForChannel (
int channel, int64 startSampleInFile, int64 numSamples)
const noexcept
925 return littleEndian ? scanMinAndMaxInterleaved<SampleType, AudioData::LittleEndian> (channel, startSampleInFile, numSamples)
926 : scanMinAndMaxInterleaved<SampleType, AudioData::BigEndian> (channel, startSampleInFile, numSamples);
929 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MemoryMappedAiffReader)
938 return { 22050, 32000, 44100, 48000, 88200, 96000, 176400, 192000 };
943 return { 8, 16, 24 };
958 return type == 0x41494646 || type == 0x41494643
959 || type == 0x61696666 || type == 0x61696663 ;
967 if (w->sampleRate > 0 && w->numChannels > 0)
970 if (! deleteStreamIfOpeningFails)
996 unsigned int numberOfChannels,
1003 (
unsigned int) bitsPerSample, metadataValues);
static JUCE_CONSTEXPR uint16 bigEndianShort(const void *bytes) noexcept
Turns 2 bytes into a big-endian integer.
static JUCE_CONSTEXPR uint32 bigEndianInt(const void *bytes) noexcept
Turns 4 bytes into a big-endian integer.
static Type swapIfLittleEndian(Type value) noexcept
Swaps the byte order of a signed or unsigned integer if the CPU is little-endian.
static JUCE_CONSTEXPR uint32 littleEndianInt(const void *bytes) noexcept
Turns 4 bytes into a little-endian integer.
static bool isLowerCase(juce_wchar character) noexcept
Checks whether a unicode character is lower-case.
static bool isLetterOrDigit(char character) noexcept
Checks whether a character is alphabetic or numeric.
static bool isUpperCase(juce_wchar character) noexcept
Checks whether a unicode character is upper-case.
Represents a local file or directory.
OSType getMacOSType() const
OSX ONLY - Finds the OSType of a file from the its resources.
FileInputStream * createInputStream() const
Creates a stream to read from this file.
Very simple container class to hold a pointer to some data on the heap.
void calloc(SizeType newNumElements, const size_t elementSize=sizeof(ElementType))
Allocates a specified amount of memory and clears it.
A class to hold a resizable block of raw data.
String toString() const
Attempts to parse the contents of the block as a zero-terminated UTF8 string.
size_t getSize() const noexcept
Returns the block's current allocated size, in bytes.
void readMaxLevels(int64 startSampleInFile, int64 numSamples, Range< float > *results, int numChannelsToRead) override
Finds the highest and lowest sample levels from a section of the audio stream.
bool readSamples(int **destSamples, int numDestChannels, int startOffsetInDestBuffer, int64 startSampleInFile, int numSamples) override
Subclasses must implement this method to perform the low-level read operation.
void getSample(int64 sample, float *result) const noexcept override
Returns the samples for all channels at a given sample position.
The base class for streams that write data to some kind of destination.
virtual int64 getPosition()=0
Returns the stream's current position.
A container for holding a set of strings which are keyed by another string.
String getValue(StringRef, const String &defaultReturnValue) const
Finds the value corresponding to a key string.
void set(const String &key, const String &value)
Adds or amends a key/value pair.
int size() const noexcept
Returns the number of strings in the array.
int getIntValue() const noexcept
Reads the value of the string as a decimal number (up to 32 bits in size).