Skip to content

Commit

Permalink
Merge pull request #804 from diizy/midioffset
Browse files Browse the repository at this point in the history
Make MIDI timing sample-accurate
  • Loading branch information
tobydox committed Jun 4, 2014
2 parents 96da871 + 06be5bb commit ab493ea
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 32 deletions.
6 changes: 3 additions & 3 deletions include/Instrument.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
*
*/

#ifndef _INSTRUMENT_H
#define _INSTRUMENT_H
#ifndef INSTRUMENT_H
#define INSTRUMENT_H

#include <QtGui/QWidget>

Expand Down Expand Up @@ -101,7 +101,7 @@ class EXPORT Instrument : public Plugin

// sub-classes can re-implement this for receiving all incoming
// MIDI-events
inline virtual bool handleMidiEvent( const MidiEvent&, const MidiTime& = MidiTime() )
inline virtual bool handleMidiEvent( const MidiEvent&, const MidiTime& = MidiTime(), f_cnt_t offset = 0 )
{
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions include/InstrumentTrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
*
*/

#ifndef _INSTRUMENT_TRACK_H
#define _INSTRUMENT_TRACK_H
#ifndef INSTRUMENT_TRACK_H
#define INSTRUMENT_TRACK_H

#include "AudioPort.h"
#include "InstrumentFunctions.h"
Expand Down Expand Up @@ -71,8 +71,8 @@ class EXPORT InstrumentTrack : public track, public MidiEventProcessor

MidiEvent applyMasterKey( const MidiEvent& event );

virtual void processInEvent( const MidiEvent& event, const MidiTime& time = MidiTime() );
virtual void processOutEvent( const MidiEvent& event, const MidiTime& time = MidiTime() );
virtual void processInEvent( const MidiEvent& event, const MidiTime& time = MidiTime(), f_cnt_t offset = 0 );
virtual void processOutEvent( const MidiEvent& event, const MidiTime& time = MidiTime(), f_cnt_t offset = 0 );
// silence all running notes played by this track
void silenceAllNotes();

Expand Down
8 changes: 4 additions & 4 deletions include/MidiController.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
*
*/

#ifndef _MIDI_CONTROLLER_H
#define _MIDI_CONTROLLER_H
#ifndef MIDI_CONTROLLER_H
#define MIDI_CONTROLLER_H

#include <QtGui/QWidget>

Expand All @@ -44,10 +44,10 @@ class MidiController : public Controller, public MidiEventProcessor
virtual ~MidiController();

virtual void processInEvent( const MidiEvent & _me,
const MidiTime & _time );
const MidiTime & _time, f_cnt_t offset = 0 );

virtual void processOutEvent( const MidiEvent& _me,
const MidiTime & _time)
const MidiTime & _time, f_cnt_t offset = 0 )
{
// No output yet
}
Expand Down
8 changes: 4 additions & 4 deletions include/MidiEventProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
*
*/

#ifndef _MIDI_EVENT_PROCESSOR_H
#define _MIDI_EVENT_PROCESSOR_H
#ifndef MIDI_EVENT_PROCESSOR_H
#define MIDI_EVENT_PROCESSOR_H

#include "MidiEvent.h"
#include "MidiTime.h"
Expand All @@ -42,8 +42,8 @@ class MidiEventProcessor
}

// to be implemented by inheriting classes
virtual void processInEvent( const MidiEvent& event, const MidiTime& time = MidiTime() ) = 0;
virtual void processOutEvent( const MidiEvent& event, const MidiTime& time = MidiTime() ) = 0;
virtual void processInEvent( const MidiEvent& event, const MidiTime& time = MidiTime(), f_cnt_t offset = 0 ) = 0;
virtual void processOutEvent( const MidiEvent& event, const MidiTime& time = MidiTime(), f_cnt_t offset = 0 ) = 0;

} ;

Expand Down
2 changes: 1 addition & 1 deletion plugins/opl2/opl2instrument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ int opl2instrument::pushVoice(int v) {
return i;
}

bool opl2instrument::handleMidiEvent( const MidiEvent& event, const MidiTime& time )
bool opl2instrument::handleMidiEvent( const MidiEvent& event, const MidiTime& time, f_cnt_t offset )
{
emulatorMutex.lock();
int key, vel, voice, tmp_pb;
Expand Down
2 changes: 1 addition & 1 deletion plugins/opl2/opl2instrument.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class opl2instrument : public Instrument
return IsSingleStreamed | IsMidiBased;
}

virtual bool handleMidiEvent( const MidiEvent& event, const MidiTime& time );
virtual bool handleMidiEvent( const MidiEvent& event, const MidiTime& time, f_cnt_t offset = 0 );
virtual void play( sampleFrame * _working_buffer );

void saveSettings( QDomDocument & _doc, QDomElement & _this );
Expand Down
4 changes: 2 additions & 2 deletions plugins/vestige/vestige.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,12 @@ void vestigeInstrument::play( sampleFrame * _buf )



bool vestigeInstrument::handleMidiEvent( const MidiEvent& event, const MidiTime& time )
bool vestigeInstrument::handleMidiEvent( const MidiEvent& event, const MidiTime& time, f_cnt_t offset )
{
m_pluginMutex.lock();
if( m_plugin != NULL )
{
m_plugin->processMidiEvent( event, time );
m_plugin->processMidiEvent( event, offset );
}
m_pluginMutex.unlock();

Expand Down
2 changes: 1 addition & 1 deletion plugins/vestige/vestige.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class vestigeInstrument : public Instrument
return IsSingleStreamed | IsMidiBased;
}

virtual bool handleMidiEvent( const MidiEvent& event, const MidiTime& time );
virtual bool handleMidiEvent( const MidiEvent& event, const MidiTime& time, f_cnt_t offset = 0 );

virtual PluginView * instantiateView( QWidget * _parent );

Expand Down
2 changes: 1 addition & 1 deletion plugins/zynaddsubfx/ZynAddSubFx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ void ZynAddSubFxInstrument::play( sampleFrame * _buf )



bool ZynAddSubFxInstrument::handleMidiEvent( const MidiEvent& event, const MidiTime& time )
bool ZynAddSubFxInstrument::handleMidiEvent( const MidiEvent& event, const MidiTime& time, f_cnt_t offset )
{
// do not forward external MIDI Control Change events if the according
// LED is not checked
Expand Down
2 changes: 1 addition & 1 deletion plugins/zynaddsubfx/ZynAddSubFx.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ZynAddSubFxInstrument : public Instrument

virtual void play( sampleFrame * _working_buffer );

virtual bool handleMidiEvent( const MidiEvent& event, const MidiTime& time = MidiTime() );
virtual bool handleMidiEvent( const MidiEvent& event, const MidiTime& time = MidiTime(), f_cnt_t offset = 0 );

virtual void saveSettings( QDomDocument & _doc, QDomElement & _parent );
virtual void loadSettings( const QDomElement & _this );
Expand Down
6 changes: 4 additions & 2 deletions src/core/NotePlayHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ NotePlayHandle::NotePlayHandle( InstrumentTrack* instrumentTrack,
// send MidiNoteOn event
m_instrumentTrack->processOutEvent(
MidiEvent( MidiNoteOn, midiChannel(), midiKey(), midiVelocity( baseVelocity ) ),
MidiTime::fromFrames( offset(), engine::framesPerTick() ) );
MidiTime::fromFrames( offset(), engine::framesPerTick() ),
offset() );
}
}

Expand Down Expand Up @@ -336,7 +337,8 @@ void NotePlayHandle::noteOff( const f_cnt_t _s )
// send MidiNoteOff event
m_instrumentTrack->processOutEvent(
MidiEvent( MidiNoteOff, midiChannel(), midiKey(), 0 ),
MidiTime::fromFrames( m_framesBeforeRelease, engine::framesPerTick() ) );
MidiTime::fromFrames( m_framesBeforeRelease, engine::framesPerTick() ),
_s );
}

// inform attached components about MIDI finished (used for recording in Piano Roll)
Expand Down
2 changes: 1 addition & 1 deletion src/core/midi/MidiController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void MidiController::updateName()



void MidiController::processInEvent( const MidiEvent& event, const MidiTime& time )
void MidiController::processInEvent( const MidiEvent& event, const MidiTime& time, f_cnt_t offset )
{
unsigned char controllerNum;
switch( event.type() )
Expand Down
14 changes: 7 additions & 7 deletions src/tracks/InstrumentTrack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ MidiEvent InstrumentTrack::applyMasterKey( const MidiEvent& event )



void InstrumentTrack::processInEvent( const MidiEvent& event, const MidiTime& time )
void InstrumentTrack::processInEvent( const MidiEvent& event, const MidiTime& time, f_cnt_t offset )
{
engine::mixer()->lock();

Expand Down Expand Up @@ -335,7 +335,7 @@ void InstrumentTrack::processInEvent( const MidiEvent& event, const MidiTime& ti
break;
}

if( eventHandled == false && instrument()->handleMidiEvent( event, time ) == false )
if( eventHandled == false && instrument()->handleMidiEvent( event, time, offset ) == false )
{
qWarning( "InstrumentTrack: unhandled MIDI event %d", event.type() );
}
Expand All @@ -346,7 +346,7 @@ void InstrumentTrack::processInEvent( const MidiEvent& event, const MidiTime& ti



void InstrumentTrack::processOutEvent( const MidiEvent& event, const MidiTime& time )
void InstrumentTrack::processOutEvent( const MidiEvent& event, const MidiTime& time, f_cnt_t offset )
{
// do nothing if we do not have an instrument instance (e.g. when loading settings)
if( m_instrument == NULL )
Expand All @@ -366,10 +366,10 @@ void InstrumentTrack::processOutEvent( const MidiEvent& event, const MidiTime& t
{
if( m_runningMidiNotes[key] > 0 )
{
m_instrument->handleMidiEvent( MidiEvent( MidiNoteOff, midiPort()->realOutputChannel(), key, 0 ), time );
m_instrument->handleMidiEvent( MidiEvent( MidiNoteOff, midiPort()->realOutputChannel(), key, 0 ), time, offset );
}
++m_runningMidiNotes[key];
m_instrument->handleMidiEvent( MidiEvent( MidiNoteOn, midiPort()->realOutputChannel(), key, event.velocity() ), time );
m_instrument->handleMidiEvent( MidiEvent( MidiNoteOn, midiPort()->realOutputChannel(), key, event.velocity() ), time, offset );

emit newNote();
}
Expand All @@ -381,12 +381,12 @@ void InstrumentTrack::processOutEvent( const MidiEvent& event, const MidiTime& t
if( key >= 0 && key < NumKeys && --m_runningMidiNotes[key] <= 0 )
{
m_runningMidiNotes[key] = qMax( 0, m_runningMidiNotes[key] );
m_instrument->handleMidiEvent( MidiEvent( MidiNoteOff, midiPort()->realOutputChannel(), key, 0 ), time );
m_instrument->handleMidiEvent( MidiEvent( MidiNoteOff, midiPort()->realOutputChannel(), key, 0 ), time, offset );
}
break;

default:
m_instrument->handleMidiEvent( transposedEvent, time );
m_instrument->handleMidiEvent( transposedEvent, time, offset );
break;
}

Expand Down

0 comments on commit ab493ea

Please sign in to comment.