The third-party library generates sequential buffers of 16-bit signed stereo samples of any desired size. I can't figure out which framework/functions to use to play from these buffers. I've been working off the example in this answer using AudioQueue but it's obviously incomplete in ways I can't resolve (local variables are used as if they are object members, undeclared variables, etc).
The code in Apple's SpeakHere example project only shows how to use AudioQueue to play audio from a file. Can someone point me in the right direction?
Figured it out. Here's a simple, complete and working white noise generator using AudioQueue created from the Window-based Application project template.
StaticAppDelegate.h:
// StaticAppDelegate.h
#import <UIKit/UIKit.h>
// STATIC ADDITIONS
// Add > Existing Frameworks... > AudioToolbox.framework to your Target
#import <AudioToolbox/AudioToolbox.h>
// END STATIC ADDITIONS
#interface StaticAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
// STATIC ADDITIONS
- (void)startStatic;
- (void)handleBufferCompleteForQueue:(AudioQueueRef)inAQ
buffer:(AudioQueueBufferRef)inBuffer;
// END STATIC ADDITIONS
#end
StaticAppDelegate.m:
// StaticAppDelegate.m
#import "StaticAppDelegate.h"
// STATIC ADDITIONS
#define kNumberBuffers 4
#define kBufferSize 2048
// AudioQueue callback
void AQOutputCallback(
void *inData,
AudioQueueRef inAQ,
AudioQueueBufferRef inBuffer
)
{
StaticAppDelegate *staticApp = (StaticAppDelegate *)inData;
[staticApp handleBufferCompleteForQueue:inAQ buffer:inBuffer];
}
// END STATIC ADDITIONS
#implementation StaticAppDelegate
#synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
[window makeKeyAndVisible];
// STATIC ADDITIONS
[self startStatic];
// END STATIC ADDITIONS
}
- (void)dealloc {
[window release];
[super dealloc];
}
// STATIC ADDITIONS
- (void)startStatic
{
srandom(time(NULL));
// initiate audio session
AudioSessionInitialize(NULL, NULL, NULL, NULL);
UInt32 category = kAudioSessionCategory_MediaPlayback; // plays through sleep lock and silent switch
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
AudioSessionSetActive(true);
// following is modified from http://stackoverflow.com/questions/1710133/playing-generated-audio-on-an-iphone
// setup queue
AudioQueueRef audioQueue;
AudioQueueBufferRef buffers[kNumberBuffers];
AudioStreamBasicDescription format;
memset(&format, 0, sizeof(format));
format.mSampleRate = 44100;
format.mFormatID = kAudioFormatLinearPCM;
format.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
format.mChannelsPerFrame = 1;
format.mBitsPerChannel = 16;
format.mBytesPerFrame = (format.mBitsPerChannel / 8) * format.mChannelsPerFrame;
format.mFramesPerPacket = 1;
format.mBytesPerPacket = format.mBytesPerFrame * format.mFramesPerPacket;
AudioQueueNewOutput(&format,
AQOutputCallback,
self,
CFRunLoopGetCurrent(),
kCFRunLoopCommonModes,
0,
&audioQueue);
// allocate and fill the buffers
for (int i = 0; i < kNumberBuffers; ++i)
{
AudioQueueAllocateBuffer(audioQueue, kBufferSize, &buffers[i]);
AQOutputCallback(self, audioQueue, buffers[i]);
}
AudioQueueSetParameter(audioQueue, kAudioQueueParam_Volume, 1.0);
AudioQueueStart(audioQueue, NULL);
}
- (void)handleBufferCompleteForQueue:(AudioQueueRef)inAQ
buffer:(AudioQueueBufferRef)inBuffer
{
inBuffer->mAudioDataByteSize = inBuffer->mAudioDataBytesCapacity;
int *buffer = (int *)inBuffer->mAudioData;
for (int i = 0; i < inBuffer->mAudioDataByteSize/sizeof(int); ++i)
{
buffer[i] = (int)rand(); // refill the buffer
}
AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);
}
// END STATIC ADDITIONS
#end
Once I figured out how to actually refill the buffer (inBuffer->mAudioData with some type casting) everything else fell into place.
Related
Trying to set an int value using a Segmented Controller. I've seen several tuts on how to change labels, but I need to set an int value.
#import "SecondViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
#synthesize caseCost;
#synthesize dilution;
#synthesize returnMsg;
#synthesize opcValue;
//synthesize opc; < -- Tried
//int opc; <--- tried
- (IBAction)opcView:(id)sender {
if (opcValue.selectedSegmentIndex == 0) {
int opc = 320;
}
if (opcValue.selectedSegmentIndex == 1) {
int opc = 128;
}
if (opcValue.selectedSegmentIndex == 2) {
int opc = 135;
}
if (opcValue.selectedSegmentIndex == 3) {
int opc = 88;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//int opc; <--- tried
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)finishBtn:(id)sender {
//int opc = 320;
float case_cost = ([caseCost.text floatValue]);
float dilutionValue = ([dilution.text floatValue]);
float gpc = (opc / dilutionValue);
float gCost = (case_cost / gpc);
float bCost = (gCost / 4);
float bpc = (gpc * 4);
NSNumberFormatter *formatterCur = [[NSNumberFormatter alloc] init];
NSNumberFormatter *formatterInt = [[NSNumberFormatter alloc] init];
[formatterCur setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatterInt setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *bottlesCost = [formatterCur stringFromNumber:[NSNumber numberWithFloat:bCost]];
NSString *gallons = [formatterInt stringFromNumber:[NSNumber numberWithInt:gpc]];
NSString *gallonsCost = [formatterCur stringFromNumber:[NSNumber numberWithFloat:gCost]];
NSString *bottles = [formatterInt stringFromNumber:[NSNumber numberWithInt:bpc]];
returnMsg.text = [NSString stringWithFormat:#"%# gallons per case at %# per gallon and %# - 32 oz bottles at %# per bottle.", gallons, gallonsCost, bottles, bottlesCost];
}
- (IBAction)opcView:(id)sender {
}
#end
in the line "float gpc = (opc / dilutionValue);" is shows as an unknown value of opc, even though I think it should from the segmented controller. I'm using the segmented controller instead of Radio Buttons i've used in Java. I used the "//int opc=320" to make sure the rest of the code worked.
In each of the if blocks in your method - (IBAction)opcView:(id)sender you are creating a local int variable named opc. So when execution leaves the if block, the local variable disappears. Thus, in - (IBAction)finishBtn:(id)sender there is no variable named opc in scope.
You should declare opc to be a property as well. You will set this property when the segment control changes selection. Later, you can read the property's value in your finish button's handler.
#import "SecondViewController.h"
#interface SecondViewController()
#property (nonatomic) int opc;
#end
#implementation SecondViewController
// this method is wired to the segment control's UIControlEventValueChanged event
- (IBAction)opcView:(id)sender
{
if (opcValue.selectedSegmentIndex == 0) {
self.opc = 320;
}
if (opcValue.selectedSegmentIndex == 1) {
self.opc = 128;
}
if (opcValue.selectedSegmentIndex == 2) {
self.opc = 135;
}
if (opcValue.selectedSegmentIndex == 3) {
self.opc = 88;
}
}
- (IBAction)finishBtn:(id)sender
{
float case_cost = ([caseCost.text floatValue]);
float dilutionValue = ([dilution.text floatValue]);
float gpc = (self.opc / dilutionValue);
// lots more code
}
In my iPhone app, I am using structure and access it from Objective C UIViewController class, In one of my AudioInput.h file, I have the below:
typedef struct
{
AudioStreamBasicDescription format;
AudioQueueRef queue;
AudioQueueBufferRef buffers[ NUM_AUDIO_BUFFERS ];
SampleBuffer samples;
bool running;
bool done;
UInt32 playPos;
} AudioRecording;
In My ViewController MainViewController.m I implemented the AudioInputCallback() as below:
static void AudioInputCallback(
void *userData,
AudioQueueRef queue,
AudioQueueBufferRef buffer,
const AudioTimeStamp *startTime,
UInt32 numPackets,
const AudioStreamPacketDescription *packetDescriptions
)
{
// TRACE( "AudioInputCallback\n" );
MainViewController *controller = (MainViewController *) userData;
AudioRecording *audioRecording = & controller->audioRecording;
if( audioRecording->done )
{
TRACE( "AudioInputCallback: done\n" );
return;
}
if( numPackets == 0 && audioRecording->format.mBytesPerPacket > 0 )
{
numPackets = buffer->mAudioDataByteSize / audioRecording->format.mBytesPerPacket;
}
if( numPackets == 0 )
{
TRACE( "not packets received\n" );
return;
}
const SInt16 *inSamples = (const SInt16 *) buffer->mAudioData;
const UInt32 remain = audioRecording->samples.size - audioRecording->samples.count;
if( numPackets > remain )
{
numPackets = remain;
}
if( numPackets == 0 )
{
TRACE( "AudioInputCallback: no packets received\n" );
}
if( numPackets > 0 )
{
/* Save samples. */
if( audioRecording->samples.data )
{
UInt16 *in = (UInt16 *) inSamples;
UInt16 *out = (UInt16 *)( audioRecording->samples.data + audioRecording->samples.count );
for( UInt32 i = 0; i < numPackets; ++i )
{
*out ++ = ntohs( *in++ );
}
}
audioRecording->samples.count += numPackets;
}
if( audioRecording->samples.count < audioRecording->samples.size )
{
OSStatus result = AudioQueueEnqueueBuffer( queue, buffer, 0, NULL );
if( result )
{
TRACE( "AudioQueueEnqueueBuffer(..) failed with status %d\n", result );
}
}
else
{
TRACE( "AudioInputCallback: enough material, stop recording\n" );
audioRecording->done = true;
AudioQueueStop( audioRecording->queue, true );
}
[controller performSelectorOnMainThread:#selector(bufferFilled) withObject:nil waitUntilDone:NO];
}
In this method on the below line It gives error as "instance variable is protected"
MainViewController *controller = (MainViewController *) userData;
AudioRecording *audioRecording = & controller->audioRecording;
This error is occurs while running for iOS 5 with XCode 4.2 but the same code is running without any error building for below iOS 5 in XCode 4.0.2.
Can anyone help me why this error occurs? and your ideas to solve this is welcome!
It's nothing to do with your structure, it's to do with how you've defined the members of your UIViewController subclass. Take a look at this :
#interface MainViewController : UIViewController
{
#public
AudioRecording audioRecording1;
#protected // This is the default
AudioRecording audioRecording2;
#private
AudioRecording audioRecording3;
}
#end
Here, this controller has three AudioRecording members. However, only audiorecording1 can be access directly from outside the class. audiorecording3 cannot be accessed by anything other than the MainViewController and audiorecording2 can be accessed by MainViewController and any subclasses of it.
You need to make your audiorecording public if you want to access it directly without going though an accessor method (or a property).
I'm currently trying to create an iOS audio project and I need to use the CARingBuffer class available in the Extras/CoreAudio/PublicUtility folder of XCode.
The problem is when I include the CARingBuffer.h in the header of my viewController and I declare a CARingBuffer object, I receive 4 compile errors.
To reproduce my problem it's pretty simple. Just create a new view based application and try to #include "CARingBuffer.h" in the header of your viewController.
Here's the content of my testViewController.h :
#import <UIKit/UIKit.h>
#include "CARingBuffer.h"
#interface testViewController : UIViewController {
}
#end
Here's the content of my testViewController.m :
#import "testViewController.h"
#implementation testViewController
- (void)dealloc
{
[super dealloc];
}
- (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.
}
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Bellow the 4 compile errors located (strangely) in the CARingBuffer according to XCode 4 :
1) Initializer element is not a constant on line :
const UInt32 kGeneralRingTimeBoundsQueueMask = kGeneralRingTimeBoundsQueueSize - 1;
2) Expected ';' after top level declarator, Expected '='... or 'atribute' before 'CARingBuffer' :
class CARingBuffer {
3) Initializer element is not a constant on line :
const UInt32 kGeneralRingTimeBoundsQueueMask = kGeneralRingTimeBoundsQueueSize - 1;
4) Expected ';' after top level declarator, Expected '='... or 'atribute' before 'CARingBuffer' :
class CARingBuffer {
Thanks in advance for your help.
Also take a look at this alternative
You need to rename the class that you are including the ring buffer in to be a .mm file.
This tells the compiler to use objective c++ .
You need change your testViewController.m to testViewController.mm because CARingBuffer is c++ class.
About how to use it, here is an extend of CARingBuffer : CARingBufferEx
//header file
#include "CARingBuffer.h"
class CARingBufferEx : public CARingBuffer {
public:
CARingBufferEx();
~CARingBufferEx();
CARingBufferError Store(const AudioBufferList *abl, UInt32 nFrames, SampleTime frameNumber);
CARingBufferError Fetch(AudioBufferList *abl, UInt32 nFrames, SampleTime frameNumber);
private:
SInt64 firstInputSampleTime;
SInt64 firstOutputSampleTime;
SInt64 offset;
};
//Class
#include "CARingBufferEx.h"
#include "stdio.h"
CARingBufferEx::CARingBufferEx():firstInputSampleTime(-1), firstOutputSampleTime(-1), offset(0) {
}
CARingBufferEx::~CARingBufferEx() {
}
CARingBufferError CARingBufferEx::Store(const AudioBufferList *abl, UInt32 nFrames, SampleTime frameNumber) {
if (firstInputSampleTime < 0) {
firstInputSampleTime = frameNumber;
if (firstOutputSampleTime > 0 && offset == 0) {
offset = firstInputSampleTime - firstOutputSampleTime;
}
}
return CARingBuffer::Store(abl, nFrames, frameNumber);
}
CARingBufferError CARingBufferEx::Fetch(AudioBufferList *abl, UInt32 nFrames, SampleTime frameNumber) {
if (firstOutputSampleTime < 0) {
firstOutputSampleTime = frameNumber;
if (firstInputSampleTime > 0 && offset == 0) {
offset = firstInputSampleTime - firstOutputSampleTime;
}
}
return CARingBuffer::Fetch(abl, nFrames, frameNumber + offset);
}
Usage:
CARingBufferEx* _musicMixerRingBuffer;
_musicMixerRingBuffer = new CARingBufferEx();
_musicMixerRingBuffer->Allocate(2, sizeof(AudioUnitSampleType), 1024 * 50);
//1024 is length for one package. and 50 means this buffer contains 50 packages at most.
//store
//ioData is AudioBufferList ,inTimeStamp is AudioTimeStamp
musicMixerRingBuffer->Store(ioData, inNumberFrames, inTimeStamp->mSampleTime);
//Fetch
musicMixerRingBuffer->Fetch(ioData, inNumberFrames, inTimeStamp->mSampleTime);
I got some C++ Sourcecode that I would like to rewrite into Objective C.
It would help me alot if someone could write me a header file for this Code. When I get the Headerfile I would be able to rewrite the rest of the Sourcecode.
It would be very nice if someone could help me please.
Thanks
I will poste the sourcecode here:
#include <stdlib.h>
#include <iostream.h>
#define STATES 5
int transitionTable[STATES][STATES];
// function declarations:
double randfloat (void);
int chooseNextEventFromTable (int current, int table[STATES][STATES]);
int chooseNextEventFromTransitionTablee (int currentState);
void setTable (int value, int table[STATES][STATES]);
//////////////////////////////////////////////////////////////////////////
int main(void) {
int i;
// for demo purposes:
transitionTable[0][0] = 0;
transitionTable[0][1] = 20;
transitionTable[0][2] = 30;
transitionTable[0][3] = 50;
transitionTable[0][4] = 0;
transitionTable[1][0] = 35;
transitionTable[1][1] = 25;
transitionTable[1][2] = 20;
transitionTable[1][3] = 30;
transitionTable[1][4] = 0;
transitionTable[2][0] = 70;
transitionTable[2][1] = 0;
transitionTable[2][2] = 15;
transitionTable[2][3] = 0;
transitionTable[2][4] = 15;
transitionTable[3][0] = 0;
transitionTable[3][1] = 25;
transitionTable[3][2] = 25;
transitionTable[3][3] = 0;
transitionTable[3][4] = 50;
transitionTable[4][0] = 13;
transitionTable[4][1] = 17;
transitionTable[4][2] = 22;
transitionTable[4][3] = 48;
transitionTable[4][4] = 0;
int currentState = 0;
for (i=0; i<10; i++) {
std::cout << currentState << " ";
currentState = chooseNextEventFromTransitionTablee(currentState);
}
return 0;
};
//////////////////////////////////////////////////////////////////////////
//////////////////////////////
//
// chooseNextEventFromTransitionTable -- choose the next note.
//
int chooseNextEventFromTransitionTablee(int currentState) {
int targetSum = 0;
int sum = 0;
int targetNote = 0;
int totalevents = 0;
int i;
currentState = currentState % STATES; // remove any octave value
for (i=0; i<STATES; i++) {
totalevents += transitionTable[currentState][i];
}
targetSum = (int)(randfloat() * totalevents + 0.5);
while (targetNote < STATES &&
sum+transitionTable[currentState][targetNote] < targetSum) {
sum += transitionTable[currentState][targetNote];
targetNote++;
}
return targetNote;
}
//////////////////////////////
//
// randfloat -- returns a random number between 0.0 and 1.0.
//
double randfloat(void) {
return (double)rand()/RAND_MAX;
}
//////////////////////////////
//
// setTable -- set all values in the transition table to the given value.
//
void setTable(int value, int table[STATES][STATES]) {
int i, j;
for (i=0; i<STATES; i++) {
for (j=0; j<STATES; j++) {
table[i][j] = value;
}
}
}
Update
I'm not only compiling the header file there is another file i'm compiling too
SourceCode:
//
// markovThreadsChainsViewController.m
// markovThreadsChains
//
// Created by Philippe Mokrzycki on 15.01.11.
// Copyright 2011 TGM. All rights reserved.
//
#import "markovThreadsChainsViewController.h"
#import "markov.h"
//#import "markovChainOC.h"
#implementation markovThreadsChainsViewController
#synthesize mcValueLabel, threadStartGenerateButton, threadStopGenerateButton;
- (IBAction) startThreadGen:(UIButton *)sender{
threadStopGenerateButton.hidden=NO;
threadStartGenerateButton.hidden=YES;
mcValueLabel.text = #"0";
currentState=0;
// markovChainOC *mCobc = [[markovChainOC alloc]init];
// [mCobc setCurrentState:0];
[NSThread detachNewThreadSelector:#selector(startThreading) toTarget:self withObject:nil];
}
- (IBAction) stopThreadGen:(UIButton *)sender{
threadStopGenerateButton.hidden=YES;
threadStartGenerateButton.hidden=NO;
[NSThread detachNewThreadSelector:#selector(stopThreading) toTarget:self withObject:nil];
}
- (void) startThreading {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
[NSThread sleepForTimeInterval:3];
[self performSelectorOnMainThread:#selector(markovGen) withObject:nil waitUntilDone:NO];
[pool release];
}
- (void) stopThreading {
[NSThread cancelPreviousPerformRequestsWithTarget:self];
}
- (void)markovGen{
transitionTable[0][0] = 25;
transitionTable[0][1] = 25;
transitionTable[1][0] = 25;
transitionTable[1][1] = 25;
// int actualValue = [mCobc getCurrentState];
int actualValue = currentState;
mcValueLabel.text = [NSString stringWithFormat:#"%", actualValue];
currentState = chooseNextEventFromTransitionTablee(currentState);
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(markovGen) userInfo:nil repeats:NO];
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (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 {
[mcValueLabel release];
[threadStartGenerateButton release];
[threadStopGenerateButton release];
// [mCobc release];
[super dealloc];
}
#end
This code has nothing object-oriented in it at all. Just change iostream.h to stdio.h, and cout to printf. Then it's a regular C program.
Just put these lines into a header file, and you should be able to include the header and call the functions from any other Objective-C or C source file.
(To support C++ also, you may need to put extern "C" { ... } around them, unless you are compiling everything as C++ or Objective-C++.)
#pragma once
#define STATES 5
int transitionTable[STATES][STATES];
// function declarations:
double randfloat (void);
int chooseNextEventFromTable (int current, int table[STATES][STATES]);
int chooseNextEventFromTransitionTablee (int currentState);
void setTable (int value, int table[STATES][STATES]);
More detail:
To use these functions from another class, you'll need the following files in your project:
markov.h (or whatever you decide to call it), containing the lines above.
markov.c (or whatever you decide to call it), containing the other stuff from your original source file, except for the main function, which you should remove
Then, your other files that use the functions should have an #include "markov.h" line, and then you should be able to call them.
If you are getting linker errors about missing functions, it means that you are not compiling markov.c as part of the project, or there are some options that are causing the function names to not match properly.
I found some problems for std::list when I'm doing some iPhone development under Xcode. Here is the code:
///////////// interface //////////////
class CObj
{
public:
int value;
};
typedef std::list<CObj*> ObjList;
#interface testAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ObjList m_objs;
}
//////////// implementation /////////
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
for (int i = 0; i< 10; i++ )
{
CObj* obj = new CObj;
obj->value = i;
m_objs.push_back(obj);
}
NSLog(#"%d objects",m_objs.size() );
ObjList::iterator it = m_objs.begin();
while (it != m_objs.end())
{
CObj* obj = *it;
if ( obj->value == 3 )
it = m_objs.erase(it);
else
it++;
}
NSLog(#"%d objects",m_objs.size() );
}
The application simply crashes at m_objs.push_back. But if I change the std::list to std::vector, everything's fine. Any idea? Thanks
By default Objective-C does not call the constructor for c++ types on initialization.
If you set this flag in the build settings "Call C++ Default Ctors/Dtors in Objective-C", it will change the default behavior. Be aware that the flag only shows up if you have the target set to Device.