问题
You must had gone through this before coming to my this question.How to use kAudioUnitSubType_LowShelfFilter of kAudioUnitType_Effect which controls bass in core Audio? Slowly & Steadily getting the things right for bass control of music. But yet not got succeeded in my objective. Now i got to know that i have to change the kAULowShelfParam_CutoffFrequency to change the bass.
The following code i was using before 5 to 7 days. this code plays music properly but doesn't change bass properly. have a look on this code snippet:-
- (void)awakeFromNib
{
printf("AUGraphController awakeFromNib\n");
mIsPlaying = false;
// clear the mSoundBuffer struct
memset(&mUserData.soundBuffer, 0, sizeof(mUserData.soundBuffer));
// create the URLs we'll use for source A and B
NSString *sourceA = [[NSBundle mainBundle] pathForResource:@"04 - Second Hand Jawaani - [rKmania.com]" ofType:@"mp3"];
NSString *sourceB = [[NSBundle mainBundle] pathForResource:@"Track2" ofType:@"mp4"];
sourceURL[0] = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)sourceA, kCFURLPOSIXPathStyle, false);
sourceURL[1] = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)sourceB, kCFURLPOSIXPathStyle, false);
[bassSlider addTarget:self action:@selector(selectEQPreset) forControlEvents:UIControlEventValueChanged];
}
// output unit
CAComponentDescription output_desc(kAudioUnitType_Output, kAudioUnitSubType_RemoteIO, kAudioUnitManufacturer_Apple);
// iPodEQ unit
CAComponentDescription eq_desc(kAudioUnitType_Effect, kAudioUnitSubType_AUiPodEQ, kAudioUnitManufacturer_Apple);
// multichannel mixer unit
CAComponentDescription mixer_desc(kAudioUnitType_Mixer, kAudioUnitSubType_MultiChannelMixer, kAudioUnitManufacturer_Apple);
printf("add nodes\n");
- (void)selectEQPreset;
{
AUPreset *aPreset = (AUPreset*)CFArrayGetValueAtIndex(mEQPresetsArray, value);
OSStatus result = AudioUnitSetProperty(mEQ, kAudioUnitProperty_PresentPreset, kAudioUnitScope_Global, 0, aPreset, sizeof(AUPreset));
if (result) { printf("AudioUnitSetProperty result %ld %08X %4.4s\n", result, (unsigned int)result, (char*)&result); return; };
printf("SET EQ PRESET %d ", value);
CFShow(aPreset->presetName);
}
Now after this code i got to know that i have to change frequency to change the bass, then i use the following code snippet but in the following code snippet music is not playing and its giving excess_bad too. i'm just writing the changes i made to the code.have a look on current code snippet:-
// iPodEQ unit
CAComponentDescription eq_desc(kAudioUnitType_Effect, kAudioUnitSubType_LowShelfFilter, kAudioUnitManufacturer_Apple);
- (void)selectEQPreset;
{
AudioUnit lowShelfAU;
assert(lowShelfAU);
float frequencyInHz = 120.0f;
frequencyInHz = bassSlider.value;
OSStatus result = AudioUnitSetParameter(lowShelfAU,kAULowShelfParam_CutoffFrequency,kAudioUnitScope_Global,0,frequencyInHz,0);
if (noErr != result)
{
assert(0 && "error!");
return ;
}
}
This code now i'm using but this is not changing the frequency. Its even stop playing the music and giving the excess_bad error on this line of code..
AudioUnitSetParameter(lowShelfAU,kAULowShelfParam_CutoffFrequency,kAudioUnitScope_Global,0,frequencyInHz,0);
Please anybody help me regarding this tell me how can i change the kAULowShelfParam_CutoffFrequency so that i can adjust the bass of music Via slider. Any help regarding this would be highly appreciable.
Thanks :)
回答1:
Before you can use an AudioUnit
you need to create it. If you're using an AUGraph
your code will look something like:
AudioComponentDescription filterDescription = { kAudioUnitType_Effect, kAudioUnitSubType_LowShelfFilter, kAudioUnitSubType_LowShelfFilter, 0, 0 };
AUNode filterNode = -1;
OSStatus result = AUGraphAddNode(mAUGraph, &filterDescription, &filterNode);
if(noErr != result) {
// Handle error
}
AudioUnit filterUnit = nullptr;
result = AUGraphNodeInfo(mAUGraph, filterNode, nullptr, &filterUnit);
if(noErr != result) {
// Handle error
}
// Set parameters on filterUnit
The reason your code is failing is that the line
AudioUnit lowShelfAU;
initializes lowShelfAU
with an undetermined value. An AudioUnit
is a pointer type so without initialization it points to an unknown area of memory. I think it is a programming best practice to always initialize your variables when they are declared, to catch these kinds of bugs:
AudioUnit lowShelfAU = nullptr;
回答2:
`AudioUnit lowShelfAU;` <-- that is an uninitialized garbage value
you need to actually create an AU instance (the Low Shelf) and add it to an AUGraph.
Note: The compiler/analyzer will identify this problem for you. I recommend people turn the warning levels waaaay up, then build, analyze, and remove all issues. Repeat until clean.
回答3:
`as you know ,apple create 3 audio units according their purpose. remoteIO --> for input and output;mixer --> for audio mixer ;for some audio effect is eq and Filter so,if u wanna cutoff the audio frequency,u need to create a filter unit and connect them together.
来源:https://stackoverflow.com/questions/12033705/how-to-use-kaulowshelfparam-cutofffrequency-parameter-of-kaudiounitsubtype-lowsh