Jack2 1.9.12

JackResampler.cpp
1 /*
2 Copyright (C) 2008 Grame
3
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.
8
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.
13
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.
17
18 */
19
20 #include "JackResampler.h"
21 #include "JackError.h"
22 #include <stdio.h>
23
24 namespace Jack
25 {
26
27 JackRingBuffer::JackRingBuffer(int size):fRingBufferSize(size)
28 {
29 fRingBuffer = jack_ringbuffer_create(sizeof(jack_default_audio_sample_t) * fRingBufferSize);
30 Reset(fRingBufferSize);
31 }
32
33 JackRingBuffer::~JackRingBuffer()
34 {
35 if (fRingBuffer) {
36 jack_ringbuffer_free(fRingBuffer);
37 }
38 }
39
40 void JackRingBuffer::Reset(unsigned int new_size)
41 {
42 fRingBufferSize = new_size;
43 jack_ringbuffer_reset(fRingBuffer);
44 jack_ringbuffer_reset_size(fRingBuffer, sizeof(jack_default_audio_sample_t) * fRingBufferSize);
45 jack_ringbuffer_read_advance(fRingBuffer, (sizeof(jack_default_audio_sample_t) * new_size/2));
46 }
47
48 unsigned int JackRingBuffer::ReadSpace()
49 {
50 return (jack_ringbuffer_read_space(fRingBuffer) / sizeof(jack_default_audio_sample_t));
51 }
52
53 unsigned int JackRingBuffer::WriteSpace()
54 {
55 return (jack_ringbuffer_write_space(fRingBuffer) / sizeof(jack_default_audio_sample_t));
56 }
57
58 unsigned int JackRingBuffer::Read(jack_default_audio_sample_t* buffer, unsigned int frames)
59 {
60 size_t len = jack_ringbuffer_read_space(fRingBuffer);
61 jack_log("JackRingBuffer::Read input available = %ld", len / sizeof(jack_default_audio_sample_t));
62
63 if (len < frames * sizeof(jack_default_audio_sample_t)) {
64 jack_error("JackRingBuffer::Read : producer too slow, missing frames = %d", frames);
65 return 0;
66 } else {
67 jack_ringbuffer_read(fRingBuffer, (char*)buffer, frames * sizeof(jack_default_audio_sample_t));
68 return frames;
69 }
70 }
71
72 unsigned int JackRingBuffer::Write(jack_default_audio_sample_t* buffer, unsigned int frames)
73 {
74 size_t len = jack_ringbuffer_write_space(fRingBuffer);
75 jack_log("JackRingBuffer::Write output available = %ld", len / sizeof(jack_default_audio_sample_t));
76
77 if (len < frames * sizeof(jack_default_audio_sample_t)) {
78 jack_error("JackRingBuffer::Write : consumer too slow, skip frames = %d", frames);
79 return 0;
80 } else {
81 jack_ringbuffer_write(fRingBuffer, (char*)buffer, frames * sizeof(jack_default_audio_sample_t));
82 return frames;
83 }
84 }
85
86 unsigned int JackRingBuffer::Read(void* buffer, unsigned int bytes)
87 {
88 size_t len = jack_ringbuffer_read_space(fRingBuffer);
89 jack_log("JackRingBuffer::Read input available = %ld", len);
90
91 if (len < bytes) {
92 jack_error("JackRingBuffer::Read : producer too slow, missing bytes = %d", bytes);
93 return 0;
94 } else {
95 jack_ringbuffer_read(fRingBuffer, (char*)buffer, bytes);
96 return bytes;
97 }
98 }
99
100 unsigned int JackRingBuffer::Write(void* buffer, unsigned int bytes)
101 {
102 size_t len = jack_ringbuffer_write_space(fRingBuffer);
103 jack_log("JackRingBuffer::Write output available = %ld", len);
104
105 if (len < bytes) {
106 jack_error("JackRingBuffer::Write : consumer too slow, skip bytes = %d", bytes);
107 return 0;
108 } else {
109 jack_ringbuffer_write(fRingBuffer, (char*)buffer, bytes);
110 return bytes;
111 }
112 }
113
114 unsigned int JackResampler::ReadResample(jack_default_audio_sample_t* buffer, unsigned int frames)
115 {
116 return Read(buffer, frames);
117 }
118
119 unsigned int JackResampler::WriteResample(jack_default_audio_sample_t* buffer, unsigned int frames)
120 {
121 return Write(buffer, frames);
122 }
123
124 }
jack_ringbuffer_read_space
size_t jack_ringbuffer_read_space(const jack_ringbuffer_t *rb)
jack_ringbuffer_create
jack_ringbuffer_t * jack_ringbuffer_create(size_t sz)
jack_error
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
jack_ringbuffer_write_space
size_t jack_ringbuffer_write_space(const jack_ringbuffer_t *rb)
jack_ringbuffer_free
void jack_ringbuffer_free(jack_ringbuffer_t *rb)
jack_ringbuffer_reset
void jack_ringbuffer_reset(jack_ringbuffer_t *rb)
jack_ringbuffer_write
size_t jack_ringbuffer_write(jack_ringbuffer_t *rb, const char *src, size_t cnt)
jack_ringbuffer_read_advance
void jack_ringbuffer_read_advance(jack_ringbuffer_t *rb, size_t cnt)
jack_ringbuffer_read
size_t jack_ringbuffer_read(jack_ringbuffer_t *rb, char *dest, size_t cnt)
jack_ringbuffer_reset_size
void jack_ringbuffer_reset_size(jack_ringbuffer_t *rb, size_t sz)
jack_log
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:108