From ba01cbf1c0b6ec70740ff81287217eec3d635f71 Mon Sep 17 00:00:00 2001 From: Brian Whitman Date: Sun, 17 May 2015 16:17:04 -0400 Subject: [PATCH] Allowing user to set one note to the host instead of having to load a midi file --- src/AHHost.cpp | 9 ++++--- src/AHHost.h | 2 ++ src/AHMidiPlayer.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++ src/AHMidiPlayer.h | 2 ++ 4 files changed, 67 insertions(+), 4 deletions(-) diff --git a/src/AHHost.cpp b/src/AHHost.cpp index c83ccc2..4e648e9 100644 --- a/src/AHHost.cpp +++ b/src/AHHost.cpp @@ -77,6 +77,11 @@ void AHHost::LoadMidiFile(const std::string midiFile) midiPlayer_.LoadMidiFile(midiFile); } +void AHHost::CreateOneNote(int noteNumber, float duration, int velocity) +{ + midiPlayer_.CreateOneNote(noteNumber, duration, velocity); +} + // Most was copy-pasted from PlaySequence example void AHHost::Play() { @@ -335,15 +340,11 @@ vector< list< vector > > AHHost::RenderToBuffer()//CAStreamBasicDescripti { vector data_temp(bufferSize_); AudioBuffer ab = abl->mBuffers[i]; - // TODO : if my understanding is correct, since we assume that we are using the genericOutput, we also assume that 32-bit little-endian float, deinterleaved // are coming from AudioUnitRender memcpy(&(data_temp[0]), ab.mData, ab.mDataByteSize); data[i].push_back(data_temp); - //for(int j=0; j<10; j++) - // printf("%.5f ", data_temp[j]); - //printf("\n"); } midiPlayer_.GetTime( ¤tTime ); diff --git a/src/AHHost.h b/src/AHHost.h index 871a95d..9df1d83 100644 --- a/src/AHHost.h +++ b/src/AHHost.h @@ -53,6 +53,8 @@ class AHHost void LoadMidiFile(const string midiFile); + void CreateOneNote(int noteNumnber, float duration, int velocity); + // void LoadInstrument( const string& instrument, UInt32 busIndex = 0 ); vector< list< vector > > Bounce(); diff --git a/src/AHMidiPlayer.cpp b/src/AHMidiPlayer.cpp index 75e0a0c..60124cc 100644 --- a/src/AHMidiPlayer.cpp +++ b/src/AHMidiPlayer.cpp @@ -54,6 +54,64 @@ void AHMidiPlayer::Stop() PrintIfErr( MusicPlayerStop( musicPlayer_ ) ); } +void AHMidiPlayer::CreateOneNote(int noteNumber, float duration, int velocity) +{ + // new music stuff + MusicPlayer temp_musicPlayer; + MusicSequence temp_musicSequence; + PrintIfErr( NewMusicPlayer(&temp_musicPlayer)); + PrintIfErr( NewMusicSequence(&temp_musicSequence) ); + PrintIfErr( MusicPlayerSetSequence(temp_musicPlayer, temp_musicSequence) ); + + + // Disposing old music stuff + MusicTrack track; + UInt32 ntracks = GetTrackCount(); + for( UInt32 trackIndex = 0; trackIndex < ntracks; ++trackIndex ) + { + PrintIfErr( MusicSequenceGetIndTrack( musicSequence_, 0, &track ) );// 0 because we always delete the first one + PrintIfErr( MusicSequenceDisposeTrack(musicSequence_, track) ); + } + PrintIfErr( DisposeMusicPlayer(musicPlayer_) ); + PrintIfErr( DisposeMusicSequence(musicSequence_) ); + + // Create a MusicSequence with one track, one note called temp_musicSequence + PrintIfErr(MusicSequenceNewTrack(temp_musicSequence, &track)); + MusicTimeStamp beat = 1.0; + MIDINoteMessage mess; + mess.channel = 0; + mess.note = noteNumber; + mess.velocity = velocity; + mess.releaseVelocity = 0; + mess.duration = duration; + PrintIfErr(MusicTrackNewMIDINoteEvent(track, beat, &mess)); + + musicPlayer_ = temp_musicPlayer; + musicSequence_ = temp_musicSequence; + + PrintIfErr( MusicSequenceSetAUGraph(musicSequence_, graph_->GetAUGraph()) ); + + + // // figure out sequence length, and add 8 beats to the end of the file to take decay etc. into account + MusicTimeStamp trackLength; + UInt32 propsize = sizeof( MusicTimeStamp ); + + ntracks = GetTrackCount(); + //CAShow(musicSequence_); + musicSequenceLength_ = 0; + for( UInt32 trackIndex = 0; trackIndex < ntracks; ++trackIndex ) + { + PrintIfErr( MusicSequenceGetIndTrack( musicSequence_, trackIndex, &track ) ); + PrintIfErr( MusicTrackGetProperty( track, kSequenceTrackProperty_TrackLength, &trackLength, &propsize ) ); + + if ( trackLength > musicSequenceLength_ ) musicSequenceLength_ = trackLength; + } + musicSequenceLength_ += 8; + + SetupMidiChannelMapping(); + +} + void AHMidiPlayer::LoadMidiFile( const string& midiFilePath ) { // new music stuff diff --git a/src/AHMidiPlayer.h b/src/AHMidiPlayer.h index 2ee2750..1acd157 100644 --- a/src/AHMidiPlayer.h +++ b/src/AHMidiPlayer.h @@ -33,6 +33,8 @@ class AHMidiPlayer ~AHMidiPlayer(); void LoadMidiFile( const std::string& midiFile ); + void CreateOneNote(int noteNumber, float duration, int velocity); + void GetTime( MusicTimeStamp* currentTime ); MusicTimeStamp GetSequenceLength() { return musicSequenceLength_; } MusicSequence GetMusicSequence() { return musicSequence_; }