MCPcopy Create free account
hub / github.com/adamstark/AudioFile / encodeAiffFile

Method encodeAiffFile

AudioFile.h:1031–1129  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1029//=============================================================
1030template <class T>
1031bool AudioFile<T>::encodeAiffFile (std::vector<uint8_t>& fileData)
1032{
1033 int32_t numBytesPerSample = bitDepth / 8;
1034 int32_t numBytesPerFrame = numBytesPerSample * getNumChannels();
1035 int32_t totalNumAudioSampleBytes = getNumSamplesPerChannel() * numBytesPerFrame;
1036 int32_t soundDataChunkSize = totalNumAudioSampleBytes + 8;
1037 int32_t iXMLChunkSize = static_cast<int32_t> (iXMLChunk.size());
1038
1039 // -----------------------------------------------------------
1040 // HEADER CHUNK
1041 addStringToFileData (fileData, "FORM");
1042
1043 // The file size in bytes is the header chunk size (4, not counting FORM and AIFF) + the COMM
1044 // chunk size (26) + the metadata part of the SSND chunk plus the actual data chunk size
1045 int32_t fileSizeInBytes = 4 + 26 + 16 + totalNumAudioSampleBytes;
1046 if (iXMLChunkSize > 0)
1047 {
1048 fileSizeInBytes += (8 + iXMLChunkSize);
1049 }
1050
1051 addInt32ToFileData (fileData, fileSizeInBytes, Endianness::BigEndian);
1052
1053 addStringToFileData (fileData, "AIFF");
1054
1055 // -----------------------------------------------------------
1056 // COMM CHUNK
1057 addStringToFileData (fileData, "COMM");
1058 addInt32ToFileData (fileData, 18, Endianness::BigEndian); // commChunkSize
1059 addInt16ToFileData (fileData, getNumChannels(), Endianness::BigEndian); // num channels
1060 addInt32ToFileData (fileData, getNumSamplesPerChannel(), Endianness::BigEndian); // num samples per channel
1061 addInt16ToFileData (fileData, bitDepth, Endianness::BigEndian); // bit depth
1062 addSampleRateToAiffData (fileData, sampleRate);
1063
1064 // -----------------------------------------------------------
1065 // SSND CHUNK
1066 addStringToFileData (fileData, "SSND");
1067 addInt32ToFileData (fileData, soundDataChunkSize, Endianness::BigEndian);
1068 addInt32ToFileData (fileData, 0, Endianness::BigEndian); // offset
1069 addInt32ToFileData (fileData, 0, Endianness::BigEndian); // block size
1070
1071 for (int i = 0; i < getNumSamplesPerChannel(); i++)
1072 {
1073 for (int channel = 0; channel < getNumChannels(); channel++)
1074 {
1075 if (bitDepth == 8)
1076 {
1077 uint8_t byte = static_cast<uint8_t> (AudioSampleConverter<T>::sampleToSignedByte (samples[channel][i]));
1078 fileData.push_back (byte);
1079 }
1080 else if (bitDepth == 16)
1081 {
1082 int16_t sampleAsInt = AudioSampleConverter<T>::sampleToSixteenBitInt (samples[channel][i]);
1083 addInt16ToFileData (fileData, sampleAsInt, Endianness::BigEndian);
1084 }
1085 else if (bitDepth == 24)
1086 {
1087 int32_t sampleAsIntAgain = AudioSampleConverter<T>::sampleToTwentyFourBitInt (samples[channel][i]);
1088

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected