@@ -109,7 +109,11 @@ class AudioFile
109109 // =============================================================
110110 /* * Loads an audio file from data in memory */
111111 bool loadFromMemory (const std::vector<uint8_t >& fileData);
112-
112+
113+ // =============================================================
114+ /* * Saves an audio file to data in memory */
115+ bool saveToMemory (std::vector<uint8_t >& fileData, AudioFileFormat format = AudioFileFormat::Wave);
116+
113117 // =============================================================
114118 /* * @Returns the sample rate */
115119 uint32_t getSampleRate () const ;
@@ -190,10 +194,10 @@ class AudioFile
190194 // =============================================================
191195 bool decodeWaveFile (const std::vector<uint8_t >& fileData);
192196 bool decodeAiffFile (const std::vector<uint8_t >& fileData);
193-
197+
194198 // =============================================================
195- bool saveToWaveFile ( const std::string& filePath );
196- bool saveToAiffFile ( const std::string& filePath );
199+ bool encodeWaveFile ( std::vector< uint8_t >& fileData );
200+ bool encodeAiffFile ( std::vector< uint8_t >& fileData );
197201
198202 // =============================================================
199203 void clearAudioBuffer ();
@@ -889,25 +893,31 @@ void AudioFile<T>::addSampleRateToAiffData (std::vector<uint8_t>& fileData, uint
889893// =============================================================
890894template <class T >
891895bool AudioFile<T>::save (const std::string& filePath, AudioFileFormat format)
896+ {
897+ std::vector<uint8_t > fileData;
898+ return saveToMemory (fileData, format) && writeDataToFile (fileData, filePath);
899+ }
900+
901+ // =============================================================
902+ template <class T >
903+ bool AudioFile<T>::saveToMemory (std::vector<uint8_t >& fileData, AudioFileFormat format)
892904{
893905 if (format == AudioFileFormat::Wave)
894906 {
895- return saveToWaveFile (filePath );
907+ return encodeWaveFile (fileData );
896908 }
897909 else if (format == AudioFileFormat::Aiff)
898910 {
899- return saveToAiffFile (filePath );
911+ return encodeAiffFile (fileData );
900912 }
901913
902914 return false ;
903915}
904916
905917// =============================================================
906918template <class T >
907- bool AudioFile<T>::saveToWaveFile (const std::string& filePath)
908- {
909- std::vector<uint8_t > fileData;
910-
919+ bool AudioFile<T>::encodeWaveFile (std::vector<uint8_t >& fileData)
920+ {
911921 int32_t dataChunkSize = getNumSamplesPerChannel () * (getNumChannels () * bitDepth / 8 );
912922 int16_t audioFormat = bitDepth == 32 && std::is_floating_point_v<T> ? WavAudioFormat::IEEEFloat : WavAudioFormat::PCM;
913923 int32_t formatChunkSize = audioFormat == WavAudioFormat::PCM ? 16 : 18 ;
@@ -1011,20 +1021,17 @@ bool AudioFile<T>::saveToWaveFile (const std::string& filePath)
10111021 // check that the various sizes we put in the metadata are correct
10121022 if (fileSizeInBytes != static_cast <int32_t > (fileData.size () - 8 ) || dataChunkSize != (getNumSamplesPerChannel () * getNumChannels () * (bitDepth / 8 )))
10131023 {
1014- reportError (" ERROR: couldn't save file to " + filePath );
1024+ reportError (" ERROR: Incorrect file or data chunk size. " );
10151025 return false ;
10161026 }
10171027
1018- // try to write the file
1019- return writeDataToFile (fileData, filePath);
1028+ return true ;
10201029}
10211030
10221031// =============================================================
10231032template <class T >
1024- bool AudioFile<T>::saveToAiffFile (const std::string& filePath)
1025- {
1026- std::vector<uint8_t > fileData;
1027-
1033+ bool AudioFile<T>::encodeAiffFile (std::vector<uint8_t >& fileData)
1034+ {
10281035 int32_t numBytesPerSample = bitDepth / 8 ;
10291036 int32_t numBytesPerFrame = numBytesPerSample * getNumChannels ();
10301037 int32_t totalNumAudioSampleBytes = getNumSamplesPerChannel () * numBytesPerFrame;
@@ -1116,12 +1123,11 @@ bool AudioFile<T>::saveToAiffFile (const std::string& filePath)
11161123 // check that the various sizes we put in the metadata are correct
11171124 if (fileSizeInBytes != static_cast <int32_t > (fileData.size () - 8 ) || soundDataChunkSize != getNumSamplesPerChannel () * numBytesPerFrame + 8 )
11181125 {
1119- reportError (" ERROR: couldn't save file to " + filePath );
1126+ reportError (" ERROR: Incorrect file or data chunk size. " );
11201127 return false ;
11211128 }
11221129
1123- // try to write the file
1124- return writeDataToFile (fileData, filePath);
1130+ return true ;
11251131}
11261132
11271133// =============================================================
0 commit comments