iOS - Accessing generated signal from Audio Unit render callback -
i have ios/objective-c program uses single audio unit play generated signal when button pressed. i'd add functionality such that:
a) when button first pressed, signal generated in kind of numeric array.
b) audio begins, , render callback accesses (and plays) generated signal.
given current code, feel these additions few lines, i'm having trouble syntax, variable types use, how track current sample, , on. i've included related code now:
the button press:
- (ibaction)startpressed:(id)sender { [self setupaudioplayer]; [self createsignal]; [self playaudio]; }
a line setupaudioplayer:
input.inputprocrefcon=&mysignal; // mysignal instance var
the audio creation:
-(void)createsignal{ int beeplength=0.020*fs; // fs sampling frequency float beepfrequency=440; // hz // declare kind of numeric array "mysignal", instance var. mysignal=...? // generate audio signal (pure tone) (int i=1; i<=beeplength; i++) { float t=i/fs; mysignal[i]=sinf(2*m_pi*beepfrequency*t); } }
the render callback:
osstatus rendertone( void *inrefcon, audiounitrenderactionflags *ioactionflags, const audiotimestamp *intimestamp, uint32 inbusnumber, uint32 innumberframes, audiobufferlist *iodata) { const int channel1 = 0; float32 *buffer = (float32 *)iodata->mbuffers[channel1].mdata; // things hazy float32 *mysignal=(float32 *)inrefcon; (uint32 frame = 0; frame < innumberframes; frame++) { buffer[frame]=mysignal[?]; } return noerr; }
so, summarize questions: how should mysignal defined? how access instance variable rendertone (my 'hazy' code above guess)? how can track current sample in rendertone? there else missing/wonky in approach?
thanks reading , help, appreciated!
(i have seen sample code passes reference view controller's instance render callback, , accesses instance variables way. however, perhaps mistakenly, read elsewhere wasn't form may involve computational overhead callback such strict timing requirements.)
since you're generating frames algebraic function, why don't follow matt gallagher's example? in brief: move function inside render callback , transfer parameters through vc instance.
generally speaking choices limited passing data callback has pre-defined form. i'm last person counsel on form in objective c, 1 of few options use globals.
you pass mysignal
array (or else frequency) global. not 'elegant' object-oriented solution, 1 work , avoid o.o. frou-frou overhead. seems appropriate use c-based solution, since render callback @ base c function.
as "tracking," not quite sure mean, in own work generating tones, i've initialized remainingcycles
global tone-length (in frame cycles = length in seconds * fs
or samplerate
whatever want call it) , decrementing each pass through frame
loop; when number hits zero, end tone. (of course, use instance variable instead of global.)
maybe violates canons of object-oriented coding, @ end of day, need job done.
Comments
Post a Comment