AudioBufferSample Change type at execution


#1

Hello Everyone,

I’m trying to change the Plugin Host Project to process midiFile.
When i try to process My MidiBuffer i get an exception.

void MainHostWindow::ProcessMidiThroughAudioSampleBuffer(MidiBuffer* midiBuffers) {

  float numSamplesToAllocate = midiBuffers->getLastEventTime() + 1.0;
  AudioSampleBuffer audio(2, numSamplesToAllocate);

  AudioProcessorGraph& graph = this->getGraphEditor()->graph.getGraph();
  graph.setRateAndBufferSizeDetails(this->sampleRate, numSamplesToAllocate);
  graph.prepareToPlay(this->sampleRate, numSamplesToAllocate);
  graph.setNonRealtime(true);
  graph.processBlock(audio, *midiBuffers);

During the process the function processAudio is called. And the variable type can be audioOutputNode or an audioInputNode, it has like 50% for each to be.
So i assume i’ve got something wrong somewhere. but i can’t find it. could you help me ?

template <typename FloatType>
void AudioProcessorGraph::AudioGraphIOProcessor::processAudio (AudioBuffer<FloatType>& buffer,
                                                           MidiBuffer& midiMessages)
{
    AudioBuffer<FloatType>*& currentAudioInputBuffer =
        graph->audioBuffers->currentAudioInputBuffer.get<FloatType>();

    AudioBuffer<FloatType>&  currentAudioOutputBuffer =
        graph->audioBuffers->currentAudioOutputBuffer.get<FloatType>();

    jassert (graph != nullptr);

    switch (type)
    {
        case audioOutputNode:
        {
            for (int i = jmin (currentAudioOutputBuffer.getNumChannels(),
                               buffer.getNumChannels()); --i >= 0;)
            {
                currentAudioOutputBuffer.addFrom (i, 0, buffer, i, 0, buffer.getNumSamples());
            }

            break;
        }

        case audioInputNode:
        {
            for (int i = jmin (currentAudioInputBuffer->getNumChannels(),
                               buffer.getNumChannels()); --i >= 0;)
            {
                buffer.copyFrom (i, 0, *currentAudioInputBuffer, i, 0, buffer.getNumSamples());
            }

            break;
        }

        case midiOutputNode:
            graph->currentMidiOutputBuffer.addEvents (midiMessages, 0, buffer.getNumSamples(), 0);
            break;

        case midiInputNode:
            midiMessages.addEvents (*graph->currentMidiInputBuffer, 0, buffer.getNumSamples(), 0);
            break;

        default:
            break;
    }
}

#2

What exception are you getting and where?


#3

EDIT: I Added the Stack Trace for each assert.

When it goes to copyFrom Function
JUCE Assertion failure in juce_AudioSampleBuffer.h:778

 void copyFrom (int destChannel,
               int destStartSample,
               const AudioBuffer& source,
               int sourceChannel,
               int sourceStartSample,
               int numSamples) noexcept
{
    jassert (&source != this || sourceChannel != destChannel);
    jassert (isPositiveAndBelow (destChannel, numChannels));
    jassert (destStartSample >= 0 && destStartSample + numSamples <= size);
    jassert (isPositiveAndBelow (sourceChannel, source.numChannels));
    jassert (sourceStartSample >= 0 && sourceStartSample + numSamples <= source.size); // Juce Assert Error

There is the Stack.

Thread 1 Juce Message Thread, Queue : com.apple.main-thread
#0	0x00000001000473d1 in juce::AudioBuffer<float>::copyFrom(int, int, juce::AudioBuffer<float> const&, int, int, int) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_audio_basics/buffers/juce_AudioSampleBuffer.h:778
#1	0x00000001000ffa0b in void juce::AudioProcessorGraph::AudioGraphIOProcessor::processAudio<float>(juce::AudioBuffer<float>&, juce::MidiBuffer&) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_audio_processors/processors/juce_AudioProcessorGraph.cpp:1581
#2	0x00000001000c8d95 in juce::AudioProcessorGraph::AudioGraphIOProcessor::processBlock(juce::AudioBuffer<float>&, juce::MidiBuffer&) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_audio_processors/processors/juce_AudioProcessorGraph.cpp:1603
#3	0x000000010017f5d6 in juce::GraphRenderingOps::ProcessBufferOp::callProcess(juce::AudioBuffer<float>&, juce::MidiBuffer&) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_audio_processors/processors/juce_AudioProcessorGraph.cpp:272
#4	0x000000010017f527 in void juce::GraphRenderingOps::ProcessBufferOp::perform<float>(juce::AudioBuffer<float>&, juce::OwnedArray<juce::MidiBuffer, juce::DummyCriticalSection> const&, int) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_audio_processors/processors/juce_AudioProcessorGraph.cpp:267
#5	0x000000010017efa6 in juce::GraphRenderingOps::AudioGraphRenderingOp<juce::GraphRenderingOps::ProcessBufferOp>::perform(juce::AudioBuffer<float>&, juce::OwnedArray<juce::MidiBuffer, juce::DummyCriticalSection> const&, int) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_audio_processors/processors/juce_AudioProcessorGraph.cpp:85
#6	0x00000001000fcb6b in void juce::AudioProcessorGraph::processAudio<float>(juce::AudioBuffer<float>&, juce::MidiBuffer&) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_audio_processors/processors/juce_AudioProcessorGraph.cpp:1462
#7	0x00000001000c8835 in juce::AudioProcessorGraph::processBlock(juce::AudioBuffer<float>&, juce::MidiBuffer&) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_audio_processors/processors/juce_AudioProcessorGraph.cpp:1480
#8	0x000000010001998c in MainHostWindow::ProcessMidiThroughAudioSampleBuffer(juce::MidiBuffer*) at /Users/meludia/PluginHostDemo/Source/MainHostWindow.cpp:654
#9	0x000000010001832b in MainHostWindow::process() at /Users/meludia/PluginHostDemo/Source/MainHostWindow.cpp:673
#10	0x0000000100017af7 in MainHostWindow::perform(juce::ApplicationCommandTarget::InvocationInfo const&) at /Users/meludia/PluginHostDemo/Source/MainHostWindow.cpp:453
#11	0x00000001000187cf in non-virtual thunk to MainHostWindow::perform(juce::ApplicationCommandTarget::InvocationInfo const&) at /Users/meludia/PluginHostDemo/Source/MainHostWindow.cpp:497
#12	0x000000010046e7e8 in juce::ApplicationCommandTarget::tryToInvoke(juce::ApplicationCommandTarget::InvocationInfo const&, bool) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_gui_basics/commands/juce_ApplicationCommandTarget.cpp:67
#13	0x00000001004f65c2 in juce::ApplicationCommandTarget::CommandMessage::messageCallback() at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_gui_basics/commands/juce_ApplicationCommandTarget.cpp:36
#14	0x00000001002a5f8c in juce::MessageQueue::deliverNextMessage() at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_events/native/juce_osx_MessageQueue.h:84
#15	0x00000001002a5eda in juce::MessageQueue::runLoopCallback() at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_events/native/juce_osx_MessageQueue.h:95
#16	0x00000001002a5e95 in juce::MessageQueue::runLoopSourceCallback(void*) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_events/native/juce_osx_MessageQueue.h:103
#17	0x00007fff87942a01 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
#18	0x00007fff87934b8d in __CFRunLoopDoSources0 ()
#19	0x00007fff879341bf in __CFRunLoopRun ()
#20	0x00007fff87933bd8 in CFRunLoopRunSpecific ()
#21	0x00007fff82c5d3dd in _NSUnhighlightCarbonMenu ()
#22	0x00007fff82c55986 in -[NSCarbonMenuImpl performActionWithHighlightingForItemAtIndex:] ()
#23	0x00007fff91a0fcd7 in _os_activity_initiate ()
#24	0x00007fff82d1cb00 in -[NSMenu performActionForItemAtIndex:] ()
#25	0x00007fff82d1ca66 in -[NSMenu _internalPerformActionForItemAtIndex:] ()
#26	0x00007fff82d1c8b2 in -[NSCarbonMenuImpl _carbonCommandProcessEvent:handlerCallRef:] ()
#27	0x00007fff82c3dd6b in NSSLMMenuEventHandler ()
#28	0x00007fff874f7b6c in DispatchEventToHandlers(EventTargetRec*, OpaqueEventRef*, HandlerCallRec*) ()
#29	0x00007fff874f6fae in SendEventToEventTargetInternal(OpaqueEventRef*, OpaqueEventTargetRef*, HandlerCallRec*) ()
#30	0x00007fff8750ccb6 in SendEventToEventTarget ()
#31	0x00007fff87546f45 in SendHICommandEvent(unsigned int, HICommand const*, unsigned int, unsigned int, unsigned char, void const*, OpaqueEventTargetRef*, OpaqueEventTargetRef*, OpaqueEventRef**) ()
#32	0x00007fff87584b8d in SendMenuCommandWithContextAndModifiers ()
#33	0x00007fff87584b30 in SendMenuItemSelectedEvent ()
#34	0x00007fff87584a09 in FinishMenuSelection(SelectionData*, MenuResult*, MenuResult*) ()
#35	0x00007fff87585481 in MenuSelectCore(MenuData*, Point, double, unsigned int, OpaqueMenuRef**, unsigned short*) ()
#36	0x00007fff875850fe in _HandleMenuSelection2 ()
#37	0x00007fff82b5bce0 in _NSHandleCarbonMenuEvent ()
#38	0x00007fff82a92bfd in _DPSNextEvent ()
#39	0x00007fff82a91e58 in -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] ()
#40	0x00007fff82a87af3 in -[NSApplication run] ()
#41	0x0000000100295cb0 in juce::MessageManager::runDispatchLoop() at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_events/native/juce_mac_MessageManager.mm:248
#42	0x0000000100295b7a in juce::JUCEApplicationBase::main() at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_events/messages/juce_ApplicationBase.cpp:240
#43	0x000000010029598c in juce::JUCEApplicationBase::main(int, char const**) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_events/messages/juce_ApplicationBase.cpp:218
#44	0x0000000100011f13 in main at /Users/meludia/PluginHostDemo/Source/HostStartup.cpp:133

When it goes to addFrom
JUCE Assertion failure in juce_AudioSampleBuffer.h:639

void addFrom (int destChannel,
              int destStartSample,
              const AudioBuffer& source,
              int sourceChannel,
              int sourceStartSample,
              int numSamples,
              Type gainToApplyToSource = (Type) 1) noexcept
{
    jassert (&source != this || sourceChannel != destChannel);
    jassert (isPositiveAndBelow (destChannel, numChannels));
    jassert (destStartSample >= 0 && destStartSample + numSamples <= size); // Juce Assert Error

The Stack trace of this assert:

Thread 1 Juce Message Thread, Queue : com.apple.main-thread
#0	0x0000000100047c6e in juce::AudioBuffer<float>::addFrom(int, int, juce::AudioBuffer<float> const&, int, int, int, float) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_audio_basics/buffers/juce_AudioSampleBuffer.h:639
#1	0x00000001000ff960 in void juce::AudioProcessorGraph::AudioGraphIOProcessor::processAudio<float>(juce::AudioBuffer<float>&, juce::MidiBuffer&) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_audio_processors/processors/juce_AudioProcessorGraph.cpp:1570
#2	0x00000001000c8d95 in juce::AudioProcessorGraph::AudioGraphIOProcessor::processBlock(juce::AudioBuffer<float>&, juce::MidiBuffer&) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_audio_processors/processors/juce_AudioProcessorGraph.cpp:1603
#3	0x000000010017f5d6 in juce::GraphRenderingOps::ProcessBufferOp::callProcess(juce::AudioBuffer<float>&, juce::MidiBuffer&) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_audio_processors/processors/juce_AudioProcessorGraph.cpp:272
#4	0x000000010017f527 in void juce::GraphRenderingOps::ProcessBufferOp::perform<float>(juce::AudioBuffer<float>&, juce::OwnedArray<juce::MidiBuffer, juce::DummyCriticalSection> const&, int) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_audio_processors/processors/juce_AudioProcessorGraph.cpp:267
#5	0x000000010017efa6 in juce::GraphRenderingOps::AudioGraphRenderingOp<juce::GraphRenderingOps::ProcessBufferOp>::perform(juce::AudioBuffer<float>&, juce::OwnedArray<juce::MidiBuffer, juce::DummyCriticalSection> const&, int) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_audio_processors/processors/juce_AudioProcessorGraph.cpp:85
#6	0x00000001000fcb6b in void juce::AudioProcessorGraph::processAudio<float>(juce::AudioBuffer<float>&, juce::MidiBuffer&) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_audio_processors/processors/juce_AudioProcessorGraph.cpp:1462
#7	0x00000001000c8835 in juce::AudioProcessorGraph::processBlock(juce::AudioBuffer<float>&, juce::MidiBuffer&) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_audio_processors/processors/juce_AudioProcessorGraph.cpp:1480
#8	0x000000010001998c in MainHostWindow::ProcessMidiThroughAudioSampleBuffer(juce::MidiBuffer*) at /Users/meludia/PluginHostDemo/Source/MainHostWindow.cpp:654
#9	0x000000010001832b in MainHostWindow::process() at /Users/meludia/PluginHostDemo/Source/MainHostWindow.cpp:673
#10	0x0000000100017af7 in MainHostWindow::perform(juce::ApplicationCommandTarget::InvocationInfo const&) at /Users/meludia/PluginHostDemo/Source/MainHostWindow.cpp:453
#11	0x00000001000187cf in non-virtual thunk to MainHostWindow::perform(juce::ApplicationCommandTarget::InvocationInfo const&) at /Users/meludia/PluginHostDemo/Source/MainHostWindow.cpp:497
#12	0x000000010046e7e8 in juce::ApplicationCommandTarget::tryToInvoke(juce::ApplicationCommandTarget::InvocationInfo const&, bool) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_gui_basics/commands/juce_ApplicationCommandTarget.cpp:67
#13	0x00000001004f65c2 in juce::ApplicationCommandTarget::CommandMessage::messageCallback() at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_gui_basics/commands/juce_ApplicationCommandTarget.cpp:36
#14	0x00000001002a5f8c in juce::MessageQueue::deliverNextMessage() at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_events/native/juce_osx_MessageQueue.h:84
#15	0x00000001002a5eda in juce::MessageQueue::runLoopCallback() at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_events/native/juce_osx_MessageQueue.h:95
#16	0x00000001002a5e95 in juce::MessageQueue::runLoopSourceCallback(void*) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_events/native/juce_osx_MessageQueue.h:103
#17	0x00007fff87942a01 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
#18	0x00007fff87934b8d in __CFRunLoopDoSources0 ()
#19	0x00007fff879341bf in __CFRunLoopRun ()
#20	0x00007fff87933bd8 in CFRunLoopRunSpecific ()
#21	0x00007fff82c5d3dd in _NSUnhighlightCarbonMenu ()
#22	0x00007fff82c55986 in -[NSCarbonMenuImpl performActionWithHighlightingForItemAtIndex:] ()
#23	0x00007fff91a0fcd7 in _os_activity_initiate ()
#24	0x00007fff82d1cb00 in -[NSMenu performActionForItemAtIndex:] ()
#25	0x00007fff82d1ca66 in -[NSMenu _internalPerformActionForItemAtIndex:] ()
#26	0x00007fff82d1c8b2 in -[NSCarbonMenuImpl _carbonCommandProcessEvent:handlerCallRef:] ()
#27	0x00007fff82c3dd6b in NSSLMMenuEventHandler ()
#28	0x00007fff874f7b6c in DispatchEventToHandlers(EventTargetRec*, OpaqueEventRef*, HandlerCallRec*) ()
#29	0x00007fff874f6fae in SendEventToEventTargetInternal(OpaqueEventRef*, OpaqueEventTargetRef*, HandlerCallRec*) ()
#30	0x00007fff8750ccb6 in SendEventToEventTarget ()
#31	0x00007fff87546f45 in SendHICommandEvent(unsigned int, HICommand const*, unsigned int, unsigned int, unsigned char, void const*, OpaqueEventTargetRef*, OpaqueEventTargetRef*, OpaqueEventRef**) ()
#32	0x00007fff87584b8d in SendMenuCommandWithContextAndModifiers ()
#33	0x00007fff87584b30 in SendMenuItemSelectedEvent ()
#34	0x00007fff87584a09 in FinishMenuSelection(SelectionData*, MenuResult*, MenuResult*) ()
#35	0x00007fff87585481 in MenuSelectCore(MenuData*, Point, double, unsigned int, OpaqueMenuRef**, unsigned short*) ()
#36	0x00007fff875850fe in _HandleMenuSelection2 ()
#37	0x00007fff82b5bce0 in _NSHandleCarbonMenuEvent ()
#38	0x00007fff82a92bfd in _DPSNextEvent ()
#39	0x00007fff82a91e58 in -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] ()
#40	0x00007fff82a87af3 in -[NSApplication run] ()
#41	0x0000000100295cb0 in juce::MessageManager::runDispatchLoop() at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_events/native/juce_mac_MessageManager.mm:248
#42	0x0000000100295b7a in juce::JUCEApplicationBase::main() at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_events/messages/juce_ApplicationBase.cpp:240
#43	0x000000010029598c in juce::JUCEApplicationBase::main(int, char const**) at /Users/meludia/PluginHostDemo/Builds/MacOSX/../../../JUCE/modules/juce_events/messages/juce_ApplicationBase.cpp:218
#44	0x0000000100011f13 in main at /Users/meludia/PluginHostDemo/Source/HostStartup.cpp:133

#4

A stack-trace would be nice as well.


#5

I edited the post to include the stack.


#6

Hi, I added the stack trace. Does anynone have an idea about what is going on ?


#7

Seems like you are calling processAudio from the message thread when the user invokes a menu item. Normally the audioGraph would run on the audio callback and you must never call processBlock from the message thread at the same time.


#8

Yes i’m doing this when the user click on an item menu.

mmmh, Wait where and how am i supposed to process my midi ? I don’t get it.


#9

If the audio graph is hooked up to the audio device manager then the processBlocks will be called automatically on the audio thread. You will need to create your own little AudioProcessor with only a midi output and inject your midi via this processor.


#10

I assume you could also temporarily disable the binding to the audio thread when you process offline. That should avoid any conflict to rise here, shouldn’t it @fabian?


#11

Yes, you could use the CallbackLock on the AudioProcessorGraph.