Jack2 1.9.6
|
00001 /* 00002 Copyright (C) 2008 Grame 00003 00004 This program is free software; you can redistribute it and/or modify 00005 it under the terms of the GNU General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or 00007 (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program; if not, write to the Free Software 00016 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00017 00018 */ 00019 00020 #include "JackResampler.h" 00021 #include <stdio.h> 00022 00023 namespace Jack 00024 { 00025 00026 JackResampler::JackResampler() 00027 :fRatio(1),fRingBufferSize(DEFAULT_RB_SIZE) 00028 { 00029 fRingBuffer = jack_ringbuffer_create(sizeof(float) * fRingBufferSize); 00030 jack_ringbuffer_read_advance(fRingBuffer, (sizeof(float) * fRingBufferSize) / 2); 00031 } 00032 00033 JackResampler::~JackResampler() 00034 { 00035 if (fRingBuffer) 00036 jack_ringbuffer_free(fRingBuffer); 00037 } 00038 00039 void JackResampler::Reset(unsigned int new_size) 00040 { 00041 fRingBufferSize = new_size; 00042 jack_ringbuffer_reset_size(fRingBuffer, sizeof(float) * fRingBufferSize); 00043 jack_ringbuffer_read_advance(fRingBuffer, (sizeof(float) * fRingBufferSize / 2)); 00044 } 00045 00046 unsigned int JackResampler::ReadSpace() 00047 { 00048 return (jack_ringbuffer_read_space(fRingBuffer) / sizeof(float)); 00049 } 00050 00051 unsigned int JackResampler::WriteSpace() 00052 { 00053 return (jack_ringbuffer_write_space(fRingBuffer) / sizeof(float)); 00054 } 00055 00056 unsigned int JackResampler::Read(float* buffer, unsigned int frames) 00057 { 00058 size_t len = jack_ringbuffer_read_space(fRingBuffer); 00059 jack_log("JackResampler::Read input available = %ld", len / sizeof(float)); 00060 00061 if (len < frames * sizeof(float)) { 00062 jack_error("JackResampler::Read : producer too slow, missing frames = %d", frames); 00063 return 0; 00064 } else { 00065 jack_ringbuffer_read(fRingBuffer, (char*)buffer, frames * sizeof(float)); 00066 return frames; 00067 } 00068 } 00069 00070 unsigned int JackResampler::Write(float* buffer, unsigned int frames) 00071 { 00072 size_t len = jack_ringbuffer_write_space(fRingBuffer); 00073 jack_log("JackResampler::Write output available = %ld", len / sizeof(float)); 00074 00075 if (len < frames * sizeof(float)) { 00076 jack_error("JackResampler::Write : consumer too slow, skip frames = %d", frames); 00077 return 0; 00078 } else { 00079 jack_ringbuffer_write(fRingBuffer, (char*)buffer, frames * sizeof(float)); 00080 return frames; 00081 } 00082 } 00083 00084 unsigned int JackResampler::ReadResample(float* buffer, unsigned int frames) 00085 { 00086 return Read(buffer, frames); 00087 } 00088 00089 unsigned int JackResampler::WriteResample(float* buffer, unsigned int frames) 00090 { 00091 return Write(buffer, frames); 00092 } 00093 00094 }