giuliomoro@24: /* giuliomoro@24: * render.cpp giuliomoro@24: * giuliomoro@24: * Created on: Oct 24, 2014 giuliomoro@24: * Author: parallels giuliomoro@24: */ giuliomoro@24: andrewm@56: #include giuliomoro@92: //#include giuliomoro@24: #include giuliomoro@92: #include giuliomoro@92: #include giuliomoro@92: giuliomoro@92: AuxiliaryTask transmitReceiveDataTask; giuliomoro@92: giuliomoro@92: #define NETWORK_AUDIO_BUFFER_SIZE 400 //1400/4 //maximum payload for a UDP datagram over ethernet is 1472 bytes, I leave some headroom and divide by 4 to get the number of floats giuliomoro@92: struct networkAudio{ giuliomoro@92: int timestamp; giuliomoro@92: int currentBuffer; giuliomoro@92: int index; giuliomoro@92: float buffers[2][NETWORK_AUDIO_BUFFER_SIZE]; giuliomoro@92: int doneOnTime; giuliomoro@92: bool toBeSent; giuliomoro@92: UdpClient udpClient; giuliomoro@92: }; andrewm@56: giuliomoro@24: float gFrequency; giuliomoro@24: float gPhase; giuliomoro@24: float gInverseSampleRate; giuliomoro@24: int gCount=0; giuliomoro@92: //networkData networkObject; giuliomoro@92: #define numNetAudio 3 giuliomoro@92: networkAudio netAudio[numNetAudio]; giuliomoro@92: AuxiliaryTask printIntervalTask; giuliomoro@92: AuxiliaryTask transmitReceiveAudioTask; giuliomoro@24: giuliomoro@92: void transmitReceiveAudio(){ //transmit and receive audio buffers giuliomoro@92: for(int n=0;naudioSampleRate; giuliomoro@24: gPhase = 0.0; giuliomoro@24: giuliomoro@92: // networkObject.counter=&gCount; giuliomoro@92: // networkObject.variables[0]=&gFrequency; giuliomoro@92: // networkObject.variables[1]=&gPhase; giuliomoro@92: // networkObject.numVariables=2; giuliomoro@92: for(int n=0; ntransmitPort+n); giuliomoro@92: // netAudio[n].udpClient.setServer(settings->serverName); giuliomoro@92: netAudio[n].udpClient.setPort(9999+n); giuliomoro@92: netAudio[n].udpClient.setServer("192.168.7.1"); giuliomoro@92: } giuliomoro@92: // setupSockets(settings->receivePort, settings->transmitPort, settings->serverName); giuliomoro@92: giuliomoro@92: // transmitReceiveDataTask=createAuxiliaryTask(*transmitReceiveData, 10, "transmit-receive-data"); giuliomoro@92: // scheduleAuxiliaryTask(transmitReceiveDataTask); //here it does not work giuliomoro@92: transmitReceiveAudioTask=BeagleRT_createAuxiliaryTask(*transmitReceiveAudio, 98, "transmit-receive-audio"); 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: andrewm@56: void render(BeagleRTContext *context, void *userData) giuliomoro@92: {/* 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: andrewm@56: for(unsigned int channel = 0; channel < context->audioChannels; channel++) andrewm@56: context->audioOut[n * context->audioChannels + channel] = out; andrewm@56: andrewm@56: if(gCount == 0){ andrewm@56: BeagleRT_scheduleAuxiliaryTask(transmitReceiveDataTask); giuliomoro@24: } giuliomoro@24: gCount++; giuliomoro@24: } giuliomoro@92: giuliomoro@92: giuliomoro@92: */ giuliomoro@92: for(int n = 0; n < context->audioFrames; n++) { giuliomoro@92: float out = 0.7f * sinf(gPhase); giuliomoro@92: gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; giuliomoro@92: if(gPhase > 2.0 * M_PI) giuliomoro@92: gPhase -= 2.0 * M_PI; giuliomoro@92: giuliomoro@92: // for(int channel = 0; channel < context->audioChannels; channel++) giuliomoro@92: // context->audioOut[n * context->audioChannels + channel] = context->audioIn[n * context->audioChannels + 0]+context->audioIn[n * context->audioChannels + 1]; giuliomoro@92: context->audioOut[n * context->audioChannels] = context->audioIn[n*context->audioChannels+0]; giuliomoro@92: context->audioOut[n * context->audioChannels+1]=out; giuliomoro@92: if(0==gCount){ giuliomoro@92: // scheduleAuxiliaryTask(transmitReceiveDataTask); giuliomoro@92: } giuliomoro@92: for(int j=0; jaudioOut[n*context->audioChannels + 0]; giuliomoro@92: netAudio[2].buffers[netAudio[2].currentBuffer][netAudio[2].index++]=analogReadFrame(context,n/2,1)+context->audioOut[n*context->audioChannels + 0]; giuliomoro@92: } giuliomoro@92: netAudio[0].buffers[netAudio[0].currentBuffer][netAudio[0].index++]=0.5*(out+context->audioOut[n*context->audioChannels + 0]);//copy channel 0 to the buffer giuliomoro@92: // netAudio[1].buffers[netAudio[1].currentBuffer][netAudio[1].index++]=0.5*(out+context->audioOut[n*context->audioChannels + 0]); giuliomoro@92: // netAudio[2].buffers[netAudio[2].currentBuffer][netAudio[2].index++]=0.5*(out+context->audioOut[n*context->audioChannels + 0]); giuliomoro@92: gCount++; 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: andrewm@56: void cleanup(BeagleRTContext *context, void *userData) giuliomoro@24: { giuliomoro@24: }