I'm having no luck loading a Sound Font file (.SF2) in my IOS app. I initially tried using Apple's code from Tech note TN2283
- (OSStatus) loadFromDLSOrSoundFont: (NSURL *)bankURL withPatch: (int)presetNumber {
OSStatus result = noErr;
// fill out a instrument data structure
AUSamplerInstrumentData instdata;
instdata.bankURL = (CFURLRef) bankURL;
instdata.instrumentType = kInstrumentType_DLSPreset;
instdata.bankMSB = kAUSampler_DefaultMelodicBankMSB;
instdata.bankLSB = kAUSampler_DefaultBankLSB;
instdata.presetID = (UInt8) presetNumber;
// set the kAUSamplerProperty_LoadPresetFromBank property
result = AudioUnitSetProperty(self.mySamplerUnit,
kAUSamplerProperty_LoadInstrument,
kAudioUnitScope_Global,
0,
&instdata,
sizeof(instdata));
// check for errors
NSCAssert (result == noErr,
#"Unable to set the preset property on the Sampler. Error code:%d '%.4s'",
(int) result,
(const char *)&result);
return result; }
But the compiler complains that 'No member named 'bankURL' in struct AUSamplerInstrumentData' which is true, the struct does not contain a 'bankURL' member??
I then came across the following code, by Apple I believe
- (OSStatus)loadSoundFont:(NSURL *)bankURL withPatch:(int)presetNumber
{
OSStatus result = noErr;
// fill out a bank preset data structure
AUSamplerBankPresetData bpdata;
bpdata.bankURL = (__bridge CFURLRef) bankURL;
bpdata.bankMSB = kAUSampler_DefaultMelodicBankMSB;
bpdata.bankLSB = kAUSampler_DefaultBankLSB;
bpdata.presetID = (UInt8) presetNumber;
// set the kAUSamplerProperty_LoadPresetFromBank property
result = AudioUnitSetProperty(self.samplerUnit,
kAUSamplerProperty_LoadPresetFromBank,
kAudioUnitScope_Global,
0,
&bpdata,
sizeof(bpdata));
// check for errors
NSCAssert (result == noErr,
#"Unable to set the preset property on the Sampler. Error code:%d '%.4s'",
(int) result,
(const char *)&result);
return result;
}
This all looks correct but when I attempt to load a sound font using this method such as follows
NSURL *SFURL = [[NSBundle mainBundle] URLForResource:#"YAMAHA DX7Piano" withExtension:#"SF2"];
[self loadSoundFont:url withPatch:0];
it throws the error "Unable to set the preset property on the Sampler.." This did lead me to think there was some error in how I specified the patch number, such as supplying a non-existent patch number. But I did eventually discover that the NSURL I was supplying was null, so I tried specifying the url as follows:
NSString *resources = [[NSBundle mainBundle] resourcePath];
NSURL *SFURL = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/%#",resources,#"YAMAHA DX7Piano.SF2"]];
This has got me a step closer. I think I am now supplying a valid url to the sound font file in my app bundle. But it still is not working. My compiler now tells me
ERROR: [0x19a824310] 486: DLS/SF2 bank load failed
There is a piece of the puzzle missing and I can't see what.??
Well I found the solution. The Sound font wasn't loading. It wasn't loadoing because it was not added to the app bundle correctly. I had dragged it into resources but had to then add it to 'Copy bundle resources' in the project 'Build Phases'.
Related
I have an AUGraph consisting of two nodes: AUSampler unit and output unit.
I am loading samples to AUSampler from sf2 files. I have multiple sf2 files and want to switch between them in runtime.
Currently I have the following code to set a sf2 file:
- (void) loadSoundFontWithName:(NSString *) name {
CheckError (AUGraphStop(_midiPlayer.graph), "couldn't stop graph");
OSStatus result = noErr;
// fill out a instrument data structure
AUSamplerInstrumentData instdata;
NSString *presetPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat: #"Sampler Files/%#", name]
ofType:#"sf2"];
const char* presetPathC = [presetPath cStringUsingEncoding:NSUTF8StringEncoding];
NSLog (#"presetPathC: %s", presetPathC);
CFURLRef presetURL = CFURLCreateFromFileSystemRepresentation(
kCFAllocatorDefault,
presetPathC,
[presetPath length],
NO);
instdata.fileURL = presetURL;
instdata.instrumentType = kInstrumentType_DLSPreset;
instdata.bankMSB = kAUSampler_DefaultMelodicBankMSB;
instdata.bankLSB = kAUSampler_DefaultBankLSB;
instdata.presetID = (UInt8) 0;
// set the kAUSamplerProperty_LoadPresetFromBank property
result = AudioUnitSetProperty(_midiPlayer.instrumentUnit,
kAUSamplerProperty_LoadInstrument,
kAudioUnitScope_Global,
0,
&instdata,
sizeof(instdata));
// check for errors
NSCAssert (result == noErr,
#"Unable to set the preset property on the Sampler. Error code:%d '%.4s'",
(int) result,
(const char *)&result);
//===============
CheckError (AUGraphStart(_midiPlayer.graph), "couldn't start graph");
}
It works only if there is not any sounds played at the time of switching to another file. If the property is set while the graph being played, it crashes with EXC_BAD_ACCESS at DLSSample::GetMoreFrames(unsigned long long, unsigned long long, void*) ()
So what is the right way to update this property?
Calling
AudioUnitReset(_midiPlayer.instrumentUnit, kAudioUnitScope_Global, 0);
just after the invocation of AudioUnitSetProperty function seems to solve my problem.
I'm currently trying to write an application using an open-source, external library. I have the source code available to it, and can build myself a fresh copy whenever needed.
Anyway, while profiling my application - I noticed that some memory was leaking in the library. It's small - 128b a shot - but still, I'd prefer not to have memory leaks to begin with.
Here's the code. The modified code that I wrote is on top (that leaks), and the original code is on bottom (that leaks).
CFURLRef getURLFromPath(const char * path) {
//modified code to hopefully clean up after myself
CFStringRef cfTotalPath = CFStringCreateWithCString (NULL, path, kCFStringEncodingUTF8);
CFURLRef cURL = CFURLCreateWithFileSystemPath(NULL, cfTotalPath, kCFURLPOSIXPathStyle, false);
CFRelease(cfTotalPath);
return cURL;
//original code
/*CFStringRef cfTotalPath = CFStringCreateWithCString (kCFAllocatorDefault,
path, kCFStringEncodingUTF8);
return CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfTotalPath,
kCFURLPOSIXPathStyle, false);*/
}
I'm relatively new to iOS programming; I'm debugging on an actual device, and I know that sometimes Instruments gives false-positives when it comes to leaks.
This is infuriating, because this one block of code is the last step in the stack trace for my leaks... and I honestly do not know how to fix it.
EDIT: From what I've read, Apple doesn't mind the occasional memory leak here and there; I'll continue programming, because this process only happens once per music file in my application - analyzing a track for the BPM (it gets saved once analyzed).
Edit2: Here's the referring code. I've added all of the CFRelease(fileURL), but it still leaks:
uint_t aubio_sink_apple_audio_open(aubio_sink_apple_audio_t *s) {
if (s->samplerate == 0 || s->channels == 0) return AUBIO_FAIL;
AudioStreamBasicDescription clientFormat;
memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
clientFormat.mFormatID = kAudioFormatLinearPCM;
clientFormat.mSampleRate = (Float64)(s->samplerate);
clientFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
clientFormat.mChannelsPerFrame = s->channels;
clientFormat.mBitsPerChannel = sizeof(short) * 8;
clientFormat.mFramesPerPacket = 1;
clientFormat.mBytesPerFrame = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
clientFormat.mBytesPerPacket = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
clientFormat.mReserved = 0;
AudioFileTypeID fileType = kAudioFileWAVEType;
CFURLRef fileURL = getURLFromPath(s->path);
bool overwrite = true;
OSStatus err = noErr;
err = ExtAudioFileCreateWithURL(fileURL, fileType, &clientFormat, NULL,
overwrite ? kAudioFileFlags_EraseFile : 0, &s->audioFile);
if (err) {
char_t errorstr[20];
AUBIO_ERR("sink_apple_audio: error when trying to create %s with "
"ExtAudioFileCreateWithURL (%s)\n", s->path,
getPrintableOSStatusError(errorstr, err));
goto beach;
}
if (createAubioBufferList(&s->bufferList, s->channels, s->max_frames * s->channels)) {
AUBIO_ERR("sink_apple_audio: error when creating buffer list for %s, "
"out of memory? \n", s->path);
goto beach;
}
//added release code
CFRelease(fileURL);
return AUBIO_OK;
beach:
//added release code
CFRelease(fileURL);
return AUBIO_FAIL;
}
EDIT3: Here's a screenshot
EDIT4: The original solution actually works, XCode refused to load the new version of my framework even though I kept recompiling it. So, I had to purge all references of the framework - including scrubbing the Build info pages - and re-add the "fixed" version.
You are leaking the returned CFURLRef created with CFURLCreateWithFileSystemPath.
You should rename the function from getURLFromPath to createURLFromPath to indicate that ownership of the returned CFURLRef is being passed on to the caller. Then any code that calls the method is responsible for releasing the CFURLRef when done with it.
So, basically I want to play some audio files (mp3 and caf mostly). But the callback never gets called. Only when I call them to prime the queue.
Here's my data struct:
struct AQPlayerState
{
CAStreamBasicDescription mDataFormat;
AudioQueueRef mQueue;
AudioQueueBufferRef mBuffers[kBufferNum];
AudioFileID mAudioFile;
UInt32 bufferByteSize;
SInt64 mCurrentPacket;
UInt32 mNumPacketsToRead;
AudioStreamPacketDescription *mPacketDescs;
bool mIsRunning;
};
Here's my callback function:
static void HandleOutputBuffer (void *aqData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer)
{
NSLog(#"HandleOutput");
AQPlayerState *pAqData = (AQPlayerState *) aqData;
if (pAqData->mIsRunning == false) return;
UInt32 numBytesReadFromFile;
UInt32 numPackets = pAqData->mNumPacketsToRead;
AudioFileReadPackets (pAqData->mAudioFile,
false,
&numBytesReadFromFile,
pAqData->mPacketDescs,
pAqData->mCurrentPacket,
&numPackets,
inBuffer->mAudioData);
if (numPackets > 0) {
inBuffer->mAudioDataByteSize = numBytesReadFromFile;
AudioQueueEnqueueBuffer (pAqData->mQueue,
inBuffer,
(pAqData->mPacketDescs ? numPackets : 0),
pAqData->mPacketDescs);
pAqData->mCurrentPacket += numPackets;
} else {
// AudioQueueStop(pAqData->mQueue, false);
// AudioQueueDispose(pAqData->mQueue, true);
// AudioFileClose (pAqData->mAudioFile);
// free(pAqData->mPacketDescs);
// free(pAqData->mFloatBuffer);
pAqData->mIsRunning = false;
}
}
And here's my method:
- (void)playFile
{
AQPlayerState aqData;
// get the source file
NSString *p = [[NSBundle mainBundle] pathForResource:#"1_Female" ofType:#"mp3"];
NSURL *url2 = [NSURL fileURLWithPath:p];
CFURLRef srcFile = (__bridge CFURLRef)url2;
OSStatus result = AudioFileOpenURL(srcFile, 0x1/*fsRdPerm*/, 0/*inFileTypeHint*/, &aqData.mAudioFile);
CFRelease (srcFile);
CheckError(result, "Error opinning sound file");
UInt32 size = sizeof(aqData.mDataFormat);
CheckError(AudioFileGetProperty(aqData.mAudioFile, kAudioFilePropertyDataFormat, &size, &aqData.mDataFormat),
"Error getting file's data format");
CheckError(AudioQueueNewOutput(&aqData.mDataFormat, HandleOutputBuffer, &aqData, CFRunLoopGetCurrent(), kCFRunLoopCommonModes, 0, &aqData.mQueue),
"Error AudioQueueNewOutPut");
// we need to calculate how many packets we read at a time and how big a buffer we need
// we base this on the size of the packets in the file and an approximate duration for each buffer
{
bool isFormatVBR = (aqData.mDataFormat.mBytesPerPacket == 0 || aqData.mDataFormat.mFramesPerPacket == 0);
// first check to see what the max size of a packet is - if it is bigger
// than our allocation default size, that needs to become larger
UInt32 maxPacketSize;
size = sizeof(maxPacketSize);
CheckError(AudioFileGetProperty(aqData.mAudioFile, kAudioFilePropertyPacketSizeUpperBound, &size, &maxPacketSize),
"Error getting max packet size");
// adjust buffer size to represent about a second of audio based on this format
CalculateBytesForTime(aqData.mDataFormat, maxPacketSize, 1.0/*seconds*/, &aqData.bufferByteSize, &aqData.mNumPacketsToRead);
if (isFormatVBR) {
aqData.mPacketDescs = new AudioStreamPacketDescription [aqData.mNumPacketsToRead];
} else {
aqData.mPacketDescs = NULL; // we don't provide packet descriptions for constant bit rate formats (like linear PCM)
}
printf ("Buffer Byte Size: %d, Num Packets to Read: %d\n", (int)aqData.bufferByteSize, (int)aqData.mNumPacketsToRead);
}
// if the file has a magic cookie, we should get it and set it on the AQ
size = sizeof(UInt32);
result = AudioFileGetPropertyInfo(aqData.mAudioFile, kAudioFilePropertyMagicCookieData, &size, NULL);
if (!result && size) {
char* cookie = new char [size];
CheckError(AudioFileGetProperty(aqData.mAudioFile, kAudioFilePropertyMagicCookieData, &size, cookie),
"Error getting cookie from file");
CheckError(AudioQueueSetProperty(aqData.mQueue, kAudioQueueProperty_MagicCookie, cookie, size),
"Error setting cookie to file");
delete[] cookie;
}
aqData.mCurrentPacket = 0;
for (int i = 0; i < kBufferNum; ++i) {
CheckError(AudioQueueAllocateBuffer (aqData.mQueue,
aqData.bufferByteSize,
&aqData.mBuffers[i]),
"Error AudioQueueAllocateBuffer");
HandleOutputBuffer (&aqData,
aqData.mQueue,
aqData.mBuffers[i]);
}
// set queue's gain
Float32 gain = 1.0;
CheckError(AudioQueueSetParameter (aqData.mQueue,
kAudioQueueParam_Volume,
gain),
"Error AudioQueueSetParameter");
aqData.mIsRunning = true;
CheckError(AudioQueueStart(aqData.mQueue,
NULL),
"Error AudioQueueStart");
}
And the output when I press play:
Buffer Byte Size: 40310, Num Packets to Read: 38
HandleOutput start
HandleOutput start
HandleOutput start
I tryed replacing CFRunLoopGetCurrent() with CFRunLoopGetMain() and CFRunLoopCommonModes with CFRunLoopDefaultMode, but nothing.
Shouldn't the primed buffers start playing right away I start the queue?
When I start the queue, no callbacks are bang fired.
What am I doing wrong? Thanks for any ideas
What you are basically trying to do here is a basic example of audio playback using Audio Queues. Without looking at your code in detail to see what's missing (that could take a while) i'd rather recommend to you to follow the steps in this basic sample code that does exactly what you're doing (without the extras that aren't really relevant.. for example why are you trying to add audio gain?)
Somewhere else you were trying to play audio using audio units. Audio units are more complex than basic audio queue playback, and I wouldn't attempt them before being very comfortable with audio queues. But you can look at this example project for a basic example of audio queues.
In general when it comes to Core Audio programming in iOS, it's best you take your time with the basic examples and build your way up.. the problem with a lot of tutorials online is that they add extra stuff and often mix it with obj-c code.. when Core Audio is purely C code (ie the extra stuff won't add anything to the learning process). I strongly recommend you go over the book Learning Core Audio if you haven't already. All the sample code is available online, but you can also clone it from this repo for convenience. That's how I learned core audio. It takes time :)
Along with a bunch of other things included in Apple's Load Preset Demo sample code, the call to CFURLCreateDataAndPropertiesFromResource is now deprecated. But I can't find a substitute for it - neither an option-click nor a look at the reference tell me any more than that it is no longer the done thing.
CFDataRef propertyResourceData = 0;
Boolean status;
SInt32 errorCode = 0;
OSStatus result = noErr;
// Read from the URL and convert into a CFData chunk
status = CFURLCreateDataAndPropertiesFromResource (
kCFAllocatorDefault,
(__bridge CFURLRef) presetURL,
&propertyResourceData,
NULL,
NULL,
&errorCode
);
NSAssert (status == YES && propertyResourceData != 0, #"Unable to create data and properties from a preset. Error code: %d '%.4s'", (int) errorCode, (const char *)&errorCode);
// Convert the data object into a property list
CFPropertyListRef presetPropertyList = 0;
CFPropertyListFormat dataFormat = 0;
CFErrorRef errorRef = 0;
presetPropertyList = CFPropertyListCreateWithData (
kCFAllocatorDefault,
propertyResourceData,
kCFPropertyListImmutable,
&dataFormat,
&errorRef
);
// Set the class info property for the Sampler unit using the property list as the value.
if (presetPropertyList != 0) {
result = AudioUnitSetProperty(
self.samplerUnit,
kAudioUnitProperty_ClassInfo,
kAudioUnitScope_Global,
0,
&presetPropertyList,
sizeof(CFPropertyListRef)
);
CFRelease(presetPropertyList);
}
if (errorRef) CFRelease(errorRef);
CFRelease (propertyResourceData);
return result;
For the properties: CFURLCopyResourcePropertiesForKeys example property: kCFURLFileSizeKey and kCFURLContentModificationDateKey, or Foundation-style with [NSURL resourceValuesForKeys:error:].
For the data: +[NSData dataWithContentsOfURL:options:error:].
They're not documented as replacements, AFAIK. Most of these newer replacement APIs have been around for a few years now.
Edit
In this example you posted in the edit, the program makes no request for properties, so you just want the data at the URL presetURL.
You can achieve this by:
NSURL * presetURL = ...;
// do review these options for your needs. you can make great
// optimizations if you use memory mapping or avoid unnecessary caching.
const NSDataReadingOptions DataReadingOptions = 0;
NSError * outError = nil;
NSData * data = [NSData dataWithContentsOfURL:presetURL
options:DataReadingOptions
error:&outError];
const bool status = nil != data; // << your `status` variable
if (!status) {
// oops - an error was encountered getting the data see `outError`
}
else {
// use the data
}
I found that I could remove even more code by using just the following:
OSStatus result = noErr;
NSData* data = [NSData dataWithContentsOfURL:presetURL];
id propertyList = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:NULL];
// Set the class info property for the Sampler unit using the property list as the value.
if (propertyList) {
result = AudioUnitSetProperty(
self.samplerUnit,
kAudioUnitProperty_ClassInfo,
kAudioUnitScope_Global,
0,
(__bridge CFPropertyListRef)propertyList,
sizeof(CFPropertyListRef)
);
}
return result;
I ended up using this code https://developer.apple.com/library/mac/technotes/tn2283/_index.html#//apple_ref/doc/uid/DTS40011217-CH1-TNTAG2
- (OSStatus) loadSynthFromPresetURL: (NSURL *) presetURL {
OSStatus result = noErr;
AUSamplerInstrumentData auPreset = {0};
auPreset.fileURL = (__bridge CFURLRef) presetURL;
auPreset.instrumentType = kInstrumentType_AUPreset;
result = AudioUnitSetProperty(self.samplerUnit,
kAUSamplerProperty_LoadInstrument,
kAudioUnitScope_Global,
0,
&auPreset,
sizeof(auPreset));
return result;
From looking at the AUSampler API it seems like it should support Garage Band EXS24 instruments. The AudioUnitProperties.h file says the following:
typedef struct AUSamplerInstrumentData {
CFURLRef fileURL;
UInt8 instrumentType;
UInt8 bankMSB;
UInt8 bankLSB;
UInt8 presetID;
} AUSamplerInstrumentData;
Where the instrument type can have the following types:
enum
{
kInstrumentType_DLSPreset = 1,
kInstrumentType_SF2Preset = kInstrumentType_DLSPreset,
kInstrumentType_AUPreset = 2,
kInstrumentType_Audiofile = 3,
kInstrumentType_EXS24 = 4
};
I've tried to load the instrument using the following function:
-(OSStatus) loadFromEXS: (NSString *) path withSampler: (AudioUnit) sampler {
OSStatus result = noErr;
NSURL *presetURL = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:path ofType:#"exs"]];
AUSamplerInstrumentData bpdata = {0};
bpdata.fileURL = (__bridge CFURLRef)(presetURL);
bpdata.instrumentType = kInstrumentType_EXS24;
result = AudioUnitSetProperty(sampler,
kAUSamplerProperty_LoadInstrument,
kAudioUnitScope_Global,
0,
&bpdata,
sizeof(bpdata));
return result;
}
In my resources I have a group which contains the .exs file and a number of .wav samples. This function produces the following error:
GlobalState::LoadEXS24Instrument: Load failed
So does this mean that the EXS file isn't correct? Does it mean that I've not loaded it correctly? Or maybe, this isn't supported in iOS6?
Yes it seems like it supports the EXS file format. There's one problem however: EXS uses absolute file paths. It does not support relative paths. That means you can't move the .wav samples around, or it will break the EXS instrument. That's my guess why it isn't working.
See this: https://developer.apple.com/library/mac/#technotes/tn2283/_index.html