IOS: added audio output

This commit is contained in:
rock88 2013-03-16 21:50:55 +07:00 committed by Unknown W. Brackets
parent 05bdc2cd2f
commit 947f838165
4 changed files with 163 additions and 5 deletions

View File

@ -428,8 +428,10 @@ elseif(IOS)
ios/AppDelegate.m
ios/AppDelegate.h
ios/ViewController.mm
ios/ViewController.h)
set(nativeExtraLibs ${nativeExtraLibs} "-framework Foundation -framework AudioToolbox -framework CoreGraphics -framework QuartzCore -framework OpenGLES -framework UIKit -framework GLKit")
ios/ViewController.h
ios/AudioEngine.mm
ios/AudioEngine.h)
set(nativeExtraLibs ${nativeExtraLibs} "-framework Foundation -framework AudioToolbox -framework CoreGraphics -framework QuartzCore -framework OpenGLES -framework UIKit -framework GLKit -framework AudioToolbox -framework OpenAL")
set(TargetBin PPSSPP)
elseif(USING_QT_UI)
# Currently unused

13
ios/AudioEngine.h Normal file
View File

@ -0,0 +1,13 @@
//
// AudioEngine.h
// PPSSPP
//
// Created by rock88 on 15/03/2013.
// Copyright (c) 2013 Homebrew. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface AudioEngine : NSObject
@end

138
ios/AudioEngine.mm Normal file
View File

@ -0,0 +1,138 @@
//
// AudioEngine.mm
// PPSSPP
//
// Created by rock88 on 15/03/2013.
// Copyright (c) 2013 Homebrew. All rights reserved.
//
#import "AudioEngine.h"
#import <OpenAL/al.h>
#import <OpenAL/alc.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#import <string>
static volatile BOOL done = 0;
#define SAMPLE_SIZE 44100/6
static short stream[SAMPLE_SIZE];
void NativeMix(short *audio, int num_samples);
@interface AudioEngine ()
@property (nonatomic,assign) ALCdevice *alcDevice;
@property (nonatomic,assign) ALCcontext *alContext;
@property (nonatomic,assign) ALuint buffer;
@property (nonatomic,assign) ALuint source;
@end
@implementation AudioEngine
@synthesize alcDevice,alContext,buffer,source;
- (id)init
{
self = [super init];
if (self)
{
[self audioInit];
[self audioLoop];
}
return self;
}
- (void)dealloc
{
[self audioShutdown];
[super dealloc];
}
- (void)checkALError
{
ALenum ErrCode;
std::string Err = "OpenAL error: ";
if ((ErrCode = alGetError()) != AL_NO_ERROR)
{
Err += (char *)alGetString(ErrCode);
printf("%s\n",Err.c_str());
}
}
- (void)audioInit
{
done = 0;
alcDevice = alcOpenDevice(NULL);
if (alcDevice)
{
NSLog(@"OpenAL device opened: %s",alcGetString(alcDevice, ALC_DEVICE_SPECIFIER));
}
else
{
NSLog(@"WARNING: could not open OpenAL device");
return;
}
alContext = alcCreateContext(alcDevice, NULL);
if (alContext)
{
alcMakeContextCurrent(alContext);
}
else
{
NSLog(@"ERROR: no OpenAL context");
return;
}
alGenSources(1, &source);
alGenBuffers(1, &buffer);
}
- (void)audioShutdown
{
done = 1;
alcMakeContextCurrent(NULL);
if (alContext)
{
alcDestroyContext(alContext);
alContext = NULL;
}
if (alcDevice)
{
alcCloseDevice(alcDevice);
alcDevice = NULL;
}
}
- (bool)playing
{
ALenum state;
alGetSourcei(source, AL_SOURCE_STATE, &state);
return (state == AL_PLAYING);
}
- (void)audioLoop
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
while (!done)
{
if (![self playing])
{
NativeMix(stream, SAMPLE_SIZE/2);
alSourcei(source, AL_BUFFER, 0);
alBufferData(buffer, AL_FORMAT_STEREO16, stream, SAMPLE_SIZE, 44100);
alSourcei(source, AL_BUFFER, buffer);
alSourcePlay(source);
}
}
});
}
@end

View File

@ -6,6 +6,7 @@
//
#import "ViewController.h"
#import "AudioEngine.h"
#import <GLKit/GLKit.h>
#include "base/display.h"
@ -41,11 +42,12 @@ ViewController* sharedViewController;
@property (nonatomic,retain) NSString* documentsPath;
@property (nonatomic,retain) NSString* bundlePath;
@property (nonatomic,retain) NSMutableArray* touches;
@property (nonatomic,retain) AudioEngine* audioEngine;
@end
@implementation ViewController
@synthesize documentsPath,bundlePath,touches;
@synthesize documentsPath,bundlePath,touches,audioEngine;
- (id)init
{
@ -104,7 +106,9 @@ ViewController* sharedViewController;
dp_xscale = (float)dp_xres / (float)pixel_xres;
dp_yscale = (float)dp_yres / (float)pixel_yres;
if (g_Config.bEnableSound)
self.audioEngine = [[[AudioEngine alloc] init] autorelease];
/*
UISwipeGestureRecognizer* gesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)] autorelease];
[self.view addGestureRecognizer:gesture];
@ -129,7 +133,8 @@ ViewController* sharedViewController;
- (void)dealloc
{
[self viewDidUnload];
self.audioEngine = nil;
self.touches = nil;
self.documentsPath = nil;
self.bundlePath = nil;