From f568ac0d4cafc0d71107c062e4dd2a212dac965f Mon Sep 17 00:00:00 2001 From: BahMan Teymouri Nezhad <40192550+PuzzleTakX@users.noreply.github.com> Date: Fri, 19 Aug 2022 16:23:32 +0430 Subject: [PATCH] Create cp.cpp --- main/cp.cpp | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 main/cp.cpp diff --git a/main/cp.cpp b/main/cp.cpp new file mode 100644 index 0000000..234a642 --- /dev/null +++ b/main/cp.cpp @@ -0,0 +1,98 @@ +#include "doctest.h" +#include +#include +#include +#include + +//============================================================= +const std::string projectBuildDirectory = PROJECT_BINARY_DIR; + +//============================================================= +// Writes an audio file with a given number of channels, sample rate, bit deoth and format +// Returns true if it was successful +bool writeTestAudioFile (int numChannels, int sampleRate, int bitDepth, AudioFileFormat format) +{ + float sampleRateAsFloat = (float) sampleRate; + + AudioFile audioFile; + + audioFile.setAudioBufferSize (numChannels, sampleRate * 4); + + for (int i = 0; i < audioFile.getNumSamplesPerChannel(); i++) + { + float sample = sinf (2. * M_PI * ((float) i / sampleRateAsFloat) * 440.) ; + + for (int k = 0; k < audioFile.getNumChannels(); k++) + audioFile.samples[k][i] = sample * 0.5; + } + + audioFile.setSampleRate (sampleRate); + audioFile.setBitDepth (bitDepth); + + std::string numChannelsAsString; + if (numChannels == 1) + numChannelsAsString = "mono"; + else if (numChannels == 2) + numChannelsAsString = "stereo"; + else + numChannelsAsString = std::to_string (numChannels) + " channels"; + + std::string bitDepthAsString = std::to_string (bitDepth); + std::string sampleRateAsString = std::to_string (sampleRate); + + if (format == AudioFileFormat::Wave) + { + return audioFile.save (projectBuildDirectory + "/audio-write-tests/" + numChannelsAsString + "_" + sampleRateAsString + "_" + bitDepthAsString + "bit" + ".wav", format); + } + + else if (format == AudioFileFormat::Aiff) + { + return audioFile.save (projectBuildDirectory + "/audio-write-tests/" + numChannelsAsString + "_" + sampleRateAsString + "_" + bitDepthAsString + "bit" + ".aif", format); + } + + return false; +} + + +//============================================================= +TEST_SUITE ("Writing Tests") +{ + +#if PEFORM_TEST_WRITE_TO_ALL_FORMATS + //============================================================= + TEST_CASE ("WritingTest::WriteSineToneToManyFormats") + { + std::vector sampleRates = {22050, 44100, 48000, 96000}; + std::vector bitDepths = {8, 16, 24, 32}; + std::vector numChannels = {1, 2, 8}; + std::vector audioFormats = {AudioFileFormat::Wave, AudioFileFormat::Aiff}; + + for (auto& sampleRate : sampleRates) + { + for (auto& bitDepth : bitDepths) + { + for (auto& channels : numChannels) + { + for (auto& format : audioFormats) + { + CHECK (writeTestAudioFile (channels, sampleRate, bitDepth, format)); + } + } + } + } + } +#endif + + //============================================================= + TEST_CASE ("WritingTest::WriteFromCopiedSampleBuffer") + { + AudioFile audioFile1, audioFile2; + + bool loadedOK = audioFile1.load (projectBuildDirectory + "/test-audio/wav_stereo_16bit_44100.wav"); + CHECK (loadedOK); + + audioFile2.setAudioBuffer (audioFile1.samples); + bool savedOK = audioFile2.save (projectBuildDirectory + "/audio-write-tests/copied_audio_file.aif", AudioFileFormat::Aiff); + CHECK (savedOK); + } +}