giuliomoro@24: /* giuliomoro@24: * render.cpp giuliomoro@24: * giuliomoro@24: * Created on: Oct 24, 2014 giuliomoro@24: * Author: parallels giuliomoro@24: */ giuliomoro@24: giuliomoro@301: #include giuliomoro@92: //#include giuliomoro@24: #include giuliomoro@221: #include giuliomoro@222: #include giuliomoro@92: #include giuliomoro@92: andrewm@56: // setup() is called once before the audio rendering starts. giuliomoro@24: // Use it to perform any initialisation and allocation which is dependent giuliomoro@24: // on the period size or sample rate. giuliomoro@24: // giuliomoro@24: // userData holds an opaque pointer to a data structure that was passed giuliomoro@24: // in from the call to initAudio(). giuliomoro@24: // giuliomoro@24: // Return true on success; returning false halts the program. giuliomoro@221: giuliomoro@221: NetworkSend networkSend; giuliomoro@222: ReceiveAudioThread receive; giuliomoro@221: float gFrequency; giuliomoro@221: float gInverseSampleRate; giuliomoro@221: float gPhase; giuliomoro@301: bool setup(BelaContext *context, void *userData) giuliomoro@24: { giuliomoro@24: // Retrieve a parameter passed in from the initAudio() call giuliomoro@24: gFrequency = *(float *)userData; giuliomoro@24: giuliomoro@222: networkSend.setup(context->audioSampleRate, context->audioFrames, 0, 9999, "192.168.7.1"); giuliomoro@222: receive.init(10000, context->audioFrames, 0); giuliomoro@222: receive.startThread(); andrewm@56: gInverseSampleRate = 1.0 / context->audioSampleRate; giuliomoro@222: gPhase = 0; giuliomoro@24: return true; giuliomoro@24: } giuliomoro@24: giuliomoro@24: // render() is called regularly at the highest priority by the audio engine. giuliomoro@24: // Input and output are given from the audio hardware and the other giuliomoro@24: // ADCs and DACs (if available). If only audio is available, numMatrixFrames giuliomoro@24: // will be 0. giuliomoro@24: giuliomoro@301: void render(BelaContext *context, void *userData) giuliomoro@221: { andrewm@56: for(unsigned int n = 0; n < context->audioFrames; n++) { giuliomoro@24: float out = 0.7f * sinf(gPhase); giuliomoro@24: gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; giuliomoro@24: if(gPhase > 2.0 * M_PI) giuliomoro@24: gPhase -= 2.0 * M_PI; giuliomoro@24: giuliomoro@222: networkSend.log(out); giuliomoro@222: float in; giuliomoro@222: int ret = receive.getSamplesSrc(&in, 1, 1); giuliomoro@221: for(unsigned int channel = 0; channel < context->audioChannels; channel++){ giuliomoro@222: audioWriteFrame(context, n, channel, in); giuliomoro@24: } giuliomoro@92: } giuliomoro@24: } giuliomoro@24: andrewm@56: // cleanup() is called once at the end, after the audio has stopped. andrewm@56: // Release any resources that were allocated in setup(). giuliomoro@24: giuliomoro@301: void cleanup(BelaContext *context, void *userData) giuliomoro@24: { giuliomoro@24: }