core audio - Efficiently writing Int16 data to memory in Swift? -


i have memory reference, mbuffers.mdata (from audiounit bufferlist), declared in os x , ios framework headers an:

unsafemutablepointer<void> 

what efficient way write lots of int16 values memory referenced pointer?

a disassembly of swift source code:

for in 0..<count {     var x  : int16 = somefastcalculation()     let lobyte : int32 =  int32(x)       & 0x00ff     let hibyte : int32 = (int32(x) >> 8) & 0x00ff     memset(mbuffers.mdata + 2 *    , lobyte, 1)        memset(mbuffers.mdata + 2 * + 1, hibyte, 1) } 

shows lots of instructions setting memset() function calls (far more instructions in somefastcalculation). loop inside real-time audio callback, efficient code minimize latency , battery consumption important.

is there faster way?

this swift source allows array assignment of individual audio samples audio unit (or auaudiounit) audio buffer, , compiles down faster result using memset.

let mutabledata = unsafemutablepointer<int16>(mbuffers.mdata) let samplearray = unsafemutablebufferpointer<int16>(     start: mutabledata,     count: int(mbuffers.mdatabytesize)/sizeof(int16))  in 0..<count {     let x : int16 = mysamplesynthfunction(i)     samplearray[i]  =  x } 

more complete gist here .


Comments