Skip to content

Commit

Permalink
Play music during game.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanner committed Dec 17, 2013
1 parent 0762bfc commit 1595b1e
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 0 deletions.
10 changes: 10 additions & 0 deletions DaftMan.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
C3FE5584185FA50A00AE8A0E /* aroundtheworld.mid in Resources */ = {isa = PBXBuildFile; fileRef = C3FE5580185FA50A00AE8A0E /* aroundtheworld.mid */; };
C3FE5585185FA50A00AE8A0E /* dafunk.mid in Resources */ = {isa = PBXBuildFile; fileRef = C3FE5581185FA50A00AE8A0E /* dafunk.mid */; };
C3FE5586185FA50A00AE8A0E /* stronger.mid in Resources */ = {isa = PBXBuildFile; fileRef = C3FE5582185FA50A00AE8A0E /* stronger.mid */; };
C3FE558B185FC24C00AE8A0E /* AudioToolbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C3FE558A185FC24C00AE8A0E /* AudioToolbox.framework */; };
C3FE5594185FDA3000AE8A0E /* DMMusicPlayer.m in Sources */ = {isa = PBXBuildFile; fileRef = C3FE5593185FDA3000AE8A0E /* DMMusicPlayer.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -166,13 +168,17 @@
C3FE5580185FA50A00AE8A0E /* aroundtheworld.mid */ = {isa = PBXFileReference; lastKnownFileType = audio.midi; path = aroundtheworld.mid; sourceTree = "<group>"; };
C3FE5581185FA50A00AE8A0E /* dafunk.mid */ = {isa = PBXFileReference; lastKnownFileType = audio.midi; path = dafunk.mid; sourceTree = "<group>"; };
C3FE5582185FA50A00AE8A0E /* stronger.mid */ = {isa = PBXFileReference; lastKnownFileType = audio.midi; path = stronger.mid; sourceTree = "<group>"; };
C3FE558A185FC24C00AE8A0E /* AudioToolbox.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AudioToolbox.framework; path = System/Library/Frameworks/AudioToolbox.framework; sourceTree = SDKROOT; };
C3FE5592185FDA3000AE8A0E /* DMMusicPlayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DMMusicPlayer.h; sourceTree = "<group>"; };
C3FE5593185FDA3000AE8A0E /* DMMusicPlayer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DMMusicPlayer.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
C3D202D01831134E00B8C3CA /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
C3FE558B185FC24C00AE8A0E /* AudioToolbox.framework in Frameworks */,
C3D202D91831134E00B8C3CA /* SpriteKit.framework in Frameworks */,
C3D202D71831134E00B8C3CA /* Cocoa.framework in Frameworks */,
);
Expand Down Expand Up @@ -329,6 +335,7 @@
C3D202D51831134E00B8C3CA /* Frameworks */ = {
isa = PBXGroup;
children = (
C3FE558A185FC24C00AE8A0E /* AudioToolbox.framework */,
C3D202D61831134E00B8C3CA /* Cocoa.framework */,
C3D202D81831134E00B8C3CA /* SpriteKit.framework */,
C3D202FB1831134F00B8C3CA /* XCTest.framework */,
Expand Down Expand Up @@ -359,6 +366,8 @@
C3C27842185A6D68005CBADF /* DMDaftColor.m */,
C392671118577B1A00399109 /* NSString+DMFontPadding.h */,
C392671218577B1A00399109 /* NSString+DMFontPadding.m */,
C3FE5592185FDA3000AE8A0E /* DMMusicPlayer.h */,
C3FE5593185FDA3000AE8A0E /* DMMusicPlayer.m */,
C39267141857947D00399109 /* Assets */,
C3D202F41831134F00B8C3CA /* Images.xcassets */,
C3D202DF1831134E00B8C3CA /* Supporting Files */,
Expand Down Expand Up @@ -560,6 +569,7 @@
C38ADA8B183278C6006E960D /* DMMovingSprite.m in Sources */,
C392671318577B1A00399109 /* NSString+DMFontPadding.m in Sources */,
C38ADA7D183273A2006E960D /* DMBrick.m in Sources */,
C3FE5594185FDA3000AE8A0E /* DMMusicPlayer.m in Sources */,
C38ADA751831BCE5006E960D /* DMWall.m in Sources */,
C319175B1854B5E300C64A8D /* DMScoreBoard.m in Sources */,
C338324418353AAE0082F702 /* DMLevel.m in Sources */,
Expand Down
6 changes: 6 additions & 0 deletions DaftMan/DMEndScene.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "DMEndScene.h"

#import "NSString+DMFontPadding.h"
#import "DMMusicPlayer.h"

@implementation DMEndScene

Expand Down Expand Up @@ -112,6 +113,11 @@ - (id)initWithSize:(CGSize)size level:(int)aLevel score:(int)aScore time:(NSTime
lineThreeLabel.verticalAlignmentMode = SKLabelVerticalAlignmentModeTop;
lineThreeLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
}

NSString *midiFilePath = [[NSBundle mainBundle] pathForResource:@"stronger" ofType:@"mid"];
NSURL *midiFileURL = [NSURL fileURLWithPath:midiFilePath];

[[DMMusicPlayer sharedMusicPlayer] playSequence:midiFileURL beatsPerMinute:0.0];
}

return self;
Expand Down
6 changes: 6 additions & 0 deletions DaftMan/DMGameScene.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "DMGameScene.h"

#import "DMLevel.h"
#import "DMMusicPlayer.h"

@implementation DMGameScene

Expand All @@ -30,6 +31,11 @@ - (id)initWithSize:(CGSize)size level:(int)aLevel startingScore:(int)score deleg

[self addChild:level];
[self addChild:scoreBoard];

NSString *midiFilePath = [[NSBundle mainBundle] pathForResource:@"aroundtheworld" ofType:@"mid"];
NSURL *midiFileURL = [NSURL fileURLWithPath:midiFilePath];

[[DMMusicPlayer sharedMusicPlayer] playSequence:midiFileURL beatsPerMinute:120];
}

