Skip to content

Commit

Permalink
Cleaned up the code a bit. Removed a goto. :-)
Browse files Browse the repository at this point in the history
Removed Apple's copyright notice from OpenALSupport, which is only required when you distribute unmodified sample code.
  • Loading branch information
hollance committed Dec 22, 2012
1 parent 5c42e18 commit f9b0376
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 164 deletions.
24 changes: 17 additions & 7 deletions Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
1D60589F0D05DD5A006BFB54 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1D30AB110D05D00D00671497 /* Foundation.framework */; };
1DF5F4E00D08C38300B7A737 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1DF5F4DF0D08C38300B7A737 /* UIKit.framework */; };
288765A50DF7441C002DB57D /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 288765A40DF7441C002DB57D /* CoreGraphics.framework */; };
7B254DE41685DD1300B21EF9 /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 7B254DE31685DD1300B21EF9 /* Default-568h@2x.png */; };
7BAA29921212690D00BF4296 /* OpenAL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7BAA29911212690D00BF4296 /* OpenAL.framework */; };
7BAA29961212691700BF4296 /* AudioToolbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7BAA29951212691700BF4296 /* AudioToolbox.framework */; };
7BAA29DA12126BFA00BF4296 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7BAA29D912126BFA00BF4296 /* QuartzCore.framework */; };
Expand Down Expand Up @@ -47,6 +48,7 @@
1D6058910D05DD3D006BFB54 /* Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Demo.app; sourceTree = BUILT_PRODUCTS_DIR; };
1DF5F4DF0D08C38300B7A737 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
288765A40DF7441C002DB57D /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
7B254DE31685DD1300B21EF9 /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = "<group>"; };
7BAA29911212690D00BF4296 /* OpenAL.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenAL.framework; path = System/Library/Frameworks/OpenAL.framework; sourceTree = SDKROOT; };
7BAA29951212691700BF4296 /* AudioToolbox.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AudioToolbox.framework; path = System/Library/Frameworks/AudioToolbox.framework; sourceTree = SDKROOT; };
7BAA29D912126BFA00BF4296 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -135,6 +137,17 @@
name = Frameworks;
sourceTree = "<group>";
};
7B254DE01685DC2C00B21EF9 /* Supporting Files */ = {
isa = PBXGroup;
children = (
7B254DE31685DD1300B21EF9 /* Default-568h@2x.png */,
7BF55AE71595F5CE00324B00 /* Demo-Info.plist */,
7BF55AE81595F5CE00324B00 /* Demo_Prefix.pch */,
7BF55AEE1595F5CE00324B00 /* main.m */,
);
name = "Supporting Files";
sourceTree = "<group>";
};
7BACBAB91211DE8E005E8F00 /* Sounds */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -177,14 +190,12 @@
7BF55AE61595F5CE00324B00 /* Demo */ = {
isa = PBXGroup;
children = (
7BF55AE71595F5CE00324B00 /* Demo-Info.plist */,
7BF55AE81595F5CE00324B00 /* Demo_Prefix.pch */,
7BF55AE91595F5CE00324B00 /* DemoAppDelegate.h */,
7BF55AEA1595F5CE00324B00 /* DemoAppDelegate.m */,
7BF55AEB1595F5CE00324B00 /* DemoViewController.h */,
7BF55AEC1595F5CE00324B00 /* DemoViewController.m */,
7BF55AED1595F5CE00324B00 /* DemoViewController.xib */,
7BF55AEE1595F5CE00324B00 /* main.m */,
7B254DE01685DC2C00B21EF9 /* Supporting Files */,
);
path = Demo;
sourceTree = "<group>";
Expand Down Expand Up @@ -263,6 +274,7 @@
7BACBAE21211DE8E005E8F00 /* P200 Piano G#9.caf in Resources */,
7BACBAE31211DE8E005E8F00 /* Piano.plist in Resources */,
7BF55AF21595F5CE00324B00 /* DemoViewController.xib in Resources */,
7B254DE41685DD1300B21EF9 /* Default-568h@2x.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -296,7 +308,6 @@
GCC_PREFIX_HEADER = Demo/Demo_Prefix.pch;
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
INFOPLIST_FILE = "Demo/Demo-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
PRODUCT_NAME = Demo;
RUN_CLANG_STATIC_ANALYZER = YES;
WARNING_CFLAGS = (
Expand All @@ -317,7 +328,6 @@
GCC_PREFIX_HEADER = Demo/Demo_Prefix.pch;
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
INFOPLIST_FILE = "Demo/Demo-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
PRODUCT_NAME = Demo;
RUN_CLANG_STATIC_ANALYZER = YES;
VALIDATE_PRODUCT = YES;
Expand All @@ -337,7 +347,7 @@
GCC_C_LANGUAGE_STANDARD = c99;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 3.0;
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
SDKROOT = iphoneos;
};
name = Debug;
Expand All @@ -350,7 +360,7 @@
GCC_C_LANGUAGE_STANDARD = c99;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 3.0;
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1";
SDKROOT = iphoneos;
};
Expand Down
Binary file added Demo/Default-568h@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 0 additions & 3 deletions Demo/DemoAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

@implementation DemoAppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
Expand Down
34 changes: 10 additions & 24 deletions Demo/DemoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The sounds in this demo project were taken from Fluid R3 by Frank Wen,
// a freely distributable SoundFont.

#import <QuartzCore/CABase.h>
#import <QuartzCore/QuartzCore.h>
#import "DemoViewController.h"
#import "SoundBankPlayer.h"

Expand All @@ -13,8 +13,8 @@ @implementation DemoViewController
BOOL _playingArpeggio;
NSArray *_arpeggioNotes;
NSUInteger _arpeggioIndex;
double _arpeggioStartTime;
double _arpeggioDelay;
CFTimeInterval _arpeggioStartTime;
CFTimeInterval _arpeggioDelay;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
Expand Down Expand Up @@ -48,13 +48,7 @@ - (IBAction)strumCMajorChord

- (IBAction)arpeggiateCMajorChord
{
NSArray *notes = [NSArray arrayWithObjects:
[NSNumber numberWithInt:48],
[NSNumber numberWithInt:55],
[NSNumber numberWithInt:64],
nil];

[self playArpeggioWithNotes:notes delay:0.05];
[self playArpeggioWithNotes:@[@48, @55, @64] delay:0.05f];
}

- (IBAction)strumAMinorChord
Expand All @@ -68,18 +62,10 @@ - (IBAction)strumAMinorChord

- (IBAction)arpeggiateAMinorChord
{
NSArray *notes = [NSArray arrayWithObjects:
[NSNumber numberWithInt:33],
[NSNumber numberWithInt:45],
[NSNumber numberWithInt:52],
[NSNumber numberWithInt:60],
[NSNumber numberWithInt:67],
nil];

[self playArpeggioWithNotes:notes delay:0.1];
[self playArpeggioWithNotes:@[@33, @45, @52, @60, @67] delay:0.1f];
}

- (void)playArpeggioWithNotes:(NSArray *)notes delay:(double)delay
- (void)playArpeggioWithNotes:(NSArray *)notes delay:(CFTimeInterval)delay
{
if (!_playingArpeggio)
{
Expand All @@ -93,7 +79,7 @@ - (void)playArpeggioWithNotes:(NSArray *)notes delay:(double)delay

- (void)startTimer
{
_timer = [NSTimer scheduledTimerWithTimeInterval:0.05 // 50 ms
_timer = [NSTimer scheduledTimerWithTimeInterval:0.05f // 50 ms
target:self
selector:@selector(handleTimer:)
userInfo:nil
Expand All @@ -114,13 +100,13 @@ - (void)handleTimer:(NSTimer *)timer
if (_playingArpeggio)
{
// Play each note of the arpeggio after "arpeggioDelay" seconds.
double now = CACurrentMediaTime();
CFTimeInterval now = CACurrentMediaTime();
if (now - _arpeggioStartTime >= _arpeggioDelay)
{
NSNumber *number = (NSNumber *)[_arpeggioNotes objectAtIndex:_arpeggioIndex];
NSNumber *number = _arpeggioNotes[_arpeggioIndex];
[_soundBankPlayer noteOn:[number intValue] gain:0.4f];

++_arpeggioIndex;
_arpeggioIndex += 1;
if (_arpeggioIndex == [_arpeggioNotes count])
{
_playingArpeggio = NO;
Expand Down
4 changes: 2 additions & 2 deletions Demo/Demo_Prefix.pch
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

#import <Availability.h>

#ifndef __IPHONE_4_0
#warning "This project uses features only available in iOS SDK 4.0 and later."
#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif

#ifdef __OBJC__
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2008-2011 Matthijs Hollemans
Copyright (c) 2008-2012 Matthijs Hollemans

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@
This is a sample-based audio player that uses OpenAL. A "sound bank" can have multiple samples, each covering one or more notes. This allows you to implement a full instrument with only a few samples. It's like SoundFonts but simpler.

The sounds in this demo project were taken from Fluid R3 by Frank Wen, a freely distributable SoundFont.

Requires Xcode 4.3 or better.
78 changes: 17 additions & 61 deletions SoundBankPlayer/OpenALSupport.c
Original file line number Diff line number Diff line change
@@ -1,48 +1,3 @@
/*
(Modified from Apple's oalTouch sample code.)
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2010 Apple Inc. All Rights Reserved.
*/

#include "OpenALSupport.h"

Expand Down Expand Up @@ -71,14 +26,13 @@ ALvoid alcMacOSXMixerOutputRateProc(const ALdouble value)
void *GetOpenALAudioData(CFURLRef inFileURL, ALsizei *outDataSize, ALenum *outDataFormat, ALsizei *outSampleRate)
{
OSStatus err = noErr;
void *theData = NULL;

ExtAudioFileRef extRef = NULL;
err = ExtAudioFileOpenURL(inFileURL, &extRef);
if (err != noErr)
{
printf("GetOpenALAudioData: ExtAudioFileOpenURL FAILED, Error = %ld\n", err);
goto Exit;
return NULL;
}

AudioStreamBasicDescription theFileFormat;
Expand All @@ -87,13 +41,15 @@ void *GetOpenALAudioData(CFURLRef inFileURL, ALsizei *outDataSize, ALenum *outDa
if (err != noErr)
{
printf("GetOpenALAudioData: ExtAudioFileGetProperty(kExtAudioFileProperty_FileDataFormat) FAILED, Error = %ld\n", err);
goto Exit;
ExtAudioFileDispose(extRef);
return NULL;
}

if (theFileFormat.mChannelsPerFrame > 2)
{
printf("GetOpenALAudioData: Unsupported format, channel count is greater than stereo\n");
goto Exit;
ExtAudioFileDispose(extRef);
return NULL;
}

// Set the output format to 16 bit signed integer (native-endian) data
Expand All @@ -112,7 +68,8 @@ void *GetOpenALAudioData(CFURLRef inFileURL, ALsizei *outDataSize, ALenum *outDa
if (err != noErr)
{
printf("GetOpenALAudioData: ExtAudioFileSetProperty(kExtAudioFileProperty_ClientDataFormat) FAILED, Error = %ld\n", err);
goto Exit;
ExtAudioFileDispose(extRef);
return NULL;
}

SInt64 theFileLengthInFrames = 0;
Expand All @@ -121,15 +78,17 @@ void *GetOpenALAudioData(CFURLRef inFileURL, ALsizei *outDataSize, ALenum *outDa
if (err != noErr)
{
printf("GetOpenALAudioData: ExtAudioFileGetProperty(kExtAudioFileProperty_FileLengthFrames) FAILED, Error = %ld\n", err);
goto Exit;
ExtAudioFileDispose(extRef);
return NULL;
}

UInt32 dataSize = theFileLengthInFrames * theOutputFormat.mBytesPerFrame;
theData = malloc(dataSize);
void *theData = malloc(dataSize);
if (theData == NULL)
{
printf("GetOpenALAudioData: malloc FAILED\n");
goto Exit;
ExtAudioFileDispose(extRef);
return NULL;
}

AudioBufferList theDataBuffer;
Expand All @@ -141,19 +100,16 @@ void *GetOpenALAudioData(CFURLRef inFileURL, ALsizei *outDataSize, ALenum *outDa
err = ExtAudioFileRead(extRef, (UInt32 *)&theFileLengthInFrames, &theDataBuffer);
if (err != noErr)
{
free(theData);
theData = NULL; // make sure to return NULL
printf("GetOpenALAudioData: ExtAudioFileRead FAILED, Error = %ld\n", err);
goto Exit;
}
free(theData);
ExtAudioFileDispose(extRef);
return NULL;
}

*outDataSize = (ALsizei)dataSize;
*outDataFormat = (theOutputFormat.mChannelsPerFrame > 1) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16;
*outSampleRate = (ALsizei)theOutputFormat.mSampleRate;

Exit:
if (extRef != NULL)
ExtAudioFileDispose(extRef);

ExtAudioFileDispose(extRef);
return theData;
}
45 changes: 1 addition & 44 deletions SoundBankPlayer/OpenALSupport.h
Original file line number Diff line number Diff line change
@@ -1,48 +1,5 @@
/*

(Modified from Apple's oalTouch sample code.)
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2010 Apple Inc. All Rights Reserved.
*/
// Modified from Apple's oalTouch sample code.

#import <AudioToolbox/AudioToolbox.h>
#import <AudioToolbox/ExtendedAudioFile.h>
Expand Down
Loading

0 comments on commit f9b0376

Please sign in to comment.