diff --git a/AudioFile.h b/AudioFile.h index 3c53243..bc71da8 100644 --- a/AudioFile.h +++ b/AudioFile.h @@ -109,7 +109,11 @@ class AudioFile //============================================================= /** Loads an audio file from data in memory */ bool loadFromMemory (const std::vector& fileData); - + + //============================================================= + /** Saves an audio file to data in memory */ + bool saveToMemory (std::vector& fileData, AudioFileFormat format = AudioFileFormat::Wave); + //============================================================= /** @Returns the sample rate */ uint32_t getSampleRate() const; @@ -190,10 +194,10 @@ class AudioFile //============================================================= bool decodeWaveFile (const std::vector& fileData); bool decodeAiffFile (const std::vector& fileData); - + //============================================================= - bool saveToWaveFile (const std::string& filePath); - bool saveToAiffFile (const std::string& filePath); + bool encodeWaveFile (std::vector& fileData); + bool encodeAiffFile (std::vector& fileData); //============================================================= void clearAudioBuffer(); @@ -889,14 +893,22 @@ void AudioFile::addSampleRateToAiffData (std::vector& fileData, uint //============================================================= template bool AudioFile::save (const std::string& filePath, AudioFileFormat format) +{ + std::vector fileData; + return saveToMemory (fileData, format) && writeDataToFile (fileData, filePath); +} + +//============================================================= +template +bool AudioFile::saveToMemory (std::vector& fileData, AudioFileFormat format) { if (format == AudioFileFormat::Wave) { - return saveToWaveFile (filePath); + return encodeWaveFile (fileData); } else if (format == AudioFileFormat::Aiff) { - return saveToAiffFile (filePath); + return encodeAiffFile (fileData); } return false; @@ -904,10 +916,8 @@ bool AudioFile::save (const std::string& filePath, AudioFileFormat format) //============================================================= template -bool AudioFile::saveToWaveFile (const std::string& filePath) -{ - std::vector fileData; - +bool AudioFile::encodeWaveFile (std::vector& fileData) +{ int32_t dataChunkSize = getNumSamplesPerChannel() * (getNumChannels() * bitDepth / 8); int16_t audioFormat = bitDepth == 32 && std::is_floating_point_v ? WavAudioFormat::IEEEFloat : WavAudioFormat::PCM; int32_t formatChunkSize = audioFormat == WavAudioFormat::PCM ? 16 : 18; @@ -1011,20 +1021,17 @@ bool AudioFile::saveToWaveFile (const std::string& filePath) // check that the various sizes we put in the metadata are correct if (fileSizeInBytes != static_cast (fileData.size() - 8) || dataChunkSize != (getNumSamplesPerChannel() * getNumChannels() * (bitDepth / 8))) { - reportError ("ERROR: couldn't save file to " + filePath); + reportError ("ERROR: Incorrect file or data chunk size."); return false; } - // try to write the file - return writeDataToFile (fileData, filePath); + return true; } //============================================================= template -bool AudioFile::saveToAiffFile (const std::string& filePath) -{ - std::vector fileData; - +bool AudioFile::encodeAiffFile (std::vector& fileData) +{ int32_t numBytesPerSample = bitDepth / 8; int32_t numBytesPerFrame = numBytesPerSample * getNumChannels(); int32_t totalNumAudioSampleBytes = getNumSamplesPerChannel() * numBytesPerFrame; @@ -1116,12 +1123,11 @@ bool AudioFile::saveToAiffFile (const std::string& filePath) // check that the various sizes we put in the metadata are correct if (fileSizeInBytes != static_cast (fileData.size() - 8) || soundDataChunkSize != getNumSamplesPerChannel() * numBytesPerFrame + 8) { - reportError ("ERROR: couldn't save file to " + filePath); + reportError ("ERROR: Incorrect file or data chunk size."); return false; } - // try to write the file - return writeDataToFile (fileData, filePath); + return true; } //============================================================= diff --git a/README.md b/README.md index 6ba20da..98b65e2 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,17 @@ AudioFile is written and maintained by Adam Stark. // Aiff file audioFile.save ("path/to/desired/audioFile.aif", AudioFileFormat::Aiff); +### Save the audio file to memory + +Write the audio file data directly to a vector of bytes (without writing to a file on disk): + + std::vector fileData; + saveToMemory (fileData, AudioFileFormat::Wave); + + or + + saveToMemory (fileData, AudioFileFormat::Aiff); + ## Examples 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: - [Metalsofa](https://github.com/Metalsofa) - [mrpossoms](https://github.com/mrpossoms) - [mynameisjohn](https://github.com/mynameisjohn) +- [nicmell](https://github.com/nicmell) - [Sidelobe](https://github.com/Sidelobe) - [sschaetz](https://github.com/sschaetz) - [Yhcrown](https://github.com/Yhcrown)