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

Function writeSineWaveToAudioFile

examples/examples.cpp:35–71  ·  view source on GitHub ↗

=======================================================================

Source from the content-addressed store, hash-verified

33{
34 //=======================================================================
35 void writeSineWaveToAudioFile()
36 {
37 //---------------------------------------------------------------
38 std::cout << "**********************" << std::endl;
39 std::cout << "Running Example: Write Sine Wave To Audio File" << std::endl;
40 std::cout << "**********************" << std::endl << std::endl;
41
42 //---------------------------------------------------------------
43 // 1. Let's setup our AudioFile instance
44
45 AudioFile<float> a;
46 a.setNumChannels (2);
47 a.setNumSamplesPerChannel (44100);
48
49 //---------------------------------------------------------------
50 // 2. Create some variables to help us generate a sine wave
51
52 const float sampleRate = 44100.f;
53 const float frequencyInHz = 440.f;
54
55 //---------------------------------------------------------------
56 // 3. Write the samples to the AudioFile sample buffer
57
58 for (int i = 0; i < a.getNumSamplesPerChannel(); i++)
59 {
60 for (int channel = 0; channel < a.getNumChannels(); channel++)
61 {
62 a.samples[channel][i] = sin ((static_cast<float> (i) / sampleRate) * frequencyInHz * 2.f * (float)M_PI);
63 }
64 }
65
66 //---------------------------------------------------------------
67 // 4. Save the AudioFile
68
69 std::string filePath = "sine-wave.wav"; // change this to somewhere useful for you
70 a.save ("sine-wave.wav", AudioFileFormat::Wave);
71 }
72
73 //=======================================================================
74 void loadAudioFileAndPrintSummary()

Callers 1

mainFunction · 0.85

Calls 5

setNumChannelsMethod · 0.80
getNumChannelsMethod · 0.80
saveMethod · 0.80

Tested by

no test coverage detected