简体   繁体   中英

How to remove the noise from the audio coming from socket that uses audio unit in iOS?

I am trying to play the audio coming from socket. But the audio has more noise (the origional sound is not coming)

I am using seperate class for speaker and want to filter the noise from the audio. My code is

OSStatus WNSpeakerOutputProc(void* inRefCon, AudioUnitRenderActionFlags* ioActionFlags, const AudioTimeStamp* inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList* ioData) {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    WNSpeakerAudioData speakerAudioData;
    speakerAudioData.outBuffer = ioData->mBuffers[0].mData;
    speakerAudioData.outBufferSize = ioData->mBuffers[0].mDataByteSize;


    [[(WNSpeaker *)inRefCon dataSource] supplyAudioData:&speakerAudioData];
    [pool drain];
    return noErr;

}

Please tell some suggestion Thank you

I would like to start by saying that you cannot just open a socket and pipe the data directly into an audio render. You will need to buffer up some minimal amount of data to dejitter the data. Are you using TCP or UDP? If UDP, how are you dealing with lost packets.
In general, you do not want to do anything slow inside your audio callback. I would highly suggest removing the AutoReleasePool creation and draining.

I would also suggest deep copying the data from your source into ioData right inside your function rather than calling the selector supplyAudioData.

It would be more helpful if you provide the details of WNSpeaker. It should copy data from your source into the destination address of ioData->mBuffers[0].mData and it should copy n bytes based on inNumberFrames.

Take more detail look at the the build code of audio unit

-(void) setupAudioUnit
{
    AudioComponentDescription desc;
    desc.componentType = kAudioUnitType_Output;
    desc.componentSubType = kAudioUnitSubType_VoiceProcessingIO;
    desc.componentManufacturer = kAudioUnitManufacturer_Apple;
    desc.componentFlags = 0;
    desc.componentFlagsMask = 0;

    AudioComponent comp = AudioComponentFindNext(NULL, &desc);

    OSStatus status;

    status = AudioComponentInstanceNew(comp, &_audioUnit);

    if(status != noErr)
    {
        NSLog(@"Error creating AudioUnit instance");
    }

    //  Enable input and output on AURemoteIO
    //  Input is enabled on the input scope of the input element
    //  Output is enabled on the output scope of the output element

    UInt32 one = 1;

    status = AudioUnitSetProperty(_audioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, kOutputBus, &one, sizeof(one));


    if(status != noErr)
    {
        NSLog(@"Error enableling AudioUnit output bus");
    }

    // Explicitly set the input and output client formats
    // sample rate = 44100, num channels = 1, format = 16 bit int point

    AudioStreamBasicDescription audioFormat = [self getAudioDescription];

    status = AudioUnitSetProperty(_audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &audioFormat, sizeof(audioFormat));

    if(status != noErr)
    {
        NSLog(@"Error setting audio format");
    }

    AURenderCallbackStruct renderCallback;
    renderCallback.inputProc = OutputRenderCallback;
    renderCallback.inputProcRefCon = (__bridge void *)(self);

    status = AudioUnitSetProperty(_audioUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Global, kOutputBus, &renderCallback, sizeof(renderCallback));

    if(status != noErr)
    {
        NSLog(@"Error setting rendering callback");
    }

    // Initialize the AURemoteIO instance
    status = AudioUnitInitialize(_audioUnit);

    if(status != noErr)
    {
        NSLog(@"Error initializing audio unit");
    }
}

- (AudioStreamBasicDescription)getAudioDescription {
    AudioStreamBasicDescription audioDescription = {0};
    audioDescription.mFormatID          = kAudioFormatLinearPCM;
    audioDescription.mFormatFlags       = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked | kAudioFormatFlagsNativeEndian;
    audioDescription.mChannelsPerFrame  = 1;
    audioDescription.mBytesPerPacket    = sizeof(SInt16)*audioDescription.mChannelsPerFrame;
    audioDescription.mFramesPerPacket   = 1;
    audioDescription.mBytesPerFrame     = sizeof(SInt16)*audioDescription.mChannelsPerFrame;
    audioDescription.mBitsPerChannel    = 8 * sizeof(SInt16);
    audioDescription.mSampleRate        = 8000.0;
    return audioDescription;
}

I am using the 8000 sample rate and Voice_ProcessingIO as a ComponentSubType but echo(noise) of the audio is not getting cancelled Please check this one.Thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM