mirror of
https://github.com/microtherion/VocalEasel.git
synced 2024-12-22 11:14:00 +00:00
Use MIDI Sequences
This commit is contained in:
parent
fe55af3f7c
commit
a80c82cb18
|
@ -15,17 +15,6 @@
|
|||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
const int kMidiChannelInUse = 0;
|
||||
|
||||
enum {
|
||||
kMidiMessage_ControlChange = 0xB,
|
||||
kMidiMessage_ProgramChange = 0xC,
|
||||
kMidiMessage_BankMSBControl = 0,
|
||||
kMidiMessage_BankLSBControl = 32,
|
||||
kMidiMessage_NoteOn = 0x9,
|
||||
kMidiMessage_NoteOff = 0x8
|
||||
};
|
||||
|
||||
class VLAUSoundOut : public VLSoundOut {
|
||||
public:
|
||||
VLAUSoundOut();
|
||||
|
@ -34,49 +23,22 @@ public:
|
|||
virtual void PlayChord(const VLChord & chord);
|
||||
|
||||
virtual ~VLAUSoundOut();
|
||||
|
||||
void Stop();
|
||||
private:
|
||||
AUGraph fGraph;
|
||||
AudioUnit fSynth;
|
||||
MusicPlayer fPlayer;
|
||||
MusicSequence fMusic;
|
||||
bool fRunning;
|
||||
|
||||
void Run();
|
||||
void Stop();
|
||||
void PlaySequence(MusicSequence music);
|
||||
void Play(const int8_t * note, size_t numNotes = 1);
|
||||
protected:
|
||||
friend class VLAUSoundEvent;
|
||||
|
||||
void Play(const int8_t * note, size_t numNotes,
|
||||
UInt32 msg, UInt32 velocity);
|
||||
};
|
||||
|
||||
VLSoundEvent::~VLSoundEvent()
|
||||
{
|
||||
}
|
||||
|
||||
class VLAUSoundEvent : public VLSoundEvent {
|
||||
public:
|
||||
VLAUSoundEvent(VLAUSoundOut * soundOut,
|
||||
const int8_t * note, size_t numNotes,
|
||||
UInt32 msg, UInt32 velocity)
|
||||
: fSoundOut(soundOut), fNotes(note, note+numNotes),
|
||||
fMsg(msg), fVelocity(velocity)
|
||||
{}
|
||||
|
||||
virtual void Perform();
|
||||
private:
|
||||
VLAUSoundOut * fSoundOut;
|
||||
std::vector<int8_t> fNotes;
|
||||
UInt32 fMsg;
|
||||
UInt32 fVelocity;
|
||||
};
|
||||
|
||||
void VLAUSoundEvent::Perform()
|
||||
{
|
||||
fSoundOut->Play(&fNotes[0], fNotes.size(), fMsg, fVelocity);
|
||||
|
||||
delete this;
|
||||
}
|
||||
|
||||
void VLSoundScheduler::Schedule(VLSoundEvent * what, float when)
|
||||
{
|
||||
usleep((int)(1000000.0f*when));
|
||||
|
@ -93,6 +55,12 @@ VLSoundOut * VLSoundOut::Instance()
|
|||
if (!sSoundScheduler.get())
|
||||
sSoundScheduler.reset(new VLSoundScheduler);
|
||||
}
|
||||
}
|
||||
|
||||
VLSoundOut * VLSoundOut::Instance()
|
||||
{
|
||||
if (!sSoundOut.get())
|
||||
sSoundOut.reset(new VLAUSoundOut);
|
||||
|
||||
return sSoundOut.get();
|
||||
}
|
||||
|
@ -107,7 +75,7 @@ VLSoundOut::~VLSoundOut()
|
|||
}
|
||||
|
||||
VLAUSoundOut::VLAUSoundOut()
|
||||
: fRunning(false)
|
||||
: fRunning(false), fMusic(0)
|
||||
{
|
||||
AUNode synthNode, limiterNode, outNode;
|
||||
ComponentDescription cd;
|
||||
|
@ -136,39 +104,42 @@ VLAUSoundOut::VLAUSoundOut()
|
|||
AUGraphOpen(fGraph);
|
||||
AUGraphConnectNodeInput(fGraph, synthNode, 0, limiterNode, 0);
|
||||
AUGraphConnectNodeInput(fGraph, limiterNode, 0, outNode, 0);
|
||||
AUGraphGetNodeInfo(fGraph, synthNode, 0, 0, 0, &fSynth);
|
||||
|
||||
AUGraphInitialize(fGraph);
|
||||
|
||||
MusicDeviceMIDIEvent(fSynth,
|
||||
kMidiMessage_ControlChange << 4 | kMidiChannelInUse,
|
||||
kMidiMessage_BankMSBControl, 0,
|
||||
0/*sample offset*/);
|
||||
MusicDeviceMIDIEvent(fSynth,
|
||||
kMidiMessage_ProgramChange << 4 | kMidiChannelInUse,
|
||||
0/*prog change num*/, 0,
|
||||
0/*sample offset*/);
|
||||
NewMusicPlayer(&fPlayer);
|
||||
}
|
||||
|
||||
VLAUSoundOut::~VLAUSoundOut()
|
||||
{
|
||||
DisposeMusicPlayer(fPlayer);
|
||||
Stop();
|
||||
DisposeAUGraph(fGraph);
|
||||
}
|
||||
|
||||
void VLAUSoundOut::Run()
|
||||
void VLAUSoundOut::PlaySequence(MusicSequence music)
|
||||
{
|
||||
if (!fRunning) {
|
||||
AUGraphStart(fGraph);
|
||||
Stop();
|
||||
|
||||
fMusic = music;
|
||||
|
||||
MusicSequenceSetAUGraph(fMusic, fGraph);
|
||||
MusicPlayerSetSequence(fPlayer, fMusic);
|
||||
MusicPlayerStart(fPlayer);
|
||||
|
||||
fRunning = true;
|
||||
}
|
||||
}
|
||||
|
||||
void VLAUSoundOut::Stop()
|
||||
{
|
||||
MusicPlayerStop(fPlayer);
|
||||
if (fRunning) {
|
||||
AUGraphStop(fGraph);
|
||||
fRunning = false;
|
||||
if (fMusic) {
|
||||
MusicPlayerSetSequence(fPlayer, NULL);
|
||||
DisposeMusicSequence(fMusic);
|
||||
fMusic = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -191,20 +162,17 @@ void VLAUSoundOut::PlayChord(const VLChord & chord)
|
|||
|
||||
void VLAUSoundOut::Play(const int8_t * note, size_t numNotes)
|
||||
{
|
||||
Run();
|
||||
MusicSequence music;
|
||||
MusicTrack track;
|
||||
|
||||
const UInt32 kNoteOn = kMidiMessage_NoteOn << 4 | kMidiChannelInUse;
|
||||
const UInt32 kNoteOff = kMidiMessage_NoteOff << 4 | kMidiChannelInUse;
|
||||
const UInt32 kNoteVelocity= 127;
|
||||
NewMusicSequence(&music);
|
||||
MusicSequenceNewTrack(music, &track);
|
||||
|
||||
Play(note, numNotes, kNoteOn, kNoteVelocity);
|
||||
sSoundScheduler.get()->Schedule(
|
||||
new VLAUSoundEvent(this, note, numNotes, kNoteOff, kNoteVelocity), 0.5f);
|
||||
}
|
||||
|
||||
void VLAUSoundOut::Play(const int8_t * note, size_t numNotes,
|
||||
UInt32 msg, UInt32 velocity)
|
||||
{
|
||||
for (size_t i = 0; i<numNotes; ++i)
|
||||
MusicDeviceMIDIEvent(fSynth, msg, note[i], velocity, 0);
|
||||
const int8_t kNoteVelocity = 127;
|
||||
for (int i=0; i<numNotes; ++i) {
|
||||
MIDINoteMessage n = {0, note[i], kNoteVelocity, 0, 1.0};
|
||||
MusicTrackNewMIDINoteEvent(track, 0.0, &n);
|
||||
}
|
||||
|
||||
PlaySequence(music);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user