Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6025335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:17:09+00:00 2026-05-23T04:17:09+00:00

I am using Cocoa on Mac 10.6. I am trying to have a metronome

  • 0

I am using Cocoa on Mac 10.6. I am trying to have a metronome click out the time but when I run the code only the first two sounds are played then it is quiet till the end when a sound plays again. If I replace the [player play] statements with NSBeep() it works fine. Any suggestions?

#import <Cocoa/Cocoa.h>


@interface Metronome : NSObject {

NSInteger timeSig;
NSInteger noteValue;
NSInteger bpm;
NSUInteger beatNumber;
CGFloat duration;
NSSound *tickPlayer;
NSSound *tockPlayer;

}
@property(readwrite) NSInteger timeSig;
@property(readwrite) NSInteger noteValue;
@property(readwrite) NSInteger bpm;
@property(readwrite) float duration;
@property(readwrite) NSUInteger beatNumber;

-(id)init;
-(id)initWithTimeSig:(NSInteger)ts  andNoteValue:(NSInteger)nv andBpm:(NSInteger)bp;
-(void)BeginMetronome;
 -(void)PlaySound;

@end

#import "Metronome.h"

 #define defaultBpm 80
 #define defaultTimeSig 4
 #define defaultNoteValue 4

 @implementation Metronome

 @synthesize timeSig, noteValue, duration, bpm, beatNumber;
 -(id)init{
return [self initWithTimeSig:defaultTimeSig andNoteValue:defaultNoteValue       andBpm:defaultBpm];
 }
 -(id)initWithTimeSig:(NSInteger)ts andNoteValue:(NSInteger)nv andBpm:(NSInteger)bp {
[super init];
if(self){
    self.timeSig = ts;
    self.noteValue = nv;
    self.bpm = bp;
    self.duration = 60.0/bpm;
    tickPlayer = [NSSound soundNamed:@"Hat1"];
    tockPlayer = [NSSound soundNamed:@"Hat2"];
    self.beatNumber = 1;


}
return self;
 }
 -(void)BeginMetronome{
BOOL continuePlaying = YES;
NSInteger iter=0;

while (continuePlaying){
    if(iter > 12){
        continuePlaying = FALSE;
    }
    iter++;
    [self PlaySound];
    NSDate *curtainTime = [[NSDate alloc] initWithTimeIntervalSinceNow:self.duration];
    NSDate *currentTime = [[NSDate alloc] init];

    while (continuePlaying && ([currentTime compare:curtainTime] != NSOrderedDescending)) { 
        // if ([soundPlayerThread isCancelled] == YES) {
        //   continuePlaying = NO;
        //}
        //[NSThread sleepForTimeInterval:0.01];
        sleep(.01);
        //[tickPlayer stop];
        //[tockPlayer stop];
        [currentTime release];
        currentTime = [[NSDate alloc] init];
    }
    [curtainTime release];      
    [currentTime release];      

}
}
  - (void)PlaySound {          //***********Problem in this loop
if ([tickPlayer isPlaying])
    [tickPlayer stop];
if ([tockPlayer isPlaying])
    [tockPlayer stop];

if (beatNumber == 1) {
    [tockPlayer play];
}
else if (beatNumber == 2){
    [tickPlayer play];
}
else if (beatNumber == self.timeSig) {
    beatNumber = 0;
    [tickPlayer play];
}
else{
    //[tickPlayer stop];
    [tickPlayer play];
    NSBeep();
}


beatNumber++;
//NSLog(@" %d ", beatNumber);
 // NSBeep();
 }



 @end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:17:10+00:00Added an answer on May 23, 2026 at 4:17 am

    I believe your use of sleep() is blocking the playing of NSSound. That’s a very sloppy way to do this, instead use a timer like so:

    static NSInteger iter;
    
    - (void) beginMetronome {
    
        iter = 0;
    
        NSTimer *timer = [NSTimer timerWithTimeInterval:self.duration target:self selector:@selector(continueMetronome:) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
        [timer fire];    
    }
    
    - (void) continueMetronome:(NSTimer *)theTimer {
    
        [self PlaySound];
    
        if (++iter > 12) {
    
            [theTimer invalidate];
        }
    }
    

    Also, you must retain the sounds that you plan to use in your init method.

    tickPlayer = [[NSSound soundNamed:@"Hat1"] retain];
    tockPlayer = [[NSSound soundNamed:@"Hat2"] retain];
    

    And Cocoa methods should start with a lowercase letter like I’ve written them. I’m not too sure how metronomes are supposed to work, so you may have to change the value of 12 to something else.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying my hand at Mac Cocoa for the first time. Lion and XCode 4.1.
Using foundation and cocoa frameworks on Mac, I am trying to convert an NSData
I have never made an app using XCode and Cocoa but I think following
I am using Mac OsX 10.6.6. I had downloaded ws4d-explorer-v3.1-cocoa-macosx-x86_64.jar. The when i run
I'm trying to get my Gmail unread email count using Cocoa (Mac) and the
I am an iOS developer and did my first Cocoa Mac application using Xcode
I am a newbie for MAC development, I have a simple Cocoa Application using
I am using the most stable SWT Cocoa downloads for Mac OS X and
I'm using cocoa JSON framework http://code.google.com/p/json-framework/ Here is the result of an JSON output
I tried using juggernaut cocoa: https://github.com/fpotter/juggernaut-cocoa Added this line to my code: JuggernautClient *client

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.