Skip to content

Commit 4f0911b

Browse files
authored
Merge pull request #94 from nicmell/master
Added saveToMemory method
2 parents 1713396 + f715d0f commit 4f0911b

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

AudioFile.h

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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
//=============================================================
890894
template <class T>
891895
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)
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
//=============================================================
906918
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+
{
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
//=============================================================
10231032
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+
{
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
//=============================================================

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ AudioFile is written and maintained by Adam Stark.
119119
// Aiff file
120120
audioFile.save ("path/to/desired/audioFile.aif", AudioFileFormat::Aiff);
121121

122+
### Save the audio file to memory
123+
124+
Write the audio file data directly to a vector of bytes (without writing to a file on disk):
125+
126+
std::vector<uint8_t> fileData;
127+
saveToMemory (fileData, AudioFileFormat::Wave);
128+
129+
or
130+
131+
saveToMemory (fileData, AudioFileFormat::Aiff);
132+
122133
## Examples
123134

124135
Please see the `examples` folder for some examples on library usage.
@@ -256,6 +267,7 @@ Many thanks to the following people for their contributions to this library:
256267
- [Metalsofa](https://github.com/Metalsofa)
257268
- [mrpossoms](https://github.com/mrpossoms)
258269
- [mynameisjohn](https://github.com/mynameisjohn)
270+
- [nicmell](https://github.com/nicmell)
259271
- [Sidelobe](https://github.com/Sidelobe)
260272
- [sschaetz](https://github.com/sschaetz)
261273
- [Yhcrown](https://github.com/Yhcrown)

0 commit comments

Comments
 (0)