MCPcopy Create free account
hub / github.com/adamstark/AudioFile / writeTestAudioFile

Function writeTestAudioFile

tests/FileWritingTests.cpp:14–116  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12// Returns true if it was successful
13template <typename T>
14void writeTestAudioFile (int numChannels, int sampleRate, int bitDepth, AudioFileFormat format)
15{
16 std::string sampleType;
17 float sampleRateAsFloat = static_cast<float> (sampleRate);
18
19 AudioFile<T> audioFileWriter;
20
21 audioFileWriter.setAudioBufferSize (numChannels, sampleRate * 4);
22
23 // In the case of an integer representation, this value will be
24 T maxValue;
25
26 if constexpr (std::is_floating_point<T>::value)
27 {
28 maxValue = 1;
29 sampleType = "floating_point";
30 }
31 else if constexpr (std::numeric_limits<T>::is_integer)
32 {
33 if constexpr (std::is_signed_v<T>)
34 {
35 sampleType = "integer (signed, " + std::to_string (sizeof (T) * 8) + "-bit)";
36 maxValue = pow (2, bitDepth - 1) - 1;
37 }
38 else
39 {
40 sampleType = "integer (unsigned, " + std::to_string (sizeof (T) * 8) + "-bit)";
41 maxValue = pow (2, bitDepth) - 1;
42 }
43 }
44
45 for (int i = 0; i < audioFileWriter.getNumSamplesPerChannel(); i++)
46 {
47 T sample;
48
49 if constexpr (std::numeric_limits<T>::is_integer && std::is_unsigned_v<T>)
50 sample = static_cast<T> (((sin (2. * M_PI * (static_cast<double> (i) / static_cast<double> (sampleRateAsFloat)) * 440.) + 1.) / 2.) * maxValue);
51 else
52 sample = static_cast<T> (sin (2. * M_PI * (static_cast<double> (i) / static_cast<double> (sampleRateAsFloat)) * 440.) * static_cast<double> (maxValue));
53
54 for (int k = 0; k < audioFileWriter.getNumChannels(); k++)
55 audioFileWriter.samples[k][i] = sample * static_cast<T> (0.5);
56 }
57
58 audioFileWriter.setSampleRate (sampleRate);
59 audioFileWriter.setBitDepth (bitDepth);
60
61 std::string numChannelsAsString;
62 if (numChannels == 1)
63 numChannelsAsString = "mono";
64 else if (numChannels == 2)
65 numChannelsAsString = "stereo";
66 else
67 numChannelsAsString = std::to_string (numChannels) + " channels";
68
69 std::string bitDepthAsString = std::to_string (bitDepth);
70 std::string sampleRateAsString = std::to_string (sampleRate);
71

Callers

nothing calls this directly

Calls 10

ApproxFunction · 0.85
setAudioBufferSizeMethod · 0.80
getNumChannelsMethod · 0.80
setSampleRateMethod · 0.80
setBitDepthMethod · 0.80
saveMethod · 0.80
loadMethod · 0.80
getBitDepthMethod · 0.80
getSampleRateMethod · 0.80

Tested by

no test coverage detected