如何从 JUCE Demo Audio Plugin Host 访问音频数据?
How to access Audio data from JUCE Demo Audio Plugin Host?
我正在做一个项目,该项目要求我从 JUCE 演示音频插件主机中加载的 MIDI 合成器插件将音频数据记录为 .wav 文件(每个文件 1 秒)。基本上,我需要从 MIDI Synth 自动创建一个数据集(对应不同的参数配置)
我是否必须发送 MIDI 音符 On/Off 消息才能生成音频数据?或者有没有更好的获取音频数据的方法?
AudioBuffer<FloatType> getBusBuffer (AudioBuffer<FloatType>& processBlockBuffer) const
这个功能能解决我的需求吗?如果是,我将如何存储数据?如果没有,请有人引导我向右function/solution。
谢谢。
我不太确定你在问什么,所以我猜:
您需要在合成器中以编程方式触发一些 MIDI 音符,然后将所有音频写入 .wav 文件,对吗?
假设您已经了解 JUCE,制作一个打开您的插件、发送 MIDI 和录制音频的应用程序将是相当简单的,但调整 AudioPluginHost
项目可能更容易。
让我们把它分成几个简单的步骤(首先打开 AudioPluginHost
项目):
- 以编程方式发送 MIDI
查看 GraphEditorPanel.h
,特别是 class GraphDocumentComponent
。它有一个私有成员变量:MidiKeyboardState keyState;
。这会收集传入的 MIDI 消息,然后将它们插入到发送到插件的传入音频和 MIDI 缓冲区中。
您只需调用 keyState.noteOn (midiChannel, midiNoteNumber, velocity)
和 keyState.noteOff (midiChannel, midiNoteNumber, velocity)
即可触发注释。
- 录制音频输出
在 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;
};
请注意,包含插件音频数据的实际音频回调发生在 FilterGraph
内的 AudioProcessorGraph
内。传入原始音频数据时,每秒会发生多次音频回调。除非您知道自己在做什么,否则在 AudioPluginHost
内更改它可能会非常混乱——使用某些东西可能会更简单就像上面的例子一样,或者创建你自己的应用程序,它有自己的音频流。
您询问的功能:
AudioBuffer<FloatType> getBusBuffer (AudioBuffer<FloatType>& processBlockBuffer) const
无关紧要。一旦你已经在音频回调中,这将使你的音频被发送到你的插件的总线(如果你的合成器有侧链)。您要做的是从回调中获取音频并将其传递给 AudioFormatWriter
,或者最好是 AudioFormatWriter::ThreadedWriter
,以便实际写入发生在不同的线程上。
如果您对 C++ 或 JUCE 一点都不熟悉,Max/MSP 或 Pure Data 可能更容易让您快速做出一些东西。
我正在做一个项目,该项目要求我从 JUCE 演示音频插件主机中加载的 MIDI 合成器插件将音频数据记录为 .wav 文件(每个文件 1 秒)。基本上,我需要从 MIDI Synth 自动创建一个数据集(对应不同的参数配置)
我是否必须发送 MIDI 音符 On/Off 消息才能生成音频数据?或者有没有更好的获取音频数据的方法?
AudioBuffer<FloatType> getBusBuffer (AudioBuffer<FloatType>& processBlockBuffer) const
这个功能能解决我的需求吗?如果是,我将如何存储数据?如果没有,请有人引导我向右function/solution。 谢谢。
我不太确定你在问什么,所以我猜:
您需要在合成器中以编程方式触发一些 MIDI 音符,然后将所有音频写入 .wav 文件,对吗?
假设您已经了解 JUCE,制作一个打开您的插件、发送 MIDI 和录制音频的应用程序将是相当简单的,但调整 AudioPluginHost
项目可能更容易。
让我们把它分成几个简单的步骤(首先打开 AudioPluginHost
项目):
- 以编程方式发送 MIDI
查看 GraphEditorPanel.h
,特别是 class GraphDocumentComponent
。它有一个私有成员变量:MidiKeyboardState keyState;
。这会收集传入的 MIDI 消息,然后将它们插入到发送到插件的传入音频和 MIDI 缓冲区中。
您只需调用 keyState.noteOn (midiChannel, midiNoteNumber, velocity)
和 keyState.noteOff (midiChannel, midiNoteNumber, velocity)
即可触发注释。
- 录制音频输出
在 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;
};
请注意,包含插件音频数据的实际音频回调发生在 FilterGraph
内的 AudioProcessorGraph
内。传入原始音频数据时,每秒会发生多次音频回调。除非您知道自己在做什么,否则在 AudioPluginHost
内更改它可能会非常混乱——使用某些东西可能会更简单就像上面的例子一样,或者创建你自己的应用程序,它有自己的音频流。
您询问的功能:
AudioBuffer<FloatType> getBusBuffer (AudioBuffer<FloatType>& processBlockBuffer) const
无关紧要。一旦你已经在音频回调中,这将使你的音频被发送到你的插件的总线(如果你的合成器有侧链)。您要做的是从回调中获取音频并将其传递给 AudioFormatWriter
,或者最好是 AudioFormatWriter::ThreadedWriter
,以便实际写入发生在不同的线程上。
如果您对 C++ 或 JUCE 一点都不熟悉,Max/MSP 或 Pure Data 可能更容易让您快速做出一些东西。