Thanks a lot!!! That did it!

I really appreciate your help.


Brian


________________________________
From: [email protected] <[email protected]>
Sent: Wednesday, July 12, 2017 12:48 PM
To: Brian Hughes
Cc: [email protected]
Subject: Re: Using midi note on message to trigger and action


hey Brian.

as you correctly guessed your MIDI data is coming in on a thread other than 
main. so in your midi input block you should use
dispatch_async(get_main_dispatch_queue(),^{ weakSelf.doSomething();});

hope this helps

|K<
https://media-arts.w3rkhof.ch



Sent from my clumbsy thumbsy operating a tiny keyBird


On 12 Jul 2017, at 07:26, Brian Hughes 
<[email protected]<mailto:[email protected]>> wrote:


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 protected]<mailto:[email protected]>)
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/coreaudio-api/developer%40clelland.biz

This email sent to [email protected]<mailto:[email protected]>
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/coreaudio-api/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to