MCPcopy Create free account
hub / github.com/Tracktion/choc / main

Function main

examples/audio_operations.cpp:41–95  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

39}
40
41int main()
42{
43 const double sampleRate = 44100.0;
44 const double duration = 2.0;
45 const int numChannels = 1;
46 const int numFrames = static_cast<int> (sampleRate * duration);
47
48 // 1. Generate a sine wave
49 auto sineWaveBuffer = choc::oscillator::createChannelArraySine<float> ({ (choc::buffer::ChannelCount) numChannels, (choc::buffer::FrameCount) numFrames }, 440.0, sampleRate);
50 saveBufferToWAV ("sine_wave_original.wav", sineWaveBuffer);
51
52 // 2. Apply a simple gain
53 auto gainedBuffer = sineWaveBuffer;
54 choc::buffer::applyGain (gainedBuffer, 0.5f);
55 saveBufferToWAV ("sine_wave_gained.wav", gainedBuffer);
56
57 // 3. Mix another sine wave
58 auto mixedBuffer = gainedBuffer;
59 auto secondSineWave = choc::oscillator::createChannelArraySine<float> ({ (choc::buffer::ChannelCount) numChannels, (choc::buffer::FrameCount) numFrames }, 660.0, sampleRate);
60 choc::buffer::add (mixedBuffer, secondSineWave);
61 saveBufferToWAV ("sine_wave_mixed.wav", mixedBuffer);
62
63 // 4. Read the WAV file back (demonstrates reading)
64 choc::audio::AudioFileFormatList formatList;
65 formatList.addFormat<choc::audio::WAVAudioFileFormat<false>>();
66
67 choc::audio::AudioFileData audioFile;
68 try
69 {
70 audioFile = formatList.loadFileContent (std::make_shared<std::ifstream> ("sine_wave_mixed.wav", std::ios::binary | std::ios::in));
71 }
72 catch (const std::exception& e)
73 {
74 std::cerr << "Failed to load audio from sine_wave_mixed.wav: " << e.what() << std::endl;
75 return 1;
76 }
77
78 if (audioFile.frames.getNumFrames() == 0)
79 {
80 std::cerr << "Failed to load audio from sine_wave_mixed.wav" << std::endl;
81 return 1;
82 }
83
84 choc::buffer::ChannelArrayBuffer<float> loadedBuffer = audioFile.frames;
85 std::cout << "Successfully loaded sine_wave_mixed.wav" << std::endl;
86
87 // 5. Perform a simple pitch shift using sinc interpolation
88 const double pitchShiftRatio = 1.2; // Shift up by 20%
89 const int pitchShiftedFrames = static_cast<int> (loadedBuffer.getNumFrames() / pitchShiftRatio);
90 choc::buffer::ChannelArrayBuffer<float> pitchShiftedBuffer (loadedBuffer.getNumChannels(), pitchShiftedFrames);
91 choc::interpolation::sincInterpolate (pitchShiftedBuffer, loadedBuffer);
92 saveBufferToWAV ("sine_wave_pitch_shifted.wav", pitchShiftedBuffer);
93
94 return 0;
95}

Callers

nothing calls this directly

Calls 8

saveBufferToWAVFunction · 0.85
applyGainFunction · 0.85
addFunction · 0.85
sincInterpolateFunction · 0.85
loadFileContentMethod · 0.80
whatMethod · 0.80
getNumFramesMethod · 0.45
getNumChannelsMethod · 0.45

Tested by

no test coverage detected