Skip to content

Instantly share code, notes, and snippets.

@merf
Created January 21, 2012 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save merf/1652885 to your computer and use it in GitHub Desktop.
Save merf/1652885 to your computer and use it in GitHub Desktop.
MusicTrack Problem
#import "ViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AudioUnit/AudioUnit.h>
const int loop_length = 4;
@interface ViewController()
-(OSStatus) InitMIDI;
@property (assign) MusicTrack musicTrack;
@end
@implementation ViewController
@synthesize musicTrack = _musicTrack;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//[self BuildAUGraph];
[self InitMIDI];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
MusicTrackClear(_musicTrack, 0, loop_length);
MIDINoteMessage aMessage;
aMessage.channel = 1;
aMessage.velocity = 98;
aMessage.duration = 0.5f;
aMessage.note = 60;
for(int i=0; i<loop_length; ++i)
{
MusicTrackNewMIDINoteEvent(_musicTrack, i, &aMessage);
}
}
//**************************************************************************************************
-(OSStatus) InitMIDI
{
OSStatus result = noErr;
//create sequence
MusicSequence sequence;
result = NewMusicSequence(&sequence);
//create track
result = MusicSequenceNewTrack(sequence, &_musicTrack);
MIDINoteMessage aMessage;
aMessage.channel = 1;
aMessage.velocity = 98;
aMessage.duration = 0.5f;
aMessage.note = 60;
for(int i=0; i<loop_length; ++i)
{
MusicTrackNewMIDINoteEvent(_musicTrack, i, &aMessage);
}
MusicTrackLoopInfo loop_info;
loop_info.loopDuration = loop_length;
loop_info.numberOfLoops = 0;
result = MusicTrackSetProperty(_musicTrack, kSequenceTrackProperty_LoopInfo, &loop_info, sizeof(MusicTrackLoopInfo));
MusicTimeStamp track_length;
track_length = loop_length;
result = MusicTrackSetProperty(_musicTrack, kSequenceTrackProperty_TrackLength, &track_length, sizeof(MusicTimeStamp));
//create player
MusicPlayer player;
result = NewMusicPlayer(&player);
result = MusicPlayerSetSequence(player, sequence);
result = MusicPlayerStart(player);
return result;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment