forked from adamstark/AudioFile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileWritingTests.cpp
98 lines (79 loc) · 3.39 KB
/
FileWritingTests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "doctest.h"
#include <iostream>
#include <vector>
#include <cmath>
#include <AudioFile.h>
//=============================================================
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<float> 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<int> sampleRates = {22050, 44100, 48000, 96000};
std::vector<int> bitDepths = {8, 16, 24, 32};
std::vector<int> numChannels = {1, 2, 8};
std::vector<AudioFileFormat> 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<float> 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);
}
}