Skip to content

Instantly share code, notes, and snippets.

@xhan
Created October 5, 2009 15:17
Show Gist options
  • Save xhan/202178 to your computer and use it in GitHub Desktop.
Save xhan/202178 to your computer and use it in GitHub Desktop.
a demo to show how to enable iPhone play MIDI
//
// PlayMidiDemoViewController.h
// PlayMidiDemo
//
// Created by xhan on 9/9/09.
// Copyright In-Blue 2009. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "fmod.hpp"
#import "fmod_errors.h"
@interface PlayMidiDemoViewController : UIViewController {
IBOutlet UILabel *time;
IBOutlet UILabel *status;
NSTimer *timer;
FMOD::System *system;
FMOD::Sound *sound1;
FMOD::Sound *sound2;
FMOD::Channel *channel;
}
@property (nonatomic, retain) UILabel *status;
@property (nonatomic, retain) UILabel *time;
- (IBAction)playSound1:(id)sender;
- (IBAction)playSound2:(id)sender;
- (void)timerUpdate:(NSTimer *)timer;
@end
//
// PlayMidiDemoViewController.m
// PlayMidiDemo
//
// Created by xhan on 9/9/09.
// Copyright In-Blue 2009. All rights reserved.
//
#import "PlayMidiDemoViewController.h"
@implementation PlayMidiDemoViewController
@synthesize status;
@synthesize time;
void ERRCHECK(FMOD_RESULT result)
{
if (result != FMOD_OK)
{
fprintf(stderr, "FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
exit(-1);
}
}
- (void)viewDidLoad {
[super viewDidLoad];
system = NULL;
sound1 = NULL;
sound2 = NULL;
channel = NULL;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[status release], status = nil;
[time release], time = nil;
[super dealloc];
}
- (void)viewWillAppear:(BOOL)animated
{
FMOD_RESULT result = FMOD_OK;
char buffer[200] = {0};
unsigned int version = 0;
/*
Create a System object and initialize
*/
result = FMOD::System_Create(&system);
ERRCHECK(result);
result = system->getVersion(&version);
ERRCHECK(result);
if (version < FMOD_VERSION)
{
fprintf(stderr, "You are using an old version of FMOD %08x. This program requires %08x\n", version, FMOD_VERSION);
exit(-1);
}
result = system->init(32, FMOD_INIT_NORMAL | FMOD_INIT_ENABLE_PROFILE, NULL);
ERRCHECK(result);
// set up DLS file
FMOD_CREATESOUNDEXINFO soundExInfo;
memset(&soundExInfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));
soundExInfo.cbsize = sizeof(FMOD_CREATESOUNDEXINFO);
char dlsName[200] = {0};
[[NSString stringWithFormat:@"%@/gm.dls", [[NSBundle mainBundle] resourcePath]] getCString:dlsName maxLength:200 encoding:NSASCIIStringEncoding];
soundExInfo.dlsname = dlsName;
// midi one
[[NSString stringWithFormat:@"%@/Bass_sample.mid", [[NSBundle mainBundle] resourcePath]] getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding];
result = system->createSound(buffer, FMOD_SOFTWARE | FMOD_CREATESTREAM, &soundExInfo, &sound1);
// ERRCHECK(result);
result = sound1->setMode(FMOD_LOOP_OFF);
// ERRCHECK(result);
// midi two
[[NSString stringWithFormat:@"%@/Drum_sample.mid", [[NSBundle mainBundle] resourcePath]] getCString:buffer maxLength:200 encoding:NSASCIIStringEncoding];
result = system->createSound(buffer, FMOD_SOFTWARE | FMOD_CREATESTREAM, &soundExInfo, &sound2);
result = sound2->setMode(FMOD_LOOP_OFF);
// timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(timerUpdate:) userInfo:nil repeats:YES];
}
- (IBAction)playSound1:(id)sender
{
FMOD_RESULT result = FMOD_OK;
result = system->playSound(FMOD_CHANNEL_FREE, sound1, false, &channel);
ERRCHECK(result);
}
- (IBAction)playSound2:(id)sender
{
FMOD_RESULT result = FMOD_OK;
result = system->playSound(FMOD_CHANNEL_FREE, sound2, false, &channel);
ERRCHECK(result);
}
- (void)timerUpdate:(NSTimer *)timer
{
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment