MuseScore/mscore/seq.cpp

1596 lines
54 KiB
C++
Raw Normal View History

2012-05-26 14:49:10 +02:00
//=============================================================================
// MuseScore
// Linux Music Score Editor
// $Id: seq.cpp 5660 2012-05-22 14:17:39Z wschweer $
//
// Copyright (C) 2002-2011 Werner Schweer and others
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================
#include "config.h"
#include "seq.h"
#include "musescore.h"
#include "synthesizer/msynthesizer.h"
2012-05-26 14:49:10 +02:00
#include "libmscore/slur.h"
2013-08-22 12:18:14 +02:00
#include "libmscore/tie.h"
2012-05-26 14:49:10 +02:00
#include "libmscore/score.h"
#include "libmscore/segment.h"
#include "libmscore/note.h"
#include "libmscore/chord.h"
#include "libmscore/tempo.h"
#include "scoreview.h"
#include "playpanel.h"
#include "libmscore/staff.h"
#include "libmscore/measure.h"
#include "preferences.h"
#include "libmscore/part.h"
#include "libmscore/ottava.h"
#include "libmscore/utils.h"
#include "libmscore/repeatlist.h"
#include "libmscore/audio.h"
#include "synthcontrol.h"
#include "pianoroll.h"
#include "pianotools.h"
2012-05-26 14:49:10 +02:00
#include "click.h"
#include <vorbis/vorbisfile.h>
2013-05-13 18:49:17 +02:00
namespace Ms {
2012-05-26 14:49:10 +02:00
Seq* seq;
static const int guiRefresh = 10; // Hz
static const int peakHoldTime = 1400; // msec
static const int peakHold = (peakHoldTime * guiRefresh) / 1000;
static OggVorbis_File vf;
2014-06-17 14:35:16 +02:00
#if 0 // yet(?) unused
2012-05-26 14:49:10 +02:00
static const int AUDIO_BUFFER_SIZE = 1024 * 512; // 2 MB
static const int MIN_CLICKS = 3; // the minimum number of 'clicks' in a count-in
2014-06-17 14:35:16 +02:00
#endif
2012-05-26 14:49:10 +02:00
//---------------------------------------------------------
// VorbisData
//---------------------------------------------------------
struct VorbisData {
int pos; // current position in audio->data()
QByteArray data;
};
static VorbisData vorbisData;
static size_t ovRead(void* ptr, size_t size, size_t nmemb, void* datasource);
static int ovSeek(void* datasource, ogg_int64_t offset, int whence);
static long ovTell(void* datasource);
static ov_callbacks ovCallbacks = {
ovRead, ovSeek, 0, ovTell
};
//---------------------------------------------------------
// ovRead
//---------------------------------------------------------
static size_t ovRead(void* ptr, size_t size, size_t nmemb, void* datasource)
{
VorbisData* vd = (VorbisData*)datasource;
size_t n = size * nmemb;
if (vd->data.size() < int(vd->pos + n))
n = vd->data.size() - vd->pos;
if (n) {
const char* src = vd->data.data() + vd->pos;
memcpy(ptr, src, n);
vd->pos += n;
}
return n;
}
//---------------------------------------------------------
// ovSeek
//---------------------------------------------------------
static int ovSeek(void* datasource, ogg_int64_t offset, int whence)
{
VorbisData* vd = (VorbisData*)datasource;
switch(whence) {
case SEEK_SET:
vd->pos = offset;
break;
case SEEK_CUR:
vd->pos += offset;
break;
case SEEK_END:
vd->pos = vd->data.size() - offset;
break;
}
return 0;
}
//---------------------------------------------------------
// ovTell
//---------------------------------------------------------
static long ovTell(void* datasource)
{
VorbisData* vd = (VorbisData*)datasource;
return vd->pos;
}
//---------------------------------------------------------
// Seq
//---------------------------------------------------------
Seq::Seq()
{
running = false;
playlistChanged = false;
cs = 0;
cv = 0;
2013-04-29 18:23:03 +02:00
tackRest = 0;
tickRest = 0;
maxMidiOutPort = 0;
2012-05-26 14:49:10 +02:00
endTick = 0;
state = Transport::STOP;
2012-05-26 14:49:10 +02:00
oggInit = false;
_driver = 0;
2013-04-08 10:31:17 +02:00
playPos = events.cbegin();
2012-05-26 14:49:10 +02:00
playTime = 0;
metronomeVolume = 0.3;
2014-07-07 17:31:41 +02:00
useJackTransportSavedFlag = false;
2012-05-26 14:49:10 +02:00
inCountIn = false;
countInPlayPos = countInEvents.cbegin();
countInPlayTime = 0;
2012-05-26 14:49:10 +02:00
meterValue[0] = 0.0;
meterValue[1] = 0.0;
meterPeakValue[0] = 0.0;
meterPeakValue[1] = 0.0;
peakTimer[0] = 0;
peakTimer[1] = 0;
heartBeatTimer = new QTimer(this);
connect(heartBeatTimer, SIGNAL(timeout()), this, SLOT(heartBeatTimeout()));
2012-05-26 14:49:10 +02:00
noteTimer = new QTimer(this);
noteTimer->setSingleShot(true);
connect(noteTimer, SIGNAL(timeout()), this, SLOT(stopNotes()));
noteTimer->stop();
2014-07-07 17:31:41 +02:00
connect(this, SIGNAL(toGui(int, int)), this, SLOT(seqMessage(int, int)), Qt::QueuedConnection);
prevTimeSig.setNumerator(0);
prevTempo = 0;
connect(this, SIGNAL(timeSigChanged()),this,SLOT(handleTimeSigTempoChanged()));
connect(this, SIGNAL(tempoChanged()),this,SLOT(handleTimeSigTempoChanged()));
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// Seq
//---------------------------------------------------------
Seq::~Seq()
{
delete _driver;
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// setScoreView
//---------------------------------------------------------
void Seq::setScoreView(ScoreView* v)
{
if (oggInit) {
ov_clear(&vf);
oggInit = false;
}
if (cv !=v && cs) {
2013-02-20 17:53:15 +01:00
unmarkNotes();
2012-05-26 14:49:10 +02:00
stopWait();
}
cv = v;
2014-04-02 10:49:54 +02:00
if (cs)
disconnect(cs, SIGNAL(playlistChanged()), this, SLOT(setPlaylistChanged()));
2012-05-26 14:49:10 +02:00
cs = cv ? cv->score() : 0;
if (!heartBeatTimer->isActive())
heartBeatTimer->start(20); // msec
2012-05-26 14:49:10 +02:00
playlistChanged = true;
_synti->reset();
2014-04-02 10:49:54 +02:00
if (cs) {
2012-05-26 14:49:10 +02:00
initInstruments();
2014-04-02 10:49:54 +02:00
connect(cs, SIGNAL(playlistChanged()), this, SLOT(setPlaylistChanged()));
}
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// init
// return false on error
//---------------------------------------------------------
bool Seq::init(bool hotPlug)
2012-05-26 14:49:10 +02:00
{
if (!_driver || !_driver->start(hotPlug)) {
2012-05-26 14:49:10 +02:00
qDebug("Cannot start I/O");
running = false;
2012-05-26 14:49:10 +02:00
return false;
}
running = true;
return true;
}
//---------------------------------------------------------
// exit
//---------------------------------------------------------
void Seq::exit()
{
if (_driver) {
2012-05-26 14:49:10 +02:00
if (MScore::debugMode)
qDebug("Stop I/O");
2012-05-26 14:49:10 +02:00
stopWait();
delete _driver;
_driver = 0;
2012-05-26 14:49:10 +02:00
}
}
//---------------------------------------------------------
// rewindStart
//---------------------------------------------------------
void Seq::rewindStart()
{
seek(0);
}
2013-08-13 05:01:38 +02:00
//---------------------------------------------------------
// loopStart
//---------------------------------------------------------
void Seq::loopStart()
{
start();
// qDebug("LoopStart. playPos = %d", playPos);
2013-08-13 05:01:38 +02:00
}
2012-05-26 14:49:10 +02:00
//---------------------------------------------------------
// canStart
// return true if sequencer can be started
//---------------------------------------------------------
bool Seq::canStart()
{
if (!_driver)
2012-05-26 14:49:10 +02:00
return false;
2014-04-02 10:49:54 +02:00
if (playlistChanged)
2012-05-26 14:49:10 +02:00
collectEvents();
return (!events.empty() && endTick != 0);
}
//---------------------------------------------------------
// start
// called from gui thread
//---------------------------------------------------------
void Seq::start()
{
2014-04-02 10:49:54 +02:00
if (playlistChanged)
2012-05-26 14:49:10 +02:00
collectEvents();
if (cs->playMode() == PlayMode::AUDIO) {
2012-05-26 14:49:10 +02:00
if (!oggInit) {
vorbisData.pos = 0;
vorbisData.data = cs->audio()->data();
int n = ov_open_callbacks(&vorbisData, &vf, 0, 0, ovCallbacks);
if (n < 0) {
2014-03-04 16:48:06 +01:00
qDebug("ogg open failed: %d", n);
2012-05-26 14:49:10 +02:00
}
oggInit = true;
}
}
if ((mscore->loop())) {
2014-07-07 17:31:41 +02:00
if (cs->selection().isRange())
setLoopSelection();
2014-07-07 17:31:41 +02:00
if (!preferences.useJackTransport || (preferences.useJackTransport && state == Transport::STOP))
2014-06-18 21:36:53 +02:00
seek(cs->repeatList()->tick2utick(cs->loopInTick()));
}
else {
2014-07-07 17:31:41 +02:00
if (!preferences.useJackTransport || (preferences.useJackTransport && state == Transport::STOP))
2014-06-18 21:36:53 +02:00
seek(cs->repeatList()->tick2utick(cs->playPos()));
2014-07-07 17:31:41 +02:00
}
if (preferences.useJackTransport && mscore->countIn() && state == Transport::STOP) {
// Ready to start playing count in, switching to fake transport
// to prevent playing in other applications with our ticks simultaneously
useJackTransportSavedFlag = true;
preferences.useJackTransport = false;
}
_driver->startTransport();
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// stop
// called from gui thread
//---------------------------------------------------------
void Seq::stop()
{
if (state == Transport::STOP)
2012-05-26 14:49:10 +02:00
return;
2012-05-26 14:49:10 +02:00
if (oggInit) {
ov_clear(&vf);
oggInit = false;
}
if (!_driver)
2012-05-26 14:49:10 +02:00
return;
if (!preferences.useJackTransport || (preferences.useJackTransport && _driver->getState() == Transport::PLAY))
2014-06-18 21:36:53 +02:00
_driver->stopTransport();
2012-05-26 14:49:10 +02:00
if (cv)
cv->setCursorOn(false);
if (cs) {
cs->setLayoutAll(false);
cs->setUpdateAll();
cs->end();
}
}
2013-04-29 19:42:38 +02:00
//---------------------------------------------------------
// stopWait
//---------------------------------------------------------
void Seq::stopWait()
{
stop();
QWaitCondition sleep;
int idx = 0;
while (state != Transport::STOP) {
2015-03-06 22:54:52 +01:00
qDebug("State %d", (int)state);
2013-04-29 19:42:38 +02:00
mutex.lock();
sleep.wait(&mutex, 100);
mutex.unlock();
++idx;
Q_ASSERT(idx <= 10);
2013-04-29 19:42:38 +02:00
}
}
2012-05-26 14:49:10 +02:00
//---------------------------------------------------------
// seqStarted
//---------------------------------------------------------
void MuseScore::seqStarted()
{
2013-02-20 17:53:15 +01:00
if (cv)
2012-05-26 14:49:10 +02:00
cv->setCursorOn(true);
2013-02-20 17:53:15 +01:00
if (cs)
2012-05-26 14:49:10 +02:00
cs->end();
}
//---------------------------------------------------------
// seqStopped
// JACK has stopped
// executed in gui environment
//---------------------------------------------------------
void MuseScore::seqStopped()
{
cv->setCursorOn(false);
}
2013-02-20 17:53:15 +01:00
//---------------------------------------------------------
// unmarkNotes
//---------------------------------------------------------
void Seq::unmarkNotes()
{
foreach(const Note* n, markedNotes) {
n->setMark(false);
cs->addRefresh(n->canvasBoundingRect());
}
markedNotes.clear();
PianoTools* piano = mscore->pianoTools();
if (piano && piano->isVisible())
piano->heartBeat(markedNotes);
2013-02-20 17:53:15 +01:00
}
2012-05-26 14:49:10 +02:00
//---------------------------------------------------------
// guiStop
//---------------------------------------------------------
void Seq::guiStop()
{
QAction* a = getAction("play");
a->setChecked(false);
2013-02-20 17:53:15 +01:00
unmarkNotes();
if (!cs)
2012-05-26 14:49:10 +02:00
return;
cs->setPlayPos(cs->repeatList()->utick2tick(cs->utime2utick(qreal(playTime) / qreal(MScore::sampleRate))));
2012-05-26 14:49:10 +02:00
cs->end();
emit stopped();
}
//---------------------------------------------------------
// seqSignal
// sequencer message to GUI
// execution environment: gui thread
//---------------------------------------------------------
2014-07-07 17:31:41 +02:00
void Seq::seqMessage(int msg, int arg)
2012-05-26 14:49:10 +02:00
{
switch(msg) {
2014-07-07 17:31:41 +02:00
case '5': {
// Update the screen after seeking from the realtime thread
Segment* seg = cs->tick2segment(arg);
if (seg)
mscore->currentScoreView()->moveCursor(seg->tick());
cs->setPlayPos(arg);
cs->end();
break;
}
2013-09-12 15:57:14 +02:00
case '4': // Restart the playback at the end of the score
loopStart();
break;
case '3': // Loop restart while playing
seek(cs->repeatList()->tick2utick(cs->loopInTick()));
break;
2013-04-29 20:16:06 +02:00
case '2':
2012-05-26 14:49:10 +02:00
guiStop();
// heartBeatTimer->stop();
if (_driver && mscore->getSynthControl()) {
2012-05-26 14:49:10 +02:00
meterValue[0] = .0f;
meterValue[1] = .0f;
meterPeakValue[0] = .0f;
meterPeakValue[1] = .0f;
peakTimer[0] = 0;
peakTimer[1] = 0;
mscore->getSynthControl()->setMeter(0.0, 0.0, 0.0, 0.0);
}
2013-04-29 19:42:38 +02:00
seek(0);
2012-05-26 14:49:10 +02:00
break;
case '0': // STOP
guiStop();
// heartBeatTimer->stop();
if (_driver && mscore->getSynthControl()) {
2012-05-26 14:49:10 +02:00
meterValue[0] = .0f;
meterValue[1] = .0f;
meterPeakValue[0] = .0f;
meterPeakValue[1] = .0f;
peakTimer[0] = 0;
peakTimer[1] = 0;
mscore->getSynthControl()->setMeter(0.0, 0.0, 0.0, 0.0);
}
break;
case '1': // PLAY
emit started();
// heartBeatTimer->start(1000/guiRefresh);
break;
default:
qDebug("MScore::Seq:: unknown seq msg %d", msg);
2012-05-26 14:49:10 +02:00
break;
}
}
//---------------------------------------------------------
// playEvent
// send one event to the synthesizer
//---------------------------------------------------------
void Seq::playEvent(const NPlayEvent& event, unsigned framePos)
2012-05-26 14:49:10 +02:00
{
int type = event.type();
if (type == ME_NOTEON) {
bool mute;
const Note* note = event.note();
if (note) {
Instrument* instr = note->staff()->part()->instrument(note->chord()->tick());
const Channel* a = instr->channel(note->subchannel());
mute = a->mute || a->soloMute;
2012-05-26 14:49:10 +02:00
}
else
mute = false;
if (!mute)
putEvent(event, framePos);
2012-05-26 14:49:10 +02:00
}
2015-06-01 19:49:48 +02:00
else if (type == ME_CONTROLLER || type == ME_PITCHBEND)
putEvent(event, framePos);
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// recomputeMaxMidiOutPort
// Computes the maximum number of midi out ports
// in all opened scores
//---------------------------------------------------------
void Seq::recomputeMaxMidiOutPort() {
if (!(preferences.useJackMidi || preferences.useAlsaAudio))
return;
int max = 0;
2015-04-03 18:23:51 +02:00
foreach(Score * s, MuseScoreCore::mscoreCore->scores()) {
if (s->midiPortCount() > max)
max = s->midiPortCount();
}
maxMidiOutPort = max;
}
2012-05-26 14:49:10 +02:00
//---------------------------------------------------------
// processMessages
2014-07-07 17:31:41 +02:00
// from gui to process thread
2012-05-26 14:49:10 +02:00
//---------------------------------------------------------
void Seq::processMessages()
{
for (;;) {
if (toSeq.isEmpty())
break;
SeqMsg msg = toSeq.dequeue();
switch(msg.id) {
case SeqMsgId::TEMPO_CHANGE:
2012-05-26 14:49:10 +02:00
{
if (!cs)
continue;
2012-05-26 14:49:10 +02:00
if (playTime != 0) {
int utick = cs->utime2utick(qreal(playTime) / qreal(MScore::sampleRate));
2013-02-20 17:53:15 +01:00
cs->tempomap()->setRelTempo(msg.realVal);
playTime = cs->utick2utime(utick) * MScore::sampleRate;
if (preferences.jackTimebaseMaster && preferences.useJackTransport)
2014-07-07 17:31:41 +02:00
_driver->seekTransport(utick + 2 * cs->utime2utick(qreal((_driver->bufferSize()) + 1) / qreal(MScore::sampleRate)));
2012-05-26 14:49:10 +02:00
}
else
2013-02-20 17:53:15 +01:00
cs->tempomap()->setRelTempo(msg.realVal);
2014-11-28 12:41:59 +01:00
cs->repeatList()->update();
prevTempo = curTempo();
emit tempoChanged();
2012-05-26 14:49:10 +02:00
}
break;
case SeqMsgId::PLAY:
2012-05-26 14:49:10 +02:00
putEvent(msg.event);
break;
case SeqMsgId::SEEK:
2013-02-20 17:53:15 +01:00
setPos(msg.intVal);
2012-05-26 14:49:10 +02:00
break;
default:
break;
2012-05-26 14:49:10 +02:00
}
}
}
//---------------------------------------------------------
// metronome
//---------------------------------------------------------
void Seq::metronome(unsigned n, float* p, bool force)
2012-05-26 14:49:10 +02:00
{
if (!mscore->metronome() && !force) {
2012-05-26 14:49:10 +02:00
tickRest = 0;
tackRest = 0;
return;
}
if (tickRest) {
2013-04-29 18:23:03 +02:00
tackRest = 0;
2012-05-26 14:49:10 +02:00
int idx = tickLength - tickRest;
int nn = n < tickRest ? n : tickRest;
for (int i = 0; i < nn; ++i) {
qreal v = tick[idx] * metronomeVolume;
*p++ += v;
*p++ += v;
++idx;
}
tickRest -= nn;
}
if (tackRest) {
int idx = tackLength - tackRest;
int nn = n < tackRest ? n : tackRest;
for (int i = 0; i < nn; ++i) {
qreal v = tack[idx] * metronomeVolume;
*p++ += v;
*p++ += v;
++idx;
}
tackRest -= nn;
}
}
//---------------------------------------------------------
// addCountInClicks
//---------------------------------------------------------
void Seq::addCountInClicks()
{
int plPos = cs->playPos();
Measure* m = cs->tick2measure(plPos);
int msrTick = m->tick();
qreal tempo = cs->tempomap()->tempo(msrTick);
Fraction timeSig = cs->sigmap()->timesig(msrTick).nominal();
int numerator = timeSig.numerator();
int denominator = timeSig.denominator();
int clickTicks = MScore::division * 4 / denominator;
NPlayEvent event;
int tick;
bool triplets = false; // whether to play click-clack in triplets or not
// COMPOUND METER: if time sig is 3*n/d, convert to 3d units
// note: 3/8, 3/16, ... are NOT considered compound
if (numerator > 3 && numerator % 3 == 0) {
// if denominator longer than 1/8 OR tempo for compound unit slower than 60MM
// (i.e. each denom. unit slower than 180MM = tempo 3.0)
// then do not count as compound, but beat click-clack-clack triplets
if (denominator < 8 || tempo * denominator / 4 < 3.0)
triplets = true;
// otherwise, count as compound meter (one beat every 3 denominator units)
else {
numerator /= 3;
clickTicks *= 3;
}
}
// NUMBER OF TICKS
int numOfClicks = numerator; // default to a full measure of 'clicks'
int lastPause = clickTicks; // the number of ticks to wait after the last 'click'
// if not at the beginning of a measure, add clicks for the initial measure part
if (msrTick < plPos) {
int delta = plPos - msrTick;
2014-07-07 17:31:41 +02:00
int addClick = (delta + clickTicks - 1) / clickTicks; // round num. of clicks up
numOfClicks += addClick;
2014-07-07 17:31:41 +02:00
lastPause = delta - (addClick - 1) * clickTicks; // anything after last click time is final pause
}
// or if measure not complete (anacrusis), add clicks for the missing measure part
else if (m->ticks() < clickTicks * numerator) {
2014-07-07 17:31:41 +02:00
int delta = clickTicks * numerator - m->ticks();
int addClick = (delta + clickTicks - 1) / clickTicks;
numOfClicks += addClick;
2014-07-07 17:31:41 +02:00
lastPause = delta - (addClick - 1) * clickTicks;
}
/*
// MIN_CLICKS: be sure to have at least MIN_CLICKS clicks: if less, add full measures
while (numOfClicks < MIN_CLICKS)
numOfClicks += numerator;
*/
// click-clack-clack triplets
if (triplets)
numerator = 3;
// add count-in events
for (int i = tick = 0; i < numOfClicks; i++, tick += clickTicks) {
event.setType( (i % numerator) == 0 ? ME_TICK1 : ME_TICK2);
countInEvents.insert( std::pair<int,NPlayEvent>(tick, event));
}
// add 1 empty event at the end to wait after the last click
tick += lastPause - clickTicks;
event.setType(ME_INVALID);
event.setPitch(0);
countInEvents.insert( std::pair<int,NPlayEvent>(tick, event));
// initialize play parameters to count-in events
countInPlayPos = countInEvents.cbegin();
countInPlayTime = 0;
}
2014-06-05 08:35:39 +02:00
//-------------------------------------------------------------------
2012-05-26 14:49:10 +02:00
// process
2014-06-05 08:35:39 +02:00
// This function is called in a realtime context. This
// means that no blocking operations are allowed which
// includes memory allocation. The usual thread synchronisation
// methods like semaphores can also not be used.
//-------------------------------------------------------------------
2012-05-26 14:49:10 +02:00
void Seq::process(unsigned n, float* buffer)
{
unsigned frames = n;
Transport driverState = _driver->getState();
// Checking for the reposition from JACK Transport
2014-07-07 17:31:41 +02:00
_driver->checkTransportSeek(playTime, frames, inCountIn);
if (driverState != state) {
2014-05-24 21:30:33 +02:00
// Got a message from JACK Transport panel: Play
if (state == Transport::STOP && driverState == Transport::PLAY) {
2014-07-07 17:31:41 +02:00
if ((preferences.useJackMidi || preferences.useJackAudio) && !getAction("play")->isChecked()) {
2014-05-24 21:30:33 +02:00
// Do not play while editing elements
2014-07-07 17:31:41 +02:00
if (mscore->state() != STATE_NORMAL || !isRunning() || !canStart())
2014-05-24 21:30:33 +02:00
return;
2014-07-07 17:31:41 +02:00
getAction("play")->setChecked(true);
getAction("play")->triggered(true);
// If we just launch MuseScore and press "Play" on JACK Transport with time 0:00
// MuseScore doesn't seek to 0 and guiPos is uninitialized, so let's make it manually
if (preferences.useJackTransport && getCurTick() == 0)
seekRT(0);
// Switching to fake transport while playing count in
// to prevent playing in other applications with our ticks simultaneously
if (preferences.useJackTransport && mscore->countIn()) {
// Stopping real JACK Transport
_driver->stopTransport();
// Starting fake transport
useJackTransportSavedFlag = preferences.useJackTransport;
preferences.useJackTransport = false;
_driver->startTransport();
2014-05-24 21:30:33 +02:00
}
}
if (preferences.useJackMidi) { // TODO same for ALSA Midi
// Initializing instruments every time we start playback.
// External synth can have wrong values, for example
// if we switch between scores
initInstruments(true);
}
2014-05-24 21:30:33 +02:00
// Need to change state after calling collectEvents()
state = Transport::PLAY;
2014-05-31 07:23:36 +02:00
if (mscore->countIn() && cs->playMode() == PlayMode::SYNTHESIZER) {
countInEvents.clear();
inCountIn = true;
}
2013-04-29 19:42:38 +02:00
emit toGui('1');
}
2014-05-24 21:30:33 +02:00
// Got a message from JACK Transport panel: Stop
else if (state == Transport::PLAY && driverState == Transport::STOP) {
state = Transport::STOP;
2014-05-24 21:30:33 +02:00
// Muting all notes
stopNotes(-1, true);
if (playPos == events.cend()) {
if (mscore->loop()) {
2013-09-12 17:16:17 +02:00
qDebug("Seq.cpp - Process - Loop whole score. playPos = %d cs->pos() = %d", playPos->first,cs->pos());
2013-09-12 15:57:14 +02:00
emit toGui('4');
return;
2013-08-19 11:21:21 +02:00
}
else {
emit toGui('2');
}
}
else {
emit toGui('0');
}
2013-04-29 19:42:38 +02:00
}
2012-05-26 14:49:10 +02:00
else if (state != driverState)
qDebug("Seq: state transition %d -> %d ?",
2015-03-06 22:54:52 +01:00
(int)state, (int)driverState);
2012-05-26 14:49:10 +02:00
}
2014-11-28 15:02:56 +01:00
memset(buffer, 0, sizeof(float) * n * 2); // assume two channels
2012-05-26 14:49:10 +02:00
float* p = buffer;
2012-05-26 14:49:10 +02:00
processMessages();
if (state == Transport::PLAY) {
2014-07-07 17:31:41 +02:00
if (!cs)
2013-06-28 16:59:19 +02:00
return;
EventMap::const_iterator* pPlayPos = &playPos;
EventMap* pEvents = &events;
int* pPlayTime = &playTime;
//
// in count-in?
//
if (inCountIn) {
if (countInEvents.size() == 0)
addCountInClicks();
pEvents = &countInEvents;
pPlayPos = &countInPlayPos;
pPlayTime = &countInPlayTime;
}
2012-05-26 14:49:10 +02:00
//
// play events for one segment
//
unsigned framePos = 0;
int endTime = *pPlayTime + frames;
int utickEnd = cs->repeatList()->tick2utick(cs->lastMeasure()->endTick()) - 1;
for ( ; *pPlayPos != pEvents->cend(); ) {
int n;
if (inCountIn) {
qreal bps = curTempo() * cs->tempomap()->relTempo();
// relTempo needed here to ensure that bps changes as we slide the tempo bar
qreal tickssec = bps * MScore::division;
qreal secs = (*pPlayPos)->first / tickssec;
int f = secs * MScore::sampleRate;
if (f >= endTime)
break;
n = f - *pPlayTime;
if (n < 0) {
qDebug("Count-in: %d: %d - %d", (*pPlayPos)->first, f, *pPlayTime);
n = 0;
}
2012-05-26 14:49:10 +02:00
}
else {
int f = cs->utick2utime(playPos->first) * MScore::sampleRate;
if (f >= endTime)
break;
n = f - *pPlayTime;
if (n < 0) {
qDebug("%d: %d - %d", (*pPlayPos)->first, f, *pPlayTime);
n = 0;
}
if (mscore->loop()) {
int utickLoop = cs->repeatList()->tick2utick(cs->loopOutTick());
if (utickLoop < utickEnd)
if ((*pPlayPos)->first >= utickLoop) {
2014-06-18 21:36:53 +02:00
qDebug ("Process playPos = %d in/out tick = %d/%d getCurTick() = %d tickLoop = %d playTime = %d",
(*pPlayPos)->first, cs->loopInTick(), cs->loopOutTick(), getCurTick(), utickLoop, *pPlayTime);
if (preferences.useJackTransport) {
2014-06-18 21:36:53 +02:00
int loopInUtick = cs->repeatList()->tick2utick(cs->loopInTick());
2014-07-07 17:31:41 +02:00
_driver->seekTransport(loopInUtick);
2014-06-18 21:36:53 +02:00
if (loopInUtick != 0) {
2014-07-07 17:31:41 +02:00
int seekto = loopInUtick - 2 * cs->utime2utick((qreal)_driver->bufferSize() / MScore::sampleRate);
seekRT((seekto > 0) ? seekto : 0 );
2014-06-18 21:36:53 +02:00
}
}
else {
emit toGui('3');
}
2014-06-18 21:36:53 +02:00
// Exit this function to avoid segmentation fault in Scoreview
return;
}
}
}
2012-05-26 14:49:10 +02:00
if (n) {
if (cs->playMode() == PlayMode::SYNTHESIZER) {
metronome(n, p, inCountIn);
_synti->process(n, p);
2012-05-26 14:49:10 +02:00
p += n * 2;
*pPlayTime += n;
2012-05-26 14:49:10 +02:00
frames -= n;
framePos += n;
}
else {
while (n > 0) {
int section;
float** pcm;
long rn = ov_read_float(&vf, &pcm, n, &section);
if (rn == 0)
break;
for (int i = 0; i < rn; ++i) {
*p++ = pcm[0][i];
*p++ = pcm[1][i];
}
*pPlayTime += rn;
2012-05-26 14:49:10 +02:00
frames -= rn;
framePos += rn;
n -= rn;
}
}
}
const NPlayEvent& event = (*pPlayPos)->second;
playEvent(event, framePos);
2012-05-26 14:49:10 +02:00
if (event.type() == ME_TICK1)
tickRest = tickLength;
else if (event.type() == ME_TICK2)
tackRest = tackLength;
2013-02-20 17:53:15 +01:00
mutex.lock();
++(*pPlayPos);
2013-02-20 17:53:15 +01:00
mutex.unlock();
2012-05-26 14:49:10 +02:00
}
if (frames) {
if (cs->playMode() == PlayMode::SYNTHESIZER) {
metronome(frames, p, inCountIn);
_synti->process(frames, p);
*pPlayTime += frames;
2012-05-26 14:49:10 +02:00
}
else {
int n = frames;
while (n > 0) {
int section;
float** pcm;
long rn = ov_read_float(&vf, &pcm, n, &section);
if (rn == 0)
break;
for (int i = 0; i < rn; ++i) {
*p++ = pcm[0][i];
*p++ = pcm[1][i];
}
*pPlayTime += rn;
frames -= rn;
framePos += rn;
n -= rn;
2012-05-26 14:49:10 +02:00
}
}
}
if (*pPlayPos == pEvents->cend()) {
2014-07-07 17:31:41 +02:00
if (inCountIn) {
inCountIn = false;
2014-07-07 17:31:41 +02:00
// Connecting to JACK Transport if MuseScore was temporarily disconnected from it
if (useJackTransportSavedFlag) {
// Stopping fake driver
_driver->stopTransport();
preferences.useJackTransport = true;
// Starting the real JACK Transport. All applications play in sync now
_driver->startTransport();
}
}
else
_driver->stopTransport();
}
2012-05-26 14:49:10 +02:00
}
else {
_synti->process(frames, p);
2012-05-26 14:49:10 +02:00
}
//
2013-03-27 20:10:14 +01:00
// metering / master gain
2012-05-26 14:49:10 +02:00
//
float lv = 0.0f;
float rv = 0.0f;
2013-03-27 20:10:14 +01:00
p = buffer;
2012-05-26 14:49:10 +02:00
for (unsigned i = 0; i < n; ++i) {
2013-04-03 12:49:55 +02:00
qreal val = *p;
2013-03-27 20:10:14 +01:00
lv = qMax(lv, fabsf(val));
*p++ = val;
2013-04-03 12:49:55 +02:00
val = *p;
2013-03-27 20:10:14 +01:00
rv = qMax(lv, fabsf(val));
*p++ = val;
2012-05-26 14:49:10 +02:00
}
meterValue[0] = lv;
meterValue[1] = rv;
if (meterPeakValue[0] < lv) {
meterPeakValue[0] = lv;
peakTimer[0] = 0;
}
if (meterPeakValue[1] < rv) {
meterPeakValue[1] = rv;
peakTimer[1] = 0;
}
}
2012-05-26 14:49:10 +02:00
//---------------------------------------------------------
// initInstruments
//---------------------------------------------------------
void Seq::initInstruments(bool realTime)
2012-05-26 14:49:10 +02:00
{
// Add midi out ports if necessary
if (cs && (preferences.useJackMidi || preferences.useAlsaAudio)) {
// Increase the maximum number of midi ports if user adds staves/instruments
int scoreMaxMidiPort = cs->midiPortCount();
if (maxMidiOutPort < scoreMaxMidiPort)
maxMidiOutPort = scoreMaxMidiPort;
// if maxMidiOutPort is equal to existing ports number, it will do nothing
if (_driver)
_driver->updateOutPortCount(maxMidiOutPort + 1);
}
2012-05-26 14:49:10 +02:00
foreach(const MidiMapping& mm, *cs->midiMapping()) {
Channel* channel = mm.articulation;
foreach(const MidiCoreEvent& e, channel->init) {
2012-05-26 14:49:10 +02:00
if (e.type() == ME_INVALID)
continue;
NPlayEvent event(e.type(), channel->channel, e.dataA(), e.dataB());
if (realTime)
putEvent(event);
else
sendEvent(event);
2012-05-26 14:49:10 +02:00
}
}
}
//---------------------------------------------------------
// collectEvents
//---------------------------------------------------------
void Seq::collectEvents()
{
2012-06-23 15:54:01 +02:00
//do not collect even while playing
if (state == Transport::PLAY)
2012-06-23 15:54:01 +02:00
return;
2012-05-26 14:49:10 +02:00
events.clear();
2013-05-03 14:45:25 +02:00
mutex.lock();
2012-11-19 09:29:46 +01:00
cs->renderMidi(&events);
2012-05-26 14:49:10 +02:00
endTick = 0;
2012-05-26 14:49:10 +02:00
if (!events.empty()) {
2013-04-08 10:31:17 +02:00
auto e = events.cend();
2012-05-26 14:49:10 +02:00
--e;
2013-04-08 10:31:17 +02:00
endTick = e->first;
2012-05-26 14:49:10 +02:00
}
2013-05-03 14:45:25 +02:00
playPos = events.cbegin();
mutex.unlock();
2012-05-26 14:49:10 +02:00
playlistChanged = false;
}
//---------------------------------------------------------
// getCurTick
//---------------------------------------------------------
int Seq::getCurTick()
{
return cs->utime2utick(qreal(playTime) / qreal(MScore::sampleRate));
}
//---------------------------------------------------------
// setRelTempo
// relTempo = 1.0 = normal tempo
//---------------------------------------------------------
void Seq::setRelTempo(double relTempo)
{
guiToSeq(SeqMsg(SeqMsgId::TEMPO_CHANGE, relTempo));
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// setPos
// seek
// realtime environment
//---------------------------------------------------------
void Seq::setPos(int utick)
{
if (cs == 0)
return;
stopNotes(-1, true);
2012-05-26 14:49:10 +02:00
2013-05-03 14:24:14 +02:00
int ucur;
if (playPos != events.end())
ucur = cs->repeatList()->utick2tick(playPos->first);
else
ucur = utick - 1;
2013-04-29 19:42:38 +02:00
if (utick != ucur)
updateSynthesizerState(ucur, utick);
2012-05-26 14:49:10 +02:00
playTime = cs->utick2utime(utick) * MScore::sampleRate;
2013-02-20 17:53:15 +01:00
mutex.lock();
2013-04-08 10:31:17 +02:00
playPos = events.lower_bound(utick);
2013-02-20 17:53:15 +01:00
mutex.unlock();
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
2014-07-07 17:31:41 +02:00
// seekCommon
// a common part of seek() and seekRT(), contains code
// that could be safely called from any thread.
// Do not use explicitly, use seek() or seekRT()
2012-05-26 14:49:10 +02:00
//---------------------------------------------------------
2014-07-07 17:31:41 +02:00
void Seq::seekCommon(int utick)
2012-05-26 14:49:10 +02:00
{
if (cs == 0)
return;
2013-02-20 17:53:15 +01:00
2014-04-02 10:49:54 +02:00
if (playlistChanged)
2013-04-25 14:35:32 +02:00
collectEvents();
2014-07-07 17:31:41 +02:00
if (cs->playMode() == PlayMode::AUDIO) {
ogg_int64_t sp = cs->utick2utime(utick) * MScore::sampleRate;
ov_pcm_seek(&vf, sp);
2014-06-18 21:36:53 +02:00
}
2014-07-07 17:31:41 +02:00
guiPos = events.lower_bound(utick);
mscore->setPos(cs->repeatList()->utick2tick(utick));
unmarkNotes();
2013-02-20 17:53:15 +01:00
}
//---------------------------------------------------------
// seek
2014-07-07 17:31:41 +02:00
// send seek message to sequencer
// gui thread
2013-02-20 17:53:15 +01:00
//---------------------------------------------------------
2014-07-07 17:31:41 +02:00
void Seq::seek(int utick)
2013-02-20 17:53:15 +01:00
{
2014-07-07 17:31:41 +02:00
if (preferences.useJackTransport) {
if (utick > endTick)
utick = 0;
_driver->seekTransport(utick);
if (utick != 0)
return;
}
seekCommon(utick);
2013-05-03 14:45:25 +02:00
2014-06-18 21:36:53 +02:00
int tick = cs->repeatList()->utick2tick(utick);
2014-07-07 17:31:41 +02:00
Segment* seg = cs->tick2segment(tick);
2013-05-03 14:45:25 +02:00
if (seg)
2013-07-03 11:17:14 +02:00
mscore->currentScoreView()->moveCursor(seg->tick());
2014-06-18 21:36:53 +02:00
cs->setPlayPos(tick);
2012-05-26 14:49:10 +02:00
cs->end();
2014-07-07 17:31:41 +02:00
guiToSeq(SeqMsg(SeqMsgId::SEEK, utick));
}
2012-11-19 09:29:46 +01:00
2014-07-07 17:31:41 +02:00
//---------------------------------------------------------
// seekRT
// realtime thread
//---------------------------------------------------------
2012-05-26 14:49:10 +02:00
2014-07-07 17:31:41 +02:00
void Seq::seekRT(int utick)
{
if (preferences.useJackTransport && utick > endTick)
utick = 0;
seekCommon(utick);
setPos(utick);
// Update the screen in GUI thread
emit toGui('5', cs->repeatList()->utick2tick(utick));
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// startNote
//---------------------------------------------------------
2012-06-28 14:50:47 +02:00
void Seq::startNote(int channel, int pitch, int velo, double nt)
2012-05-26 14:49:10 +02:00
{
if (state != Transport::STOP)
2012-05-26 14:49:10 +02:00
return;
NPlayEvent ev(ME_NOTEON, channel, pitch, velo);
2012-05-26 14:49:10 +02:00
ev.setTuning(nt);
sendEvent(ev);
}
2012-06-28 14:50:47 +02:00
void Seq::startNote(int channel, int pitch, int velo, int duration, double nt)
2012-05-26 14:49:10 +02:00
{
stopNotes();
2012-06-28 14:50:47 +02:00
startNote(channel, pitch, velo, nt);
2012-05-26 14:49:10 +02:00
startNoteTimer(duration);
}
//---------------------------------------------------------
// startNoteTimer
//---------------------------------------------------------
void Seq::startNoteTimer(int duration)
{
if (duration) {
noteTimer->setInterval(duration);
noteTimer->start();
}
}
//---------------------------------------------------------
// stopNoteTimer
//---------------------------------------------------------
void Seq::stopNoteTimer()
{
if (noteTimer->isActive()) {
noteTimer->stop();
stopNotes();
}
}
//---------------------------------------------------------
// stopNotes
//---------------------------------------------------------
void Seq::stopNotes(int channel, bool realTime)
2012-05-26 14:49:10 +02:00
{
auto send = [this, realTime](const NPlayEvent& event) {
if (realTime)
putEvent(event);
else
sendEvent(event);
};
// Stop notes in all channels
if (channel == -1) {
2014-12-20 21:56:46 +01:00
for(int ch = 0; ch < cs->midiMapping()->size(); ch++) {
send(NPlayEvent(ME_CONTROLLER, ch, CTRL_SUSTAIN, 0));
send(NPlayEvent(ME_CONTROLLER, ch, CTRL_ALL_NOTES_OFF, 0));
2014-12-20 21:56:46 +01:00
}
}
else {
2014-12-20 21:56:46 +01:00
send(NPlayEvent(ME_CONTROLLER, channel, CTRL_SUSTAIN, 0));
send(NPlayEvent(ME_CONTROLLER, channel, CTRL_ALL_NOTES_OFF, 0));
}
2014-07-07 17:31:41 +02:00
if (preferences.useAlsaAudio || preferences.useJackAudio || preferences.usePulseAudio || preferences.usePortaudioAudio)
_synti->allNotesOff(channel);
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// setController
//---------------------------------------------------------
void Seq::setController(int channel, int ctrl, int data)
{
NPlayEvent event(ME_CONTROLLER, channel, ctrl, data);
2012-05-26 14:49:10 +02:00
sendEvent(event);
}
//---------------------------------------------------------
// sendEvent
// called from GUI context to send a midi event to
// midi out or synthesizer
//---------------------------------------------------------
void Seq::sendEvent(const NPlayEvent& ev)
2012-05-26 14:49:10 +02:00
{
guiToSeq(SeqMsg(SeqMsgId::PLAY, ev));
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// nextMeasure
//---------------------------------------------------------
void Seq::nextMeasure()
{
2013-04-08 10:31:17 +02:00
Measure* m = cs->tick2measure(guiPos->first);
2012-05-26 14:49:10 +02:00
if (m) {
if (m->nextMeasure())
m = m->nextMeasure();
seek(m->tick());
2012-05-26 14:49:10 +02:00
}
}
//---------------------------------------------------------
// nextChord
//---------------------------------------------------------
void Seq::nextChord()
{
2013-04-08 10:31:17 +02:00
int tick = guiPos->first;
for (auto i = guiPos; i != events.cend(); ++i) {
if (i->second.type() == ME_NOTEON && i->first > tick && i->second.velo()) {
seek(i->first);
2012-05-26 14:49:10 +02:00
break;
}
}
}
//---------------------------------------------------------
// prevMeasure
//---------------------------------------------------------
void Seq::prevMeasure()
{
auto i = guiPos;
if (i == events.begin())
2012-05-26 14:49:10 +02:00
return;
--i;
2013-04-08 10:31:17 +02:00
Measure* m = cs->tick2measure(i->first);
2012-05-26 14:49:10 +02:00
if (m) {
2013-04-08 10:31:17 +02:00
if ((i->first == m->tick()) && m->prevMeasure())
2013-02-20 17:53:15 +01:00
m = m->prevMeasure();
seek(m->tick());
2012-05-26 14:49:10 +02:00
}
}
//---------------------------------------------------------
// prevChord
//---------------------------------------------------------
void Seq::prevChord()
{
2013-04-08 10:31:17 +02:00
int tick = playPos->first;
2012-05-26 14:49:10 +02:00
//find the chord just before playpos
EventMap::const_iterator i = events.upper_bound(cs->repeatList()->tick2utick(tick));
2012-05-26 14:49:10 +02:00
for (;;) {
2013-04-08 10:31:17 +02:00
if (i->second.type() == ME_NOTEON) {
const NPlayEvent& n = i->second;
2013-04-08 10:31:17 +02:00
if (i->first < tick && n.velo()) {
tick = i->first;
2012-05-26 14:49:10 +02:00
break;
}
}
2013-04-08 10:31:17 +02:00
if (i == events.cbegin())
2012-05-26 14:49:10 +02:00
break;
--i;
}
//go the previous chord
2013-04-08 10:31:17 +02:00
if (i != events.cbegin()) {
2012-05-26 14:49:10 +02:00
i = playPos;
for (;;) {
2013-04-08 10:31:17 +02:00
if (i->second.type() == ME_NOTEON) {
const NPlayEvent& n = i->second;
2013-04-08 10:31:17 +02:00
if (i->first < tick && n.velo()) {
seek(i->first);
2012-05-26 14:49:10 +02:00
break;
}
}
2013-04-08 10:31:17 +02:00
if (i == events.cbegin())
2012-05-26 14:49:10 +02:00
break;
--i;
}
}
}
//---------------------------------------------------------
// seekEnd
//---------------------------------------------------------
void Seq::seekEnd()
{
qDebug("seek to end");
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// guiToSeq
//---------------------------------------------------------
void Seq::guiToSeq(const SeqMsg& msg)
{
if (!_driver || !running)
2012-05-26 14:49:10 +02:00
return;
toSeq.enqueue(msg);
}
//---------------------------------------------------------
// eventToGui
//---------------------------------------------------------
void Seq::eventToGui(NPlayEvent e)
2012-05-26 14:49:10 +02:00
{
fromSeq.enqueue(SeqMsg(SeqMsgId::MIDI_INPUT_EVENT, e));
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// midiInputReady
//---------------------------------------------------------
void Seq::midiInputReady()
{
if (_driver)
_driver->midiRead();
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// SeqMsgFifo
//---------------------------------------------------------
SeqMsgFifo::SeqMsgFifo()
{
maxCount = SEQ_MSG_FIFO_SIZE;
clear();
}
//---------------------------------------------------------
// enqueue
//---------------------------------------------------------
void SeqMsgFifo::enqueue(const SeqMsg& msg)
{
int i = 0;
int n = 50;
QMutex mutex;
QWaitCondition qwc;
mutex.lock();
for (; i < n; ++i) {
if (!isFull())
break;
qwc.wait(&mutex,100);
}
2013-02-15 14:50:03 +01:00
mutex.unlock();
2012-05-26 14:49:10 +02:00
if (i == n) {
qDebug("===SeqMsgFifo: overflow");
2012-05-26 14:49:10 +02:00
return;
}
messages[widx] = msg;
push();
}
//---------------------------------------------------------
// dequeue
//---------------------------------------------------------
SeqMsg SeqMsgFifo::dequeue()
{
SeqMsg msg = messages[ridx];
pop();
return msg;
}
//---------------------------------------------------------
// putEvent
//---------------------------------------------------------
void Seq::putEvent(const NPlayEvent& event, unsigned framePos)
2012-05-26 14:49:10 +02:00
{
if (!cs)
return;
int channel = event.channel();
if (channel >= cs->midiMapping()->size()) {
qDebug("bad channel value %d >= %d", channel, cs->midiMapping()->size());
return;
}
2013-04-04 13:01:58 +02:00
int syntiIdx= _synti->index(cs->midiMapping(channel)->articulation->synti);
_synti->play(event, syntiIdx);
if (_driver != 0 && (preferences.useJackMidi || preferences.useAlsaAudio))
_driver->putEvent(event, framePos);
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// heartBeat
// update GUI
//---------------------------------------------------------
void Seq::heartBeatTimeout()
2012-05-26 14:49:10 +02:00
{
SynthControl* sc = mscore->getSynthControl();
if (sc && _driver) {
2012-05-26 14:49:10 +02:00
if (++peakTimer[0] >= peakHold)
meterPeakValue[0] *= .7f;
if (++peakTimer[1] >= peakHold)
meterPeakValue[1] *= .7f;
sc->setMeter(meterValue[0], meterValue[1], meterPeakValue[0], meterPeakValue[1]);
}
2013-02-20 17:53:15 +01:00
while (!fromSeq.isEmpty()) {
SeqMsg msg = fromSeq.dequeue();
if (msg.id == SeqMsgId::MIDI_INPUT_EVENT) {
2013-02-20 17:53:15 +01:00
int type = msg.event.type();
if (type == ME_NOTEON)
mscore->midiNoteReceived(msg.event.channel(), msg.event.pitch(), msg.event.velo());
else if (type == ME_NOTEOFF)
mscore->midiNoteReceived(msg.event.channel(), msg.event.pitch(), 0);
else if (type == ME_CONTROLLER)
mscore->midiCtrlReceived(msg.event.controller(), msg.event.value());
}
}
if (state != Transport::PLAY || inCountIn)
2012-05-26 14:49:10 +02:00
return;
2014-06-18 21:36:53 +02:00
2012-05-26 14:49:10 +02:00
int endTime = playTime;
2013-02-20 17:53:15 +01:00
mutex.lock();
auto ppos = playPos;
2013-04-08 10:31:17 +02:00
if (ppos != events.cbegin())
2013-02-20 17:53:15 +01:00
--ppos;
mutex.unlock();
2014-07-07 17:31:41 +02:00
if (cs && cs->sigmap()->timesig(getCurTick()).nominal()!=prevTimeSig) {
2014-06-05 17:50:29 +02:00
prevTimeSig = cs->sigmap()->timesig(getCurTick()).nominal();
emit timeSigChanged();
}
2014-07-07 17:31:41 +02:00
if (cs && curTempo()!=prevTempo) {
2014-06-05 17:50:29 +02:00
prevTempo = curTempo();
emit tempoChanged();
}
2013-06-25 10:41:15 +02:00
QRectF r;
2013-04-08 10:31:17 +02:00
for (;guiPos != events.cend(); ++guiPos) {
if (guiPos->first > ppos->first)
break;
if (mscore->loop())
if (guiPos->first >= cs->repeatList()->tick2utick(cs->loopOutTick()))
break;
const NPlayEvent& n = guiPos->second;
2013-02-20 17:53:15 +01:00
if (n.type() == ME_NOTEON) {
2012-05-26 14:49:10 +02:00
const Note* note1 = n.note();
if (n.velo()) {
while (note1) {
2013-02-20 17:53:15 +01:00
note1->setMark(true);
2012-05-26 14:49:10 +02:00
markedNotes.append(note1);
2013-06-25 10:41:15 +02:00
r |= note1->canvasBoundingRect();
2012-05-26 14:49:10 +02:00
note1 = note1->tieFor() ? note1->tieFor()->endNote() : 0;
}
}
else {
while (note1) {
2013-02-20 17:53:15 +01:00
note1->setMark(false);
2013-06-25 10:41:15 +02:00
r |= note1->canvasBoundingRect();
2012-05-26 14:49:10 +02:00
markedNotes.removeOne(note1);
note1 = note1->tieFor() ? note1->tieFor()->endNote() : 0;
}
}
}
}
2013-04-08 10:31:17 +02:00
int utick = ppos->first;
int tick = cs->repeatList()->utick2tick(utick);
2012-05-26 14:49:10 +02:00
mscore->currentScoreView()->moveCursor(tick);
mscore->setPos(tick);
emit(heartBeat(tick, utick, endTime));
2012-05-26 14:49:10 +02:00
PianorollEditor* pre = mscore->getPianorollEditor();
if (pre && pre->isVisible())
pre->heartBeat(this);
PianoTools* piano = mscore->pianoTools();
if (piano && piano->isVisible())
piano->heartBeat(markedNotes);
2013-06-25 10:41:15 +02:00
cv->update(cv->toPhysical(r));
2012-05-26 14:49:10 +02:00
}
2013-04-25 14:35:32 +02:00
//---------------------------------------------------------
// updateSynthesizerState
// collect all controller events between tick1 and tick2
// and send them to the synthesizer
2014-10-01 20:17:30 +02:00
// Called from RT thread
2013-04-25 14:35:32 +02:00
//---------------------------------------------------------
void Seq::updateSynthesizerState(int tick1, int tick2)
{
if (tick1 > tick2)
tick1 = 0;
2014-10-01 20:17:30 +02:00
// Making a local copy of events to avoid touching it
// from different threads at the same time
EventMap ev = events;
EventMap::const_iterator i1 = ev.lower_bound(tick1);
EventMap::const_iterator i2 = ev.upper_bound(tick2);
2013-04-25 14:35:32 +02:00
for (; i1 != i2; ++i1) {
if (i1->second.type() == ME_CONTROLLER)
playEvent(i1->second, 0);
2013-04-25 14:35:32 +02:00
}
}
//---------------------------------------------------------
// curTempo
//---------------------------------------------------------
double Seq::curTempo() const
{
return cs->tempomap()->tempo(playPos->first);
}
//---------------------------------------------------------
// set Loop in position
//---------------------------------------------------------
void Seq::setLoopIn()
{
2013-08-19 08:53:30 +02:00
int tick;
if (state == Transport::PLAY) { // If in playback mode, set the In position where note is being played
auto ppos = playPos;
if (ppos != events.cbegin())
--ppos; // We have to go back one pos to get the correct note that has just been played
2013-09-12 15:57:14 +02:00
tick = cs->repeatList()->utick2tick(ppos->first);
}
2013-10-18 12:21:01 +02:00
else
tick = cs->pos(); // Otherwise, use the selected note.
if (tick >= cs->loopOutTick()) // If In pos >= Out pos, reset Out pos to end of score
2013-10-18 12:21:01 +02:00
cs->setPos(POS::RIGHT, cs->lastMeasure()->endTick() - 1);
cs->setPos(POS::LEFT, tick);
}
2013-08-19 08:53:30 +02:00
//---------------------------------------------------------
// set Loop Out position
//---------------------------------------------------------
void Seq::setLoopOut()
{
2013-08-19 08:53:30 +02:00
int tick;
if (state == Transport::PLAY) { // If in playback mode, set the Out position where note is being played
2013-09-12 15:57:14 +02:00
tick = cs->repeatList()->utick2tick(playPos->first);
}
2013-10-18 12:21:01 +02:00
else
2014-07-07 17:31:41 +02:00
tick = cs->pos() + cs->inputState().ticks(); // Otherwise, use the selected note.
if (tick <= cs->loopInTick()) // If Out pos <= In pos, reset In pos to beginning of score
2013-10-18 12:21:01 +02:00
cs->setPos(POS::LEFT, 0);
2014-09-22 04:55:58 +02:00
else
if (tick > cs->lastMeasure()->endTick() - 1)
tick = cs->lastMeasure()->endTick() - 1;
2013-10-18 12:21:01 +02:00
cs->setPos(POS::RIGHT, tick);
if (state == Transport::PLAY)
guiToSeq(SeqMsg(SeqMsgId::SEEK, tick));
}
2013-11-28 16:13:41 +01:00
void Seq::setPos(POS, unsigned tick)
{
2014-03-04 16:48:06 +01:00
qDebug("seq: setPos %d", tick);
2013-11-28 16:13:41 +01:00
}
//---------------------------------------------------------
// set Loop In/Out position based on the selection
//---------------------------------------------------------
void Seq::setLoopSelection()
{
cs->setLoopInTick(cs->selection().tickStart());
cs->setLoopOutTick(cs->selection().tickEnd());
2013-08-13 05:01:38 +02:00
}
2014-06-18 21:36:53 +02:00
//---------------------------------------------------------
// Called after tempo or time signature
// changed while playback
//---------------------------------------------------------
void Seq::handleTimeSigTempoChanged()
{
2014-06-18 21:36:53 +02:00
_driver->handleTimeSigTempoChanged();
}
2013-08-19 08:53:30 +02:00
}