gpt4 book ai didi

c++ - 如何从 JUCE Demo Audio Plugin Host 访问音频数据?

转载 作者:行者123 更新时间:2023-11-30 03:24:05 25 4
gpt4 key购买 nike

我正在从事一个项目,该项目要求我从 JUCE 演示音频插件主机中加载的 MIDI 合成器插件将音频数据记录为 .wav 文件(每个文件 1 秒)。基本上,我需要从 MIDI Synth 自动创建一个数据集(对应不同的参数配置)。

我是否必须发送 MIDI 音符开/关消息才能生成音频数据?或者有没有更好的获取音频数据的方法?

AudioBuffer<FloatType> getBusBuffer (AudioBuffer<FloatType>& processBlockBuffer) const

这个功能能解决我的需求吗?如果是,我将如何存储数据?如果没有,请有人指导我找到正确的功能/解决方案。谢谢。

最佳答案

我不太确定你在问什么,所以我猜:

您需要以编程方式触发合成器中的一些 MIDI 音符,然后将所有音频写入 .wav 文件,对吗?

假设您已经了解 JUCE,制作一个打开您的插件、发送 MIDI 和录制音频的应用程序将是相当简单的,但调整 AudioPluginHost 可能会更容易。项目。

让我们把它分成几个简单的步骤(首先打开 AudioPluginHost 项目):

  1. 以编程方式发送 MIDI

GraphEditorPanel.h ,特别是类 GraphDocumentComponent .它有一个私有(private)成员变量:MidiKeyboardState keyState; .这会收集传入的 MIDI 消息,然后将它们插入到发送到插件的传入音频和 MIDI 缓冲区中。

您可以简单地调用keyState.noteOn (midiChannel, midiNoteNumber, velocity)keyState.noteOff (midiChannel, midiNoteNumber, velocity)触发一个音符。

  1. 录制音频输出

在 JUCE 中这是一件相当简单的事情——您应该从查看 JUCE 演示开始。以下示例在后台记录输出音频,但还有许多其他方法可以做到这一点:

class AudioRecorder  : public AudioIODeviceCallback
{
public:
AudioRecorder (AudioThumbnail& thumbnailToUpdate)
: thumbnail (thumbnailToUpdate)
{
backgroundThread.startThread();
}

~AudioRecorder()
{
stop();
}

//==============================================================================
void startRecording (const File& file)
{
stop();

if (sampleRate > 0)
{
// Create an OutputStream to write to our destination file...
file.deleteFile();
ScopedPointer<FileOutputStream> fileStream (file.createOutputStream());

if (fileStream.get() != nullptr)
{
// Now create a WAV writer object that writes to our output stream...
WavAudioFormat wavFormat;
auto* writer = wavFormat.createWriterFor (fileStream.get(), sampleRate, 1, 16, {}, 0);

if (writer != nullptr)
{
fileStream.release(); // (passes responsibility for deleting the stream to the writer object that is now using it)

// Now we'll create one of these helper objects which will act as a FIFO buffer, and will
// write the data to disk on our background thread.
threadedWriter.reset (new AudioFormatWriter::ThreadedWriter (writer, backgroundThread, 32768));

// Reset our recording thumbnail
thumbnail.reset (writer->getNumChannels(), writer->getSampleRate());
nextSampleNum = 0;

// And now, swap over our active writer pointer so that the audio callback will start using it..
const ScopedLock sl (writerLock);
activeWriter = threadedWriter.get();
}
}
}
}

void stop()
{
// First, clear this pointer to stop the audio callback from using our writer object..
{
const ScopedLock sl (writerLock);
activeWriter = nullptr;
}

// Now we can delete the writer object. It's done in this order because the deletion could
// take a little time while remaining data gets flushed to disk, so it's best to avoid blocking
// the audio callback while this happens.
threadedWriter.reset();
}

bool isRecording() const
{
return activeWriter != nullptr;
}

//==============================================================================
void audioDeviceAboutToStart (AudioIODevice* device) override
{
sampleRate = device->getCurrentSampleRate();
}

void audioDeviceStopped() override
{
sampleRate = 0;
}

void audioDeviceIOCallback (const float** inputChannelData, int numInputChannels,
float** outputChannelData, int numOutputChannels,
int numSamples) override
{
const ScopedLock sl (writerLock);

if (activeWriter != nullptr && numInputChannels >= thumbnail.getNumChannels())
{
activeWriter->write (inputChannelData, numSamples);

// Create an AudioBuffer to wrap our incoming data, note that this does no allocations or copies, it simply references our input data
AudioBuffer<float> buffer (const_cast<float**> (inputChannelData), thumbnail.getNumChannels(), numSamples);
thumbnail.addBlock (nextSampleNum, buffer, 0, numSamples);
nextSampleNum += numSamples;
}

// We need to clear the output buffers, in case they're full of junk..
for (int i = 0; i < numOutputChannels; ++i)
if (outputChannelData[i] != nullptr)
FloatVectorOperations::clear (outputChannelData[i], numSamples);
}

private:
AudioThumbnail& thumbnail;
TimeSliceThread backgroundThread { "Audio Recorder Thread" }; // the thread that will write our audio data to disk
ScopedPointer<AudioFormatWriter::ThreadedWriter> threadedWriter; // the FIFO used to buffer the incoming data
double sampleRate = 0.0;
int64 nextSampleNum = 0;

CriticalSection writerLock;
AudioFormatWriter::ThreadedWriter* volatile activeWriter = nullptr;
};

请注意,包含插件音频数据的实际音频回调发生在 AudioProcessorGraph 中。里面FilterGraph .在传入原始音频数据的地方每秒会发生多次音频回调。在 AudioPluginHost 中更改它可能会非常困惑。除非您知道自己在做什么——使用类似上述示例的东西或创建您自己的具有自己的音频流的应用程序可能会更简单。

您询问的功能:

AudioBuffer<FloatType> getBusBuffer (AudioBuffer<FloatType>& processBlockBuffer) const

无关紧要。一旦你已经在音频回调中,这将使你的音频被发送到你的插件的总线(如果你的合成器有侧链)。你想要做的是从回调中获取音频并将其传递给 AudioFormatWriter ,或者最好是 AudioFormatWriter::ThreadedWriter这样实际的写入发生在不同的线程上。

如果您对 C++ 或 JUCE 一点都不熟悉,Max/MSP 或 Pure Data 可能更容易让您快速做出一些东西。

关于c++ - 如何从 JUCE Demo Audio Plugin Host 访问音频数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49972215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com