Using midi note on message to trigger and action
Using midi note on message to trigger and action
- Subject: Using midi note on message to trigger and action
- From: Brian Hughes <email@hidden>
- Date: Wed, 12 Jul 2017 05:26:28 +0000
- Thread-topic: Using midi note on message to trigger and action
I have a small os x application that teaches basic music reading in a game
format. Recently I decided to add midi capability to the application. That took
me a while but I have succeeded. The user can now interact with the application
via midi. Basically the user has to identify the correct pitch by playing the
the correct midi note on their midi controller.
However sometimes the user is asked a secondary Yes/No bonus questions related
to the correct selection of the pitch. The user must answer the question by
interacting with the button on screen.
I decided it would be nice if the user could use the midi controller to answer
the Yes/No questions. So I use any sharp or flat (black note) to trigger the No
button and any natural (white note) to trigger the Yes button.
All this works fine when I step through the program in debug but when I run the
program in real time it doesn't. After triggering the Yes or No buttons with
the midi controller the midi stops working. I have NSLogs attached to both the
note on and note off messages. Any time I use a midi controller to send a note
on or off message it prints to the console. But after I trigger the Yes/No
button no midi messages are generated indicating that my midi has died.
The Yes/No buttons basically do some GUI stuff, test to see if the answer is
correct, play appropriate background music, and increase or decrease the score
using a timer to increment the score with the music.
Also, the game is timed so the game also uses a timer for a count down clock.
However when the user selects the answer to a Yes/No question the timer for the
game clock is stopped until the background music and score increment is finished
When user sends a midi on message I use a performClick: to trigger the buttons.
[clefGamesYesButtonOutlet performClick:self];
[clefGamesNoButtonOutlet performClick: self];
The relevant code for the Yes buttons is: (the No Button is basically identical)
if ([clefGamesModel bonusQuestionComparePitch] == [clefGamesModel
bonusQuestionCurrentPitch]) //check to see if yes is the correct answer
{
if ([clefGamesModel gameTimeInSeconds_] > 0)
{
[gameTimer_ invalidate];
[gameTimer_ release];
[self setTimerIsStopped: YES];
}
crowdApplause = [NSSound soundNamed:@"crowdApplause.aif"];
[crowdApplause play];
dispatch_async(dispatch_get_main_queue(), ^{
bonusPointsTimer_ = [[NSTimer scheduledTimerWithTimeInterval: 0.13
target: self
selector:
@selector (addBonusPoints:)
userInfo: nil
repeats: YES]
retain];
});
}else //Yes is the incorrect answer
{
if ([clefGamesModel gameTimeInSeconds_] > 0)
{
[gameTimer_ invalidate];
[gameTimer_ release];
[self setTimerIsStopped: YES];
}
[crowdGroan play];
dispatch_async(dispatch_get_main_queue(), ^{
bonusPointsTimer_ = [[NSTimer scheduledTimerWithTimeInterval: 0.15
target: self
selector:
@selector (subtractBonusPoints:)
userInfo: nil
repeats: YES]
retain];
});
[clefGamesIsThisField setStringValue: NSLocalizedString (@"OOPS! You lose 25
points!", @"Text for incorrect answer to bonus question in the isThis Field")];
[clefGamesModel setBonusEvents_: ([clefGamesModel bonusEvents_] + 1)];
}
The addBonusPoint: and subtractBonusPoints: increment the score
-(void) addBonusPoints: (NSTimer *) aTimer
{
NSNotificationCenter *myNotificationCenter;
myNotificationCenter = [NSNotificationCenter defaultCenter];
[clefGamesModel setBonusPoints_: ([clefGamesModel bonusPoints_] + 1)];
[clefGamesModel setGameScore_: ([clefGamesModel gameScore_] + 1)];
[clefGamesModel setTotalScore_: ([clefGamesModel totalScore_] + 1)];
[myNotificationCenter postNotificationName: @"LNBonusPointsChanged"
object: self];
[clefGamesModel setBonusPointsTimerCounter: ([clefGamesModel
bonusPointsTimerCounter] + 1)];
[myNotificationCenter postNotificationName: @"LNBonusPointsTimerCounterChanged"
object: self];
}
The methods triggered by the notifications are
-(void) handleBonusPointsChanged: (NSNotification *) aNotification
{
NSString *scoreString = [NSString stringWithFormat: @"%d", [clefGamesModel
gameScore_]];
NSString *totalScoreString = [NSString stringWithFormat: @"%d", [clefGamesModel
totalScore_]];
[clefGamesScoreField setStringValue: scoreString];
[clefGamesTotalScoreField setStringValue: totalScoreString];
}
-(void) handleBonusPointsTimerCounterChanged: (NSNotification *) aNotification
{
if ([clefGamesModel bonusPointsTimerCounter] == BONUS_POINTS)
{
[bonusPointsTimer_ invalidate];
[bonusPointsTimer_ release];
[clefGamesModel setBonusPointsTimerCounter: 0];
}
}
I am stumped. I suspect it has something to do with threading, timers and midi
but I can't figure out what that is. Any help would be greatly appreciated.
Brian
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden