30static const char*
const aiffFormatName =
"AIFF file";
43namespace AiffFileHelpers
48 #pragma pack (push, 1)
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));
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));
173 case neither:
keyString =
"neither";
break;
203 static bool isValidTag (
const char* d)
noexcept
210 static bool isAppleGenre (
const String&
tag)
noexcept
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;
409 if (type == chunkName (
"FVER"))
414 if (
ver != 0 &&
ver != (
int) 0xa2805140)
417 else if (type == chunkName (
"COMM"))
430 if ((
byte0 & 0x80) != 0
443 littleEndian =
false;
451 littleEndian =
false;
453 else if (
compType == chunkName (
"sowt"))
459 littleEndian =
false;
469 else if (type == chunkName (
"SSND"))
477 else if (type == chunkName (
"MARK"))
485 for (
uint16 i = 0; i < numCues; ++i)
508 else if (type == chunkName (
"COMT"))
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));
572 while (numSamples > 0)
602 template <
typename Endianness>
615 default: jassertfalse;
break;
620 int64 dataChunkStart;
624 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AiffAudioFormatReader)
636 using namespace AiffFileHelpers;
638 if (metadataValues.
size() > 0)
643 jassert (metadataValues.
getValue (
"MetaDataSource",
"None") !=
"WAV");
645 MarkChunk::create (markChunk, metadataValues);
646 COMTChunk::create (comtChunk, metadataValues);
647 InstChunk::create (instChunk, metadataValues);
650 headerPosition =
out->getPosition();
656 if ((bytesWritten & 1) != 0)
663 bool write (
const int** data,
int numSamples)
override
665 jassert (numSamples >= 0);
666 jassert (data !=
nullptr && *data !=
nullptr);
680 default: jassertfalse;
break;
683 if (bytesWritten +
bytes >= (
size_t) 0xfff00000
694 bytesWritten +=
bytes;
695 lengthInSamples += (
uint64) numSamples;
700 MemoryBlock tempBlock, markChunk, comtChunk, instChunk;
701 uint64 lengthInSamples = 0, bytesWritten = 0;
702 int64 headerPosition = 0;
703 bool writeFailed =
false;
707 using namespace AiffFileHelpers;
717 + (comtChunk.getSize() > 0 ? comtChunk.getSize() + 8 : 0)
718 + (instChunk.getSize() > 0 ? instChunk.getSize() + 8 : 0));
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);
803 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AiffAudioFormatWriter)
813 littleEndian (reader.littleEndian)
830 AiffAudioFormatReader::copySampleData<AudioData::LittleEndian>
834 AiffAudioFormatReader::copySampleData<AudioData::BigEndian>
849 zeromem (result,
sizeof (
float) * (
size_t)
num);
853 float** dest = &result;
866 default: jassertfalse;
break;
879 default: jassertfalse;
break;
890 jassert (numSamples <= 0);
906 default: jassertfalse;
break;
911 const bool littleEndian;
913 template <
typename SampleType>
920 template <
typename SampleType>
921 Range<float> scanMinAndMaxForChannel (
int channel, int64 startSampleInFile, int64 numSamples)
const noexcept
923 return littleEndian ? scanMinAndMaxInterleaved<SampleType, AudioData::LittleEndian> (channel, startSampleInFile, numSamples)
927 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MemoryMappedAiffReader)
936 return { 22050, 32000, 44100, 48000, 88200, 96000, 176400, 192000 };
941 return { 8, 16, 24 };
956 return type == 0x41494646 || type == 0x41494643
957 || type == 0x61696666 || type == 0x61696663 ;
965 if (
w->sampleRate > 0 &&
w->numChannels > 0)
994 unsigned int numberOfChannels,
1001 (
unsigned int) bitsPerSample, metadataValues);
Holds a resizable array of primitive or copy-by-value objects.
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.
A class to hold a resizable block of raw data.
void ensureSize(const size_t minimumSize, bool initialiseNewSpaceToZero=false)
Increases the block's size only if it's smaller than a given size.
size_t getSize() const noexcept
Returns the block's current allocated size, in bytes.
void * getData() const noexcept
Returns a void pointer to the data.
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 bool write(const void *dataToWrite, size_t numberOfBytes)=0
Writes a block of data to the stream.
virtual int64 getPosition()=0
Returns the stream's current position.
virtual bool writeByte(char byte)
Writes a single byte to the stream.
virtual bool writeIntBigEndian(int value)
Writes a 32-bit integer to the stream in a big-endian byte order.
virtual bool setPosition(int64 newPosition)=0
Tries to move the stream's output position.
virtual bool writeShortBigEndian(short value)
Writes a 16-bit integer to the stream in a big-endian byte order.
virtual bool writeInt(int value)
Writes a 32-bit integer to the stream in a little-endian byte order.
JUCE_CONSTEXPR bool contains(const ValueType position) const noexcept
Returns true if the given position lies inside this range.
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).