| 902 | //============================================================= |
| 903 | template <class T> |
| 904 | bool AudioFile<T>::encodeWaveFile (std::vector<uint8_t>& fileData) |
| 905 | { |
| 906 | int32_t dataChunkSize = getNumSamplesPerChannel() * (getNumChannels() * bitDepth / 8); |
| 907 | int16_t audioFormat = bitDepth == 32 && std::is_floating_point_v<T> ? WavAudioFormat::IEEEFloat : WavAudioFormat::PCM; |
| 908 | int32_t formatChunkSize = audioFormat == WavAudioFormat::PCM ? 16 : 18; |
| 909 | int32_t iXMLChunkSize = static_cast<int32_t> (iXMLChunk.size()); |
| 910 | |
| 911 | // ----------------------------------------------------------- |
| 912 | // HEADER CHUNK |
| 913 | addStringToFileData (fileData, "RIFF"); |
| 914 | |
| 915 | // The file size in bytes is the header chunk size (4, not counting RIFF and WAVE) + the format |
| 916 | // chunk size (24) + the metadata part of the data chunk plus the actual data chunk size |
| 917 | int32_t fileSizeInBytes = 4 + formatChunkSize + 8 + 8 + dataChunkSize; |
| 918 | if (iXMLChunkSize > 0) |
| 919 | { |
| 920 | fileSizeInBytes += (8 + iXMLChunkSize); |
| 921 | } |
| 922 | |
| 923 | addInt32ToFileData (fileData, fileSizeInBytes); |
| 924 | |
| 925 | addStringToFileData (fileData, "WAVE"); |
| 926 | |
| 927 | // ----------------------------------------------------------- |
| 928 | // FORMAT CHUNK |
| 929 | addStringToFileData (fileData, "fmt "); |
| 930 | addInt32ToFileData (fileData, formatChunkSize); // format chunk size (16 for PCM) |
| 931 | addInt16ToFileData (fileData, audioFormat); // audio format |
| 932 | addInt16ToFileData (fileData, static_cast<int16_t> (getNumChannels())); // num channels |
| 933 | addInt32ToFileData (fileData, static_cast<int32_t> (sampleRate)); // sample rate |
| 934 | |
| 935 | int32_t numBytesPerSecond = static_cast<int32_t> ((getNumChannels() * sampleRate * bitDepth) / 8); |
| 936 | addInt32ToFileData (fileData, numBytesPerSecond); |
| 937 | |
| 938 | int16_t numBytesPerBlock = getNumChannels() * (bitDepth / 8); |
| 939 | addInt16ToFileData (fileData, numBytesPerBlock); |
| 940 | |
| 941 | addInt16ToFileData (fileData, static_cast<int16_t> (bitDepth)); |
| 942 | |
| 943 | if (audioFormat == WavAudioFormat::IEEEFloat) |
| 944 | addInt16ToFileData (fileData, 0); // extension size |
| 945 | |
| 946 | // ----------------------------------------------------------- |
| 947 | // DATA CHUNK |
| 948 | addStringToFileData (fileData, "data"); |
| 949 | addInt32ToFileData (fileData, dataChunkSize); |
| 950 | |
| 951 | for (int i = 0; i < getNumSamplesPerChannel(); i++) |
| 952 | { |
| 953 | for (int channel = 0; channel < getNumChannels(); channel++) |
| 954 | { |
| 955 | if (bitDepth == 8) |
| 956 | { |
| 957 | uint8_t byte = AudioSampleConverter<T>::sampleToUnsignedByte (samples[channel][i]); |
| 958 | fileData.push_back (byte); |
| 959 | } |
| 960 | else if (bitDepth == 16) |
| 961 | { |
nothing calls this directly
no outgoing calls
no test coverage detected