@@ -109,7 +109,11 @@ class AudioFile
109
109
// =============================================================
110
110
/* * Loads an audio file from data in memory */
111
111
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
+
113
117
// =============================================================
114
118
/* * @Returns the sample rate */
115
119
uint32_t getSampleRate () const ;
@@ -190,10 +194,10 @@ class AudioFile
190
194
// =============================================================
191
195
bool decodeWaveFile (const std::vector<uint8_t >& fileData);
192
196
bool decodeAiffFile (const std::vector<uint8_t >& fileData);
193
-
197
+
194
198
// =============================================================
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 );
197
201
198
202
// =============================================================
199
203
void clearAudioBuffer ();
@@ -889,25 +893,31 @@ void AudioFile<T>::addSampleRateToAiffData (std::vector<uint8_t>& fileData, uint
889
893
// =============================================================
890
894
template <class T >
891
895
bool 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)
892
904
{
893
905
if (format == AudioFileFormat::Wave)
894
906
{
895
- return saveToWaveFile (filePath );
907
+ return encodeWaveFile (fileData );
896
908
}
897
909
else if (format == AudioFileFormat::Aiff)
898
910
{
899
- return saveToAiffFile (filePath );
911
+ return encodeAiffFile (fileData );
900
912
}
901
913
902
914
return false ;
903
915
}
904
916
905
917
// =============================================================
906
918
template <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
+ {
911
921
int32_t dataChunkSize = getNumSamplesPerChannel () * (getNumChannels () * bitDepth / 8 );
912
922
int16_t audioFormat = bitDepth == 32 && std::is_floating_point_v<T> ? WavAudioFormat::IEEEFloat : WavAudioFormat::PCM;
913
923
int32_t formatChunkSize = audioFormat == WavAudioFormat::PCM ? 16 : 18 ;
@@ -1011,20 +1021,17 @@ bool AudioFile<T>::saveToWaveFile (const std::string& filePath)
1011
1021
// check that the various sizes we put in the metadata are correct
1012
1022
if (fileSizeInBytes != static_cast <int32_t > (fileData.size () - 8 ) || dataChunkSize != (getNumSamplesPerChannel () * getNumChannels () * (bitDepth / 8 )))
1013
1023
{
1014
- reportError (" ERROR: couldn't save file to " + filePath );
1024
+ reportError (" ERROR: Incorrect file or data chunk size. " );
1015
1025
return false ;
1016
1026
}
1017
1027
1018
- // try to write the file
1019
- return writeDataToFile (fileData, filePath);
1028
+ return true ;
1020
1029
}
1021
1030
1022
1031
// =============================================================
1023
1032
template <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
+ {
1028
1035
int32_t numBytesPerSample = bitDepth / 8 ;
1029
1036
int32_t numBytesPerFrame = numBytesPerSample * getNumChannels ();
1030
1037
int32_t totalNumAudioSampleBytes = getNumSamplesPerChannel () * numBytesPerFrame;
@@ -1116,12 +1123,11 @@ bool AudioFile<T>::saveToAiffFile (const std::string& filePath)
1116
1123
// check that the various sizes we put in the metadata are correct
1117
1124
if (fileSizeInBytes != static_cast <int32_t > (fileData.size () - 8 ) || soundDataChunkSize != getNumSamplesPerChannel () * numBytesPerFrame + 8 )
1118
1125
{
1119
- reportError (" ERROR: couldn't save file to " + filePath );
1126
+ reportError (" ERROR: Incorrect file or data chunk size. " );
1120
1127
return false ;
1121
1128
}
1122
1129
1123
- // try to write the file
1124
- return writeDataToFile (fileData, filePath);
1130
+ return true ;
1125
1131
}
1126
1132
1127
1133
// =============================================================
0 commit comments