void Core::PlayMathBuffer(void* cookie, void* buffer, size_t size, const media_raw_audio_format& format) { //size_t limit = (kLimit/gCronoSettings.Speed); size_t limit = (gCronoSettings.Speed / 60)*(kFileFormat.u.raw_audio.frame_rate *(kFileFormat.u.raw_audio.channel_count+1)); bool stereo = format.channel_count == 2; kTicLen = (gCronoSettings.Speed/60*60)/ 5; limit = limit*10; if (kSize >= limit) { /* timeval start; prev = start; gettimeofday(&start, NULL); printf("took %lu\n", start.tv_usec - prev.tv_usec);*/ kSize -= limit; kSem = 0; } size_t fillSize = size; if (kSize + size > limit) { fillSize = size + kSize - limit; } if (kSem == 1) { memset(buffer, 0, fillSize); kSize += fillSize; /*if (fillSize < kSize) { fillSize = kSize - fillSize; kSem = 0; }*/ } if (kSem == 0) { switch(gCronoSettings.Engine) { case CRONO_SINE_ENGINE: FillSineBuffer((float*)buffer, fillSize, stereo); break; case CRONO_TRIANGLE_ENGINE: FillTriangleBuffer((float*)buffer, fillSize, stereo); break; case CRONO_SAWTOOTH_ENGINE: FillSawtoothBuffer((float*)buffer, fillSize, stereo); break; } kSize += fillSize; if (kSize >= kTicLen) { kSem = 1; } } }
BBuffer* ToneProducer::FillNextBuffer(bigtime_t event_time) { // get a buffer from our buffer group BBuffer* buf = mBufferGroup->RequestBuffer(mOutput.format.u.raw_audio.buffer_size, BufferDuration()); // if we fail to get a buffer (for example, if the request times out), we skip this // buffer and go on to the next, to avoid locking up the control thread if (!buf) { return NULL; } // now fill it with data, continuing where the last buffer left off // 20sep99: multichannel support size_t numFrames = mOutput.format.u.raw_audio.buffer_size / (sizeof(float)*mOutput.format.u.raw_audio.channel_count); bool stereo = (mOutput.format.u.raw_audio.channel_count == 2); if(!stereo) { ASSERT(mOutput.format.u.raw_audio.channel_count == 1); } // PRINT(("buffer: %ld, %ld frames, %s\n", mOutput.format.u.raw_audio.buffer_size, numFrames, stereo ? "stereo" : "mono")); float* data = (float*) buf->Data(); switch (mWaveform) { case SINE_WAVE: FillSineBuffer(data, numFrames, stereo); break; case TRIANGLE_WAVE: FillTriangleBuffer(data, numFrames, stereo); break; case SAWTOOTH_WAVE: FillSawtoothBuffer(data, numFrames, stereo); break; } // fill in the buffer header media_header* hdr = buf->Header(); hdr->type = B_MEDIA_RAW_AUDIO; hdr->size_used = mOutput.format.u.raw_audio.buffer_size; hdr->time_source = TimeSource()->ID(); bigtime_t stamp; if (RunMode() == B_RECORDING) { // In B_RECORDING mode, we stamp with the capture time. We're not // really a hardware capture node, but we simulate it by using the (precalculated) // time at which this buffer "should" have been created. stamp = event_time; } else { // okay, we're in one of the "live" performance run modes. in these modes, we // stamp the buffer with the time at which the buffer should be rendered to the // output, not with the capture time. mStartTime is the cached value of the // first buffer's performance time; we calculate this buffer's performance time as // an offset from that time, based on the amount of media we've created so far. // Recalculating every buffer like this avoids accumulation of error. stamp = mStartTime + bigtime_t(double(mFramesSent) / double(mOutput.format.u.raw_audio.frame_rate) * 1000000.0); } hdr->start_time = stamp; return buf; }
void Core::PlayBuffer(void* cookie, void* buffer, size_t size, const media_raw_audio_format& format) { size_t limit = (kLimit/gCronoSettings.Speed); bool stereo = format.channel_count == 2; printf("%f\n", limit / kFileFormat.u.raw_audio.frame_rate*2); kTicLen = kFileFormat.u.raw_audio.frame_rate/7; limit = limit*10; if (kSize >= limit) { //timeval start; //prev = start; //gettimeofday(&start, NULL); //printf("took %lu\n", start.tv_usec - prev.tv_usec); kSize -= limit; kSem = 0; } size_t remaining = 0; if (kSize+size > limit) { remaining = kSize+size - limit; size -= remaining; } if (kSem == 1) { memset(buffer, 0, size); kSize += size; //if (fillSize < kSize) { // fillSize = kSize - fillSize; // kSem = 0; //} if (remaining >= 0) { kSem = 0; size = remaining; } } if (kSem == 0) { switch(gCronoSettings.Engine) { case CRONO_SINE_ENGINE: FillSineBuffer((float*)buffer, size, stereo); break; case CRONO_TRIANGLE_ENGINE: FillTriangleBuffer((float*)buffer, size, true); break; case CRONO_SAWTOOTH_ENGINE: FillSawtoothBuffer((float*)buffer, size, stereo); break; } kSize += size; if (remaining > 0) { printf("defect\n"); memset(buffer, size-remaining, remaining); remaining = 0; } if (kSize >= kTicLen) { kSem = 1; } } }