Merge pull request #23 from jackaudio/device_reservation_fixes
[jack2.git] / common / JackResampler.cpp
bloba9183c4267d043eaf3b83acdbf38b620467a4334
1 /*
2 Copyright (C) 2008 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "JackResampler.h"
21 #include "JackError.h"
22 #include <stdio.h>
24 namespace Jack
27 JackResampler::JackResampler()
28 :fRatio(1), fRingBufferSize(DEFAULT_RB_SIZE)
30 fRingBuffer = jack_ringbuffer_create(sizeof(jack_default_audio_sample_t) * fRingBufferSize);
31 jack_ringbuffer_read_advance(fRingBuffer, (sizeof(jack_default_audio_sample_t) * fRingBufferSize) / 2);
34 JackResampler::~JackResampler()
36 if (fRingBuffer) {
37 jack_ringbuffer_free(fRingBuffer);
41 void JackResampler::Reset(unsigned int new_size)
43 fRingBufferSize = new_size;
44 jack_ringbuffer_reset(fRingBuffer);
45 jack_ringbuffer_reset_size(fRingBuffer, sizeof(jack_default_audio_sample_t) * fRingBufferSize);
46 jack_ringbuffer_read_advance(fRingBuffer, (sizeof(jack_default_audio_sample_t) * fRingBufferSize / 2));
49 unsigned int JackResampler::ReadSpace()
51 return (jack_ringbuffer_read_space(fRingBuffer) / sizeof(jack_default_audio_sample_t));
54 unsigned int JackResampler::WriteSpace()
56 return (jack_ringbuffer_write_space(fRingBuffer) / sizeof(jack_default_audio_sample_t));
59 unsigned int JackResampler::Read(jack_default_audio_sample_t* buffer, unsigned int frames)
61 size_t len = jack_ringbuffer_read_space(fRingBuffer);
62 jack_log("JackResampler::Read input available = %ld", len / sizeof(jack_default_audio_sample_t));
64 if (len < frames * sizeof(jack_default_audio_sample_t)) {
65 jack_error("JackResampler::Read : producer too slow, missing frames = %d", frames);
66 return 0;
67 } else {
68 jack_ringbuffer_read(fRingBuffer, (char*)buffer, frames * sizeof(jack_default_audio_sample_t));
69 return frames;
73 unsigned int JackResampler::Write(jack_default_audio_sample_t* buffer, unsigned int frames)
75 size_t len = jack_ringbuffer_write_space(fRingBuffer);
76 jack_log("JackResampler::Write output available = %ld", len / sizeof(jack_default_audio_sample_t));
78 if (len < frames * sizeof(jack_default_audio_sample_t)) {
79 jack_error("JackResampler::Write : consumer too slow, skip frames = %d", frames);
80 return 0;
81 } else {
82 jack_ringbuffer_write(fRingBuffer, (char*)buffer, frames * sizeof(jack_default_audio_sample_t));
83 return frames;
87 unsigned int JackResampler::Read(void* buffer, unsigned int bytes)
89 size_t len = jack_ringbuffer_read_space(fRingBuffer);
90 jack_log("JackResampler::Read input available = %ld", len);
92 if (len < bytes) {
93 jack_error("JackResampler::Read : producer too slow, missing bytes = %d", bytes);
94 return 0;
95 } else {
96 jack_ringbuffer_read(fRingBuffer, (char*)buffer, bytes);
97 return bytes;
101 unsigned int JackResampler::Write(void* buffer, unsigned int bytes)
103 size_t len = jack_ringbuffer_write_space(fRingBuffer);
104 jack_log("JackResampler::Write output available = %ld", len);
106 if (len < bytes) {
107 jack_error("JackResampler::Write : consumer too slow, skip bytes = %d", bytes);
108 return 0;
109 } else {
110 jack_ringbuffer_write(fRingBuffer, (char*)buffer, bytes);
111 return bytes;
115 unsigned int JackResampler::ReadResample(jack_default_audio_sample_t* buffer, unsigned int frames)
117 return Read(buffer, frames);
120 unsigned int JackResampler::WriteResample(jack_default_audio_sample_t* buffer, unsigned int frames)
122 return Write(buffer, frames);