return self;
Expand Down
6 changes: 6 additions & 0 deletions DaftMan/DMMainMenuScene.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#import "NSString+DMFontPadding.h"
#import "DMDaftColor.h"
#import "DMMusicPlayer.h"

@implementation DMMainMenuScene

Expand Down Expand Up @@ -61,6 +62,11 @@ - (id)initWithSize:(CGSize)size {
copyrightLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;

[titleLabel runAction:[DMDaftColor actionForLabel:titleLabel]];

NSString *midiFilePath = [[NSBundle mainBundle] pathForResource:@"dafunk" ofType:@"mid"];
NSURL *midiFileURL = [NSURL fileURLWithPath:midiFilePath];

[[DMMusicPlayer sharedMusicPlayer] playSequence:midiFileURL beatsPerMinute:120];
}

return self;
Expand Down
22 changes: 22 additions & 0 deletions DaftMan/DMMusicPlayer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// DMMusicPlayer.h
// DaftMan
//
// Created by Tanner Smith on 12/16/13.
// Copyright (c) 2013 Tanner Smith. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>

@interface DMMusicPlayer : NSObject

@property (nonatomic, assign) MusicPlayer musicPlayer;
@property (nonatomic, assign) MusicSequence musicSequence;
@property (nonatomic, assign) MusicTrack tempoTrack;

+ (id)sharedMusicPlayer;

- (void)playSequence:(NSURL *)url beatsPerMinute:(double)bpm;

@end
86 changes: 86 additions & 0 deletions DaftMan/DMMusicPlayer.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//
// DMMusicPlayer.m
// DaftMan
//
// Created by Tanner Smith on 12/16/13.
// Copyright (c) 2013 Tanner Smith. All rights reserved.
//

#import "DMMusicPlayer.h"

@implementation DMMusicPlayer

@synthesize musicPlayer;
@synthesize musicSequence, tempoTrack;

+ (id)sharedMusicPlayer {
static dispatch_once_t onceToken;
static DMMusicPlayer *player;

dispatch_once(&onceToken, ^{
player = [[DMMusicPlayer alloc] init];
});

return player;
}

- (id)init {
if (self = [super init]) {
NewMusicPlayer(&musicPlayer);
}

return self;
}

- (void)playSequence:(NSURL *)url beatsPerMinute:(double)bpm {
if (musicSequence) {
[self stop];

DisposeMusicSequence(musicSequence);
}

if (NewMusicSequence(&musicSequence) != 0) {
[NSException raise:@"play" format:@"Can't create MusicSequence"];
}

if (MusicSequenceFileLoad(musicSequence, (__bridge CFURLRef) url, 0, 0) != 0) {
[NSException raise:@"play" format:@"Can't load MusicSequence"];
}

if (MusicSequenceGetTempoTrack(musicSequence, &tempoTrack) != noErr) {
[NSException raise:@"main" format:@"Cannot get tempo track."];
}

if (MusicTrackNewExtendedTempoEvent(tempoTrack, 0.0, 120) != noErr) {
[NSException raise:@"main" format:@"Cannot add tempo event."];
}

MusicPlayerSetSequence(musicPlayer, musicSequence);
MusicPlayerPreroll(musicPlayer);

[self start];
}

- (void)start {
if (musicPlayer) {
MusicPlayerStart(musicPlayer);
}
}

- (void)stop {
if (musicPlayer) {
MusicPlayerStop(musicPlayer);
}
}

- (void)dealloc {
[self stop];

if (musicSequence) {
DisposeMusicSequence(musicSequence);
}

DisposeMusicPlayer(musicPlayer);
}

@end

0 comments on commit 1595b1e

Please sign in to comment.