如何从JUCE演示音频插件主机访问音频数据?

时间:2018-04-23 01:17:33

标签: c++ audio plugins juce

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

我是否必须发送MIDI Note On / Off消息才能生成音频数据?或者有更好的方法来获取音频数据吗?

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

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

1 个答案:

答案 0 :(得分:2)

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

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

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

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

  1. 以编程方式发送MIDI
  2. 查看GraphEditorPanel.h,特别是班级GraphDocumentComponent。它有一个私有成员变量:MidiKeyboardState keyState;。这会收集传入的MIDI信息,然后将它们插入到传入的音频和信号中。发送到插件的MIDI缓冲区。

    您只需致电keyState.noteOn (midiChannel, midiNoteNumber, velocity)keyState.noteOff (midiChannel, midiNoteNumber, velocity)即可触发记事。

    1. 录制音频输出
    2. 在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可能更容易让您快速提升。