开发者

Music Scale : loop function to play different notes

I am making an iPhone app which includes tutorials on guitar scales. The goal is that I have many sounds that my function plays, until the scale is done.

Here is a random scale: F G A B C D E. I recorded sounds like C.mp3, D, E, F, G, A, B, C2, D2, E2... 2 being an higher note. Here is the code I use :

- (IBAction) playScale {

    NSLog(@"s: %i", s);
    NSLog(@"p: %i", pressed);
    [self.player prepareToPlay];

    components = [self.scale componentsSeparatedByString:@", "];
    ns_NoteToPlay = [components objectAtIndex:s];
    NSString *path = [[NSBundle mainBundle] pathForResource:ns_NoteToPlay ofType:@"m4a"];
    NSLog(@"Path : %@", path);
    self.player.delegate = self;
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    [self.player play];
    mChord.text = ns_NoteToPlay;

    NSLog(@"Nombre de notes ds Array : %i", components.count);

    if ( s == 0) { 
        int clean;
        clean = components.count + 1;
        [NSTi开发者_运维知识库mer scheduledTimerWithTimeInterval:clean target:self selector:@selector(cleanDots) userInfo:nil repeats:NO];
    }

    nsNowPlayingNote = ns_NoteToPlay;
    [self instrument];
    s = s+1;



    if ((s < components.count) && (pressed == 0)) {
        [self performSelector:@selector(playScale) withObject:nil afterDelay:0.8];    
    } 


}

This works. The function plays the scale, but I can't find a way to tell when to call the higher note C2. In consequence, when it plays D then C, C should be C2, but still calls C, which is lower.


You could keep around an array of NSStrings which contain the name of your files, something like:

[NSArray arrayWithObjects:@"C", @"D"..., @"C2", @"D2", nil]

And then you could keep an int variable around that you use to iterate through the array.

As a side note, are you sure AVAudioPlayer is the right choice to play your sounds? You should at least have a look into AudioServicesPlaySystemSound(), which is more appropriate to play short sounds.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