2

Reverb.m

    #define D 1000

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

MusicPlaybackState *musicPlaybackState = (MusicPlaybackState*) inRefCon;

                //Sample Rate 44.1    
                float a0,a1; 
                double y0, sampleinp;

                    //Delay Gain 
                    a0 = 1; 
                    a1 = 0.5; 

                for (int i = 0; i< ioData->mNumberBuffers; i++){
                AudioBuffer buffer = ioData->mBuffers[i];
                SIn16 *outSampleBuffer = buffer.mData;    
                    for (int j = 0; j < inNumberFrames*2; j++) {  

                            //Delay Left Channel 

                            sampleinp = *musicPlaybackState->samplePtr++; 

                    /* IIR equation of Comb Filter 
                     y[n] = (a*x[n])+ (b*x[n-D]) 
                    */ 

        y0 = (a0*sampleinp) + (a1*sampleinp-D);

                            outSample[j] = fmax(fmin(y0, 32767.0), -32768.0);  


                            j++;            

                    //Delay Right Channel 

        sampleinp = *musicPlaybackState->samplePtr++;

                           y0 = (a0*sampleinp) + (a1*sampleinp-D);

                            outSample[j] = fmax(fmin(y0, 32767.0), -32768.0);  


                        } 
                    } 

                }

Ok, I got a lot of info but I'm having trouble implementing it. Can someone help, it's probably something really easy i'm forgeting. It's just playing back as normal with a little boost but no delays.

Cocell
  • 159
  • 2
  • 10

1 Answers1

1

Your treatment of the x0[] variables doesn't look right -- the way you have it, the left and right channels will be intermingled. You assign to x0[j] for the left channel, then overwrite x0[j] with the right channel data. So the delayed signal x0[j-D] will always correspond to the right channel, with the delayed left channel data being lost.

You didn't say what your sample rate is, but for a typical audio application, a three-sample delay might not have much of an audible effect. At 44.1 ksamp/sec, with a 3-sample delay the peaks and troughs of the filter response will be at multiples of 14,700 Hz. All you'll get is a single peak in the audio frequency range, in a part of the spectrum where there's hardly any power (assuming the signal is speech or music).

Jim Lewis
  • 43,505
  • 7
  • 82
  • 96
  • @Jim Lewis So what your saying is that if I were to set Left channel to x0[n] and right to x1[n] They wouldn't intermingle and I would hear the delay on both channels? – Cocell Feb 21 '11 at 18:33
  • When I changed the variables for the channels I get a little static on both channels instead of just the left channel. – Cocell Feb 21 '11 at 18:39
  • @Jim Lewis: I convert all audio/music to 44.1, So to actually hear the delay, I would need a number somewhere in the thousands or more? – Cocell Feb 21 '11 at 18:51
  • At a 44100 sample rate, to hear a 100 mS echo, you would need a 4410 sample delay, so yes, thousands. – hotpaw2 Feb 21 '11 at 19:06
  • @Cocell: Try D=44 samples, which should produce peaks at close to 1 kHz intervals -- that should be a pretty noticeable effect. – Jim Lewis Feb 21 '11 at 19:07
  • Tried both 44 samples which still gave me static and 4410 sample delay that cleared up the static but no delays. For some reason I don't think that the it's changing the samples the right way and the input array where x0[j-D] – Cocell Feb 21 '11 at 19:24
  • @Cocell: "Static" could mean lots of things -- maybe the next step is for you to plot some waveforms, or figure out some other way to describe the inputs you're using, the output you expect, and the output you're getting. And you'll probably need to post more of the code. – Jim Lewis Feb 21 '11 at 19:34
  • x[j-D], if j=2 and D=4410, so x[-4408] Which doesn't exist. So now I'm very confused on how to get that inbetween buffer x[0] and let's say x[512]; Anyone?? Do I need another circular buffer for the delay? – Cocell Feb 22 '11 at 22:43
  • 1
    @Cocell: A circular buffer with a capacity of D samples (per channel) is probably a good idea. I noticed the possibility of (j-D) going negative, but depending on how x was declared, that wouldn't necessarily be a problem. (For instance, if x was declared as a pointer into the middle of a padded buffer, instead of an array, x[i] might be valid even if i is negative). That's part of the reason I suggested posting more code -- it's hard to tell whether a fragment of code is correct or not without the surrounding context, especially the way its inputs are declared and initialized. – Jim Lewis Feb 22 '11 at 23:04
  • @Jim: I posted more code. x[j] is double array. Is this what you're saying, double *x = double[D]; and then use x0[j-D]? – Cocell Feb 22 '11 at 23:51
  • Should I be using j for each step through samples or i? x0[i-D] or x0[j-D] ? – Cocell Feb 23 '11 at 00:03
  • @Cocell: Is that your real code? I can't make sense out of it; as it stands now you're declaring x0[] inside the for loop with a different size for each iteration, At any rate, I think I'm done with this question. This forum isn't really suited for extended back-and-forth discussion. My parting advice is to take a step back, and think about how you might get this working one small step at a time, and how you might test each piece as you work toward your final goal. – Jim Lewis Feb 23 '11 at 00:13
  • @jim: Thank you for your time I do appreciate it. I'm sorry for the mix up, was trying to make it easy but made it harder. The real code is posted without the CoreAudio part. I was away from my desk when I had posted code earlier. – Cocell Feb 23 '11 at 00:46