Skip to content

Commit aa48d72

Browse files
author
Darcy Liu
committed
add oalTouch
1 parent 4a7a427 commit aa48d72

33 files changed

+2307
-1
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,4 +1212,12 @@ Demonstrates how to implement and customize the 2 UIView properties of UITableVi
12121212

12131213
[URL](https://developer.apple.com/library/ios/#samplecode/HeaderFooter/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007989)
12141214

1215-
Last Revision: Version 1.3, 2010-06-29
1215+
Last Revision: Version 1.3, 2010-06-29
1216+
1217+
#oalTouch#
1218+
1219+
The code uses OpenAL to play a single audio source. Move source or listener position by dragging icons around on the grid. Turn accelerometer functionality on to set listener orientation by tilting the device.
1220+
1221+
[URL](https://developer.apple.com/library/ios/#samplecode/oalTouch/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007769)
1222+
1223+
Last Revision: Version 1.9, 2010-06-29

oalTouch.zip

1.51 MB
Binary file not shown.

oalTouch/Classes/MyOpenALSupport.c

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
3+
File: MyOpenALSupport.c
4+
Abstract: OpenAL-related support functions
5+
Version: 1.9
6+
7+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
8+
Inc. ("Apple") in consideration of your agreement to the following
9+
terms, and your use, installation, modification or redistribution of
10+
this Apple software constitutes acceptance of these terms. If you do
11+
not agree with these terms, please do not use, install, modify or
12+
redistribute this Apple software.
13+
14+
In consideration of your agreement to abide by the following terms, and
15+
subject to these terms, Apple grants you a personal, non-exclusive
16+
license, under Apple's copyrights in this original Apple software (the
17+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
18+
Software, with or without modifications, in source and/or binary forms;
19+
provided that if you redistribute the Apple Software in its entirety and
20+
without modifications, you must retain this notice and the following
21+
text and disclaimers in all such redistributions of the Apple Software.
22+
Neither the name, trademarks, service marks or logos of Apple Inc. may
23+
be used to endorse or promote products derived from the Apple Software
24+
without specific prior written permission from Apple. Except as
25+
expressly stated in this notice, no other rights or licenses, express or
26+
implied, are granted by Apple herein, including but not limited to any
27+
patent rights that may be infringed by your derivative works or by other
28+
works in which the Apple Software may be incorporated.
29+
30+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
31+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
32+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
33+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
34+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
35+
36+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
37+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
40+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
41+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
42+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
43+
POSSIBILITY OF SUCH DAMAGE.
44+
45+
Copyright (C) 2010 Apple Inc. All Rights Reserved.
46+
47+
48+
*/
49+
50+
#include "MyOpenALSupport.h"
51+
52+
ALvoid alBufferDataStaticProc(const ALint bid, ALenum format, ALvoid* data, ALsizei size, ALsizei freq)
53+
{
54+
static alBufferDataStaticProcPtr proc = NULL;
55+
56+
if (proc == NULL) {
57+
proc = (alBufferDataStaticProcPtr) alcGetProcAddress(NULL, (const ALCchar*) "alBufferDataStatic");
58+
}
59+
60+
if (proc)
61+
proc(bid, format, data, size, freq);
62+
63+
return;
64+
}
65+
66+
void* MyGetOpenALAudioData(CFURLRef inFileURL, ALsizei *outDataSize, ALenum *outDataFormat, ALsizei* outSampleRate)
67+
{
68+
OSStatus err = noErr;
69+
SInt64 theFileLengthInFrames = 0;
70+
AudioStreamBasicDescription theFileFormat;
71+
UInt32 thePropertySize = sizeof(theFileFormat);
72+
ExtAudioFileRef extRef = NULL;
73+
void* theData = NULL;
74+
AudioStreamBasicDescription theOutputFormat;
75+
76+
// Open a file with ExtAudioFileOpen()
77+
err = ExtAudioFileOpenURL(inFileURL, &extRef);
78+
if(err) { printf("MyGetOpenALAudioData: ExtAudioFileOpenURL FAILED, Error = %ld\n", err); goto Exit; }
79+
80+
// Get the audio data format
81+
err = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_FileDataFormat, &thePropertySize, &theFileFormat);
82+
if(err) { printf("MyGetOpenALAudioData: ExtAudioFileGetProperty(kExtAudioFileProperty_FileDataFormat) FAILED, Error = %ld\n", err); goto Exit; }
83+
if (theFileFormat.mChannelsPerFrame > 2) { printf("MyGetOpenALAudioData - Unsupported Format, channel count is greater than stereo\n"); goto Exit;}
84+
85+
// Set the client format to 16 bit signed integer (native-endian) data
86+
// Maintain the channel count and sample rate of the original source format
87+
theOutputFormat.mSampleRate = theFileFormat.mSampleRate;
88+
theOutputFormat.mChannelsPerFrame = theFileFormat.mChannelsPerFrame;
89+
90+
theOutputFormat.mFormatID = kAudioFormatLinearPCM;
91+
theOutputFormat.mBytesPerPacket = 2 * theOutputFormat.mChannelsPerFrame;
92+
theOutputFormat.mFramesPerPacket = 1;
93+
theOutputFormat.mBytesPerFrame = 2 * theOutputFormat.mChannelsPerFrame;
94+
theOutputFormat.mBitsPerChannel = 16;
95+
theOutputFormat.mFormatFlags = kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger;
96+
97+
// Set the desired client (output) data format
98+
err = ExtAudioFileSetProperty(extRef, kExtAudioFileProperty_ClientDataFormat, sizeof(theOutputFormat), &theOutputFormat);
99+
if(err) { printf("MyGetOpenALAudioData: ExtAudioFileSetProperty(kExtAudioFileProperty_ClientDataFormat) FAILED, Error = %ld\n", err); goto Exit; }
100+
101+
// Get the total frame count
102+
thePropertySize = sizeof(theFileLengthInFrames);
103+
err = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_FileLengthFrames, &thePropertySize, &theFileLengthInFrames);
104+
if(err) { printf("MyGetOpenALAudioData: ExtAudioFileGetProperty(kExtAudioFileProperty_FileLengthFrames) FAILED, Error = %ld\n", err); goto Exit; }
105+
106+
// Read all the data into memory
107+
UInt32 dataSize = theFileLengthInFrames * theOutputFormat.mBytesPerFrame;;
108+
theData = malloc(dataSize);
109+
if (theData)
110+
{
111+
AudioBufferList theDataBuffer;
112+
theDataBuffer.mNumberBuffers = 1;
113+
theDataBuffer.mBuffers[0].mDataByteSize = dataSize;
114+
theDataBuffer.mBuffers[0].mNumberChannels = theOutputFormat.mChannelsPerFrame;
115+
theDataBuffer.mBuffers[0].mData = theData;
116+
117+
// Read the data into an AudioBufferList
118+
err = ExtAudioFileRead(extRef, (UInt32*)&theFileLengthInFrames, &theDataBuffer);
119+
if(err == noErr)
120+
{
121+
// success
122+
*outDataSize = (ALsizei)dataSize;
123+
*outDataFormat = (theOutputFormat.mChannelsPerFrame > 1) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16;
124+
*outSampleRate = (ALsizei)theOutputFormat.mSampleRate;
125+
}
126+
else
127+
{
128+
// failure
129+
free (theData);
130+
theData = NULL; // make sure to return NULL
131+
printf("MyGetOpenALAudioData: ExtAudioFileRead FAILED, Error = %ld\n", err); goto Exit;
132+
}
133+
}
134+
135+
Exit:
136+
// Dispose the ExtAudioFileRef, it is no longer needed
137+
if (extRef) ExtAudioFileDispose(extRef);
138+
return theData;
139+
}
140+

