MainComponent::MainComponent() { // Make sure you set the size of the component after // you add any child components. setSize (800, 600);
// Some platforms require permissions to open input channels so request that here if (juce::RuntimePermissions::isRequired (juce::RuntimePermissions::recordAudio) && ! juce::RuntimePermissions::isGranted (juce::RuntimePermissions::recordAudio)) { juce::RuntimePermissions::request (juce::RuntimePermissions::recordAudio, [&] (bool granted) { setAudioChannels (granted ? 2 : 0, 2); }); } else { // Specify the number of input and output channels that we want to open setAudioChannels (0, 2); // no inputs, 2 outputs } } ``` 因为只是生成器,所以不需要有输入,且设定为两个输出(左右声道)。
// For more details, see the help for AudioProcessor::getNextAudioBlock()
// Right now we are not producing any data, in which case we need to clear the buffer // (to prevent the output of random noise) //bufferToFill.clearActiveBufferRegion();
for (auto channel = 0; channel < bufferToFill.buffer->getNumChannels(); ++channel) { // Get a pointer to the start sample in the buffer for this audio output channel auto* buffer = bufferToFill.buffer->getWritePointer(channel, bufferToFill.startSample);
// Fill the required number of samples with noise between -0.125 and +0.125 for (auto sample = 0; sample < bufferToFill.numSamples; ++sample) buffer[sample] = random.nextFloat() * 0.25f - 0.125f; } }
同时,引入juce的random类:MainComponent.h
1 2 3 4 5 6 7
private: //============================================================================== // Your private member variables go here... juce::Random random;