void Synthesiser::renderNextBlock (AudioSampleBuffer& outputBuffer, const MidiBuffer& midiData,
                                   int startSample, int numSamples)
{
    // must set the sample rate before using this!
    jassert (sampleRate != 0);

    const ScopedLock sl (lock);

    MidiBuffer::Iterator midiIterator (midiData);
    midiIterator.setNextSamplePosition (startSample);
    MidiMessage m (0xf4, 0.0);

    while (numSamples > 0)
    {
        int midiEventPos;
        const bool useEvent = midiIterator.getNextEvent (m, midiEventPos)
                                && midiEventPos < startSample + numSamples;

        const int numThisTime = useEvent ? midiEventPos - startSample
                                         : numSamples;

        if (numThisTime > 0)
            renderVoices (outputBuffer, startSample, numThisTime);

        if (useEvent)
            handleMidiEvent (m);

        startSample += numThisTime;
        numSamples -= numThisTime;
    }
}
Exemple #2
0
int GenericProcessor::checkForEvents(MidiBuffer& midiMessages)
{

	if (midiMessages.getNumEvents() > 0) 
	{
			
		int m = midiMessages.getNumEvents();
		//std::cout << m << " events received by node " << getNodeId() << std::endl;

		MidiBuffer::Iterator i (midiMessages);
		MidiMessage message(0xf4);

		int samplePosition = 0;
		i.setNextSamplePosition(samplePosition);

		while (i.getNextEvent (message, samplePosition)) {
			
			uint8* dataptr = message.getRawData();

			handleEvent(*dataptr, message, samplePosition);

		}

	}

	return -1;

}
void Synthesiser::processNextBlock (AudioBuffer<floatType>& outputAudio,
                                    const MidiBuffer& midiData,
                                    int startSample,
                                    int numSamples)
{
    // must set the sample rate before using this!
    jassert (sampleRate != 0);
    const int targetChannels = outputAudio.getNumChannels();

    MidiBuffer::Iterator midiIterator (midiData);
    midiIterator.setNextSamplePosition (startSample);

    bool firstEvent = true;
    int midiEventPos;
    MidiMessage m;

    const ScopedLock sl (lock);

    while (numSamples > 0)
    {
        if (! midiIterator.getNextEvent (m, midiEventPos))
        {
            if (targetChannels > 0)
                renderVoices (outputAudio, startSample, numSamples);

            return;
        }

        const int samplesToNextMidiMessage = midiEventPos - startSample;

        if (samplesToNextMidiMessage >= numSamples)
        {
            if (targetChannels > 0)
                renderVoices (outputAudio, startSample, numSamples);

            handleMidiEvent (m);
            break;
        }

        if (samplesToNextMidiMessage < ((firstEvent && ! subBlockSubdivisionIsStrict) ? 1 : minimumSubBlockSize))
        {
            handleMidiEvent (m);
            continue;
        }

        firstEvent = false;

        if (targetChannels > 0)
            renderVoices (outputAudio, startSample, samplesToNextMidiMessage);

        handleMidiEvent (m);
        startSample += samplesToNextMidiMessage;
        numSamples  -= samplesToNextMidiMessage;
    }

    while (midiIterator.getNextEvent (m, midiEventPos))
        handleMidiEvent (m);
}
Exemple #4
0
void MPESynthesiserBase::renderNextBlock (AudioBuffer<floatType>& outputAudio,
                                          const MidiBuffer& inputMidi,
                                          int startSample,
                                          int numSamples)
{
    // you must set the sample rate before using this!
    jassert (sampleRate != 0);

    MidiBuffer::Iterator midiIterator (inputMidi);
    midiIterator.setNextSamplePosition (startSample);

    bool firstEvent = true;
    int midiEventPos;
    MidiMessage m;

    const ScopedLock sl (noteStateLock);

    while (numSamples > 0)
    {
        if (! midiIterator.getNextEvent (m, midiEventPos))
        {
            renderNextSubBlock (outputAudio, startSample, numSamples);
            return;
        }

        const int samplesToNextMidiMessage = midiEventPos - startSample;

        if (samplesToNextMidiMessage >= numSamples)
        {
            renderNextSubBlock (outputAudio, startSample, numSamples);
            handleMidiEvent (m);
            break;
        }

        if (samplesToNextMidiMessage < ((firstEvent && ! subBlockSubdivisionIsStrict) ? 1 : minimumSubBlockSize))
        {
            handleMidiEvent (m);
            continue;
        }

        firstEvent = false;

        renderNextSubBlock (outputAudio, startSample, samplesToNextMidiMessage);
        handleMidiEvent (m);
        startSample += samplesToNextMidiMessage;
        numSamples  -= samplesToNextMidiMessage;
    }

    while (midiIterator.getNextEvent (m, midiEventPos))
        handleMidiEvent (m);
}
void MidiMessageCollector::removeNextBlockOfMessages (MidiBuffer& destBuffer,
                                                      const int numSamples)
{
   #if JUCE_DEBUG
    jassert (hasCalledReset); // you need to call reset() to set the correct sample rate before using this object
   #endif

    jassert (numSamples > 0);

    auto timeNow = Time::getMillisecondCounterHiRes();
    auto msElapsed = timeNow - lastCallbackTime;

    const ScopedLock sl (midiCallbackLock);
    lastCallbackTime = timeNow;

    if (! incomingMessages.isEmpty())
    {
        int numSourceSamples = jmax (1, roundToInt (msElapsed * 0.001 * sampleRate));
        int startSample = 0;
        int scale = 1 << 16;

        const uint8* midiData;
        int numBytes, samplePosition;

        MidiBuffer::Iterator iter (incomingMessages);

        if (numSourceSamples > numSamples)
        {
            // if our list of events is longer than the buffer we're being
            // asked for, scale them down to squeeze them all in..
            const int maxBlockLengthToUse = numSamples << 5;

            if (numSourceSamples > maxBlockLengthToUse)
            {
                startSample = numSourceSamples - maxBlockLengthToUse;
                numSourceSamples = maxBlockLengthToUse;
                iter.setNextSamplePosition (startSample);
            }

            scale = (numSamples << 10) / numSourceSamples;

            while (iter.getNextEvent (midiData, numBytes, samplePosition))
            {
                samplePosition = ((samplePosition - startSample) * scale) >> 10;

                destBuffer.addEvent (midiData, numBytes,
                                     jlimit (0, numSamples - 1, samplePosition));
            }
        }
        else
        {
Exemple #6
0
void Synthesiser::renderNextBlock (AudioSampleBuffer& outputBuffer, const MidiBuffer& midiData,
                                   int startSample, int numSamples)
{
    // must set the sample rate before using this!
    jassert (sampleRate != 0);

    MidiBuffer::Iterator midiIterator (midiData);
    midiIterator.setNextSamplePosition (startSample);

    int midiEventPos;
    MidiMessage m;

    const ScopedLock sl (lock);

    while (numSamples > 0)
    {
        if (! midiIterator.getNextEvent (m, midiEventPos))
        {
            renderVoices (outputBuffer, startSample, numSamples);
            return;
        }

        const int samplesToNextMidiMessage = midiEventPos - startSample;

        if (samplesToNextMidiMessage >= numSamples)
        {
            renderVoices (outputBuffer, startSample, numSamples);
            handleMidiEvent (m);
            break;
        }

        if (samplesToNextMidiMessage < minimumSubBlockSize)
        {
            handleMidiEvent (m);
            continue;
        }

        renderVoices (outputBuffer, startSample, samplesToNextMidiMessage);
        handleMidiEvent (m);
        startSample += samplesToNextMidiMessage;
        numSamples  -= samplesToNextMidiMessage;
    }

    while (midiIterator.getNextEvent (m, midiEventPos))
        handleMidiEvent (m);
}
void SpikeViewer::renderOpenGL()
{
		
	 if (eventBuffer->getNumEvents() > 0) {

	   	glRasterPos2f(0.1,0.1);

	   	//const char* str = "i";
	  // 	void* font = GLUT_BITMAP_8_BY_13;

	  // 	glutBitmapCharacter(font,54);
	   //	drawBorder();

		//std::cout << "Events received by Spike Viewer." << std::endl;

		MidiBuffer::Iterator i (*eventBuffer);
		MidiMessage message(0xf4);

		int samplePosition;
		i.setNextSamplePosition(samplePosition);

		//Array<int> peaks;


		clearWaveforms();

		while (i.getNextEvent (message, samplePosition)) {

			int numbytes = message.getRawDataSize();
			int numSamples = (numbytes-2)/2;
			uint8* dataptr = message.getRawData();

			int chan = (*dataptr<<8) + *(dataptr+1);
			int electrode = config->getSource(0)->getElectrodeNumberForChannel(chan);

			//std::cout << chan << "::" << electrode << std::endl;

 			dataptr += 2;

			//glViewport(0,0,getWidth()/2,getHeight());

			if (electrode == 0)
			{
			//for (int n = 0; n < 4; n++) {
				setViewportForWaveN(chan);
				float peak = drawWaveform(dataptr, numSamples);

				peaks.set(chan,peak*1.25);
				//peaks.set(chan,peak);
				
			}

			if (peaks.size() == 4)
			{
				drawProjections();
				peaks.clear();
			}

			//std::cout << " Bytes received: " << numbytes << std::endl;
			//std::cout << " Message timestamp = " << message.getTimeStamp() << std::endl;
			//std::cout << " Message channel: " << chan << std::endl;

 			//std::cout << "   ";
			
 			//AudioDataConverters::convertInt16BEToFloat ( dataptr, // source
    		//			spikeData, // dest
    		//			numSamples, // numSamples
    		//			2 ); // destBytesPerSample = 2

			//for (int n = 0; n < numSamples; n++) {
			//	std::cout << String(spikeData[n]) << " ";
			//}
					
			//std::cout << std::endl << std::endl;
		}

		// for (int ch = 0; ch < 4; ch++)
		// {
			
		// }

		//eventBuffer->clear();

	}

	//glOrtho(0, 0.5, 0.5, 0, 0, 1);
	glFlush();

}
Exemple #8
0
void SpikeDisplayCanvas::processSpikeEvents()
{


	if (spikeBuffer->getNumEvents() > 0) 
	{
		
		//int m = spikeBuffer->getNumEvents();

		//std::cout << "Received " << m << " events." << std::endl;
			
		//std::cout << m << " events received by node " << getNodeId() << std::endl;
		MidiBuffer::Iterator i (*spikeBuffer);
		MidiMessage message(0xf4);

		int samplePosition = 0;

		i.setNextSamplePosition(samplePosition);
		
		//int eventCount = 0;
		
		while (i.getNextEvent (message, samplePosition)) {
			//eventCount++;
			 uint8_t* dataptr = message.getRawData();
			 int bufferSize = message.getRawDataSize();
			// int nSamples = (bufferSize-4)/2;

			SpikeObject newSpike;
			SpikeObject simSpike;

			unpackSpike(&newSpike, dataptr, bufferSize);

			//

			int chan = newSpike.source;

			generateSimulatedSpike(&simSpike, 0, 0);


			for (int i = 0; i < newSpike.nChannels * newSpike.nSamples; i++)
			{
                    simSpike.data[i] = newSpike.data[i%80] + 5000;// * 3 - 10000;
			}

			simSpike.nSamples = 40;

			

			// std::cout << "Received spike on electrode " << chan << std::endl;

			// std::cout << "Spike has " << newSpike.nChannels << " channels and " <<
			//              newSpike.nSamples << " samples." << std::endl;

			// std::cout << "Data: ";

			// for (int n = 0; n < newSpike.nSamples; n++)
			// {
			// 	std::cout << newSpike.data[n] << " ";
			// }

			//	std::cout << std::endl;

			plots[chan]->processSpikeObject(simSpike);

		}

	}

	spikeBuffer->clear();

}
Exemple #9
0
void EventNode::process(AudioSampleBuffer &buffer, 
                            MidiBuffer &midiMessages,
                            int& nSamples)
{
	accumulator++;

	if (!isSource) {
		
		if (midiMessages.getNumEvents() > 0) {
			
			std::cout << "Events received by node " << getNodeId() << std::endl;

			 MidiBuffer::Iterator i (midiMessages);
			 MidiMessage message(0xf4);

			 int samplePosition;
			 i.setNextSamplePosition(samplePosition);

			 while (i.getNextEvent (message, samplePosition)) {
				
					//message.getChannel();

					//MidiMessage msgCopy = MidiMessage(message);
					int numbytes = message.getRawDataSize();
					uint8* dataptr = message.getRawData();

					

					std::cout << " Bytes received: " << numbytes << std::endl;
					std::cout << " Message timestamp = " << message.getTimeStamp() << std::endl;

					//std::cout << sizeof(int) << " " << sizeof(uint16) << std::endl;
 
 					std::cout << "   ";
					for (int n = 0; n < numbytes; n++) {
						std::cout << String(*dataptr++) << " ";
					}
					
					std::cout << std::endl << std::endl;
				 	//std::cout << "  Event on channel " << message.getRawData() << std::endl; //<< message.getRawDataSize() << std::endl;

			}


			// accumulator = 0;
		}//MidiBuffer::Iterator = midiMessages.

		//midiMessages.clear();

	} else {

		if (accumulator > 20) {

			uint8 data[95];

			for (int n = 0; n < sizeof(data); n++) {
				data[n] = 1;
			}

			//MidiMessage event = MidiMessage::noteOn(2,1,10.0f);
			MidiMessage event = MidiMessage(data, 	// spike data (float)
											sizeof(data), 	// number of bytes to use
											1000.0 	// timestamp (64-bit)
											);
			
			//event.setChannel(1);

			midiMessages.addEvent(data, sizeof(data), 5);
			//midiMessages.addEvent(event, 1);

			for (int n = 0; n < sizeof(data); n++) {
				data[n] = 2;
			}

			midiMessages.addEvent(data, sizeof(data), 10);

			for (int n = 0; n < sizeof(data); n++) {
				data[n] = 3;
			}

			midiMessages.addEvent(data, sizeof(data), 15);

			//midiMessages.addEvent(event, 5);

			//std::cout << "Midi buffer contains " << midiMessages.getNumEvents() << " events." << std::endl;

			accumulator = 0;
		}
		

	}

	
	

}