oalTouch/Classes/MyOpenALSupport.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
3+
File: MyOpenALSupport.h
4+
Abstract: OpenAL-related support functions
5+
Version: 1.9
6+
7+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
8+
Inc. ("Apple") in consideration of your agreement to the following
9+
terms, and your use, installation, modification or redistribution of
10+
this Apple software constitutes acceptance of these terms. If you do
11+
not agree with these terms, please do not use, install, modify or
12+
redistribute this Apple software.
13+
14+
In consideration of your agreement to abide by the following terms, and
15+
subject to these terms, Apple grants you a personal, non-exclusive
16+
license, under Apple's copyrights in this original Apple software (the
17+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
18+
Software, with or without modifications, in source and/or binary forms;
19+
provided that if you redistribute the Apple Software in its entirety and
20+
without modifications, you must retain this notice and the following
21+
text and disclaimers in all such redistributions of the Apple Software.
22+
Neither the name, trademarks, service marks or logos of Apple Inc. may
23+
be used to endorse or promote products derived from the Apple Software
24+
without specific prior written permission from Apple. Except as
25+
expressly stated in this notice, no other rights or licenses, express or
26+
implied, are granted by Apple herein, including but not limited to any
27+
patent rights that may be infringed by your derivative works or by other
28+
works in which the Apple Software may be incorporated.
29+
30+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
31+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
32+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
33+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
34+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
35+
36+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
37+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
40+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
41+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
42+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
43+
POSSIBILITY OF SUCH DAMAGE.
44+
45+
Copyright (C) 2010 Apple Inc. All Rights Reserved.
46+
47+
48+
*/
49+
50+
#import <OpenAL/al.h>
51+
#import <OpenAL/alc.h>
52+
#import <AudioToolbox/AudioToolbox.h>
53+
#import <AudioToolbox/ExtendedAudioFile.h>
54+
55+
typedef ALvoid AL_APIENTRY (*alBufferDataStaticProcPtr) (const ALint bid, ALenum format, ALvoid* data, ALsizei size, ALsizei freq);
56+
ALvoid alBufferDataStaticProc(const ALint bid, ALenum format, ALvoid* data, ALsizei size, ALsizei freq);
57+
58+
void* MyGetOpenALAudioData(CFURLRef inFileURL, ALsizei *outDataSize, ALenum *outDataFormat, ALsizei* outSampleRate);
59+
60+

oalTouch/Classes/oalPlayback.h

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
3+
File: oalPlayback.h
4+
Abstract: An Obj-C class which wraps an OpenAL playback environment
5+
Version: 1.9
6+
7+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
8+
Inc. ("Apple") in consideration of your agreement to the following
9+
terms, and your use, installation, modification or redistribution of
10+
this Apple software constitutes acceptance of these terms. If you do
11+
not agree with these terms, please do not use, install, modify or
12+
redistribute this Apple software.
13+
14+
In consideration of your agreement to abide by the following terms, and
15+
subject to these terms, Apple grants you a personal, non-exclusive
16+
license, under Apple's copyrights in this original Apple software (the
17+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
18+
Software, with or without modifications, in source and/or binary forms;
19+
provided that if you redistribute the Apple Software in its entirety and
20+
without modifications, you must retain this notice and the following
21+
text and disclaimers in all such redistributions of the Apple Software.
22+
Neither the name, trademarks, service marks or logos of Apple Inc. may
23+
be used to endorse or promote products derived from the Apple Software
24+
without specific prior written permission from Apple. Except as
25+
expressly stated in this notice, no other rights or licenses, express or
26+
implied, are granted by Apple herein, including but not limited to any
27+
patent rights that may be infringed by your derivative works or by other
28+
works in which the Apple Software may be incorporated.
29+
30+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
31+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
32+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
33+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
34+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
35+
36+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
37+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
40+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
41+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
42+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
43+
POSSIBILITY OF SUCH DAMAGE.
44+
45+
Copyright (C) 2010 Apple Inc. All Rights Reserved.
46+
47+
48+
*/
49+
50+
#import <UIKit/UIKit.h>
51+
#import <AVFoundation/AVFoundation.h>
52+
53+
#import <OpenAL/al.h>
54+
#import <OpenAL/alc.h>
55+
56+
#define kDefaultDistance 25.0
57+
58+
@interface oalPlayback : NSObject
59+
{
60+
IBOutlet UISwitch* musicSwitch;
61+
62+
ALuint source;
63+
ALuint buffer;
64+
ALCcontext* context;
65+
ALCdevice* device;
66+
67+
void* data;
68+
CGPoint sourcePos;
69+
CGPoint listenerPos;
70+
CGFloat listenerRotation;
71+
ALfloat sourceVolume;
72+
BOOL isPlaying;
73+
BOOL wasInterrupted;
74+
75+
NSURL* bgURL;
76+
AVAudioPlayer* bgPlayer;
77+
UInt32 iPodIsPlaying;
78+
79+
}
80+
81+
@property (nonatomic, assign) BOOL isPlaying; // Whether the sound is playing or stopped
82+
@property (nonatomic, assign) UInt32 iPodIsPlaying; // Whether the iPod is playing
83+
@property (nonatomic, assign) BOOL wasInterrupted; // Whether playback was interrupted by the system
84+
@property (nonatomic, assign) CGPoint sourcePos; // The coordinates of the sound source
85+
@property (nonatomic, assign) CGPoint listenerPos; // The coordinates of the listener
86+
@property (nonatomic, assign) CGFloat listenerRotation; // The rotation angle of the listener in radians
87+
88+
- (IBAction)toggleMusic:(UISwitch*)sender;
89+
- (void)checkForMusic;
90+
91+
- (void)initOpenAL;
92+
- (void)teardownOpenAL;
93+
94+
- (void)startSound;
95+
- (void)stopSound;
96+
97+
@end

0 commit comments

Comments
 (0)