JackAudioAdapterInterface.h 6.53 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
Copyright (C) 2008 Grame

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

sletz's avatar
sletz committed
20
21
#ifndef __JackAudioAdapterInterface__
#define __JackAudioAdapterInterface__
22

sletz's avatar
sletz committed
23
#include "JackResampler.h"
sletz's avatar
sletz committed
24
#include "JackFilters.h"
sletz's avatar
sletz committed
25
#include "JackConstants.h"
sletz's avatar
sletz committed
26
#include <stdio.h>
sletz's avatar
sletz committed
27

28
29
30
namespace Jack
{

31
32
#ifdef JACK_MONITOR

sletz's avatar
sletz committed
33
34
#define TABLE_MAX 100000

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    struct Measure
    {
        int delta;
        int time1;
        int time2;
        float r1;
        float r2;
        int pos1;
        int pos2;
    };

    struct MeasureTable
    {

        Measure fTable[TABLE_MAX];
        int fCount;

        MeasureTable() :fCount ( 0 )
        {}
sletz's avatar
sletz committed
54

sletz's avatar
sletz committed
55
56
        void Write(int time1, int time2, float r1, float r2, int pos1, int pos2);
        void Save(unsigned int fHostBufferSize, unsigned int fHostSampleRate, unsigned int fAdaptedSampleRate, unsigned int fAdaptedBufferSize);
sletz's avatar
sletz committed
57

58
    };
sletz's avatar
sletz committed
59

60
61
#endif

62
63
64
    /*!
    \brief Base class for audio adapters.
    */
sletz's avatar
sletz committed
65

66
67
    class JackAudioAdapterInterface
    {
moret's avatar
moret committed
68

69
    protected:
moret's avatar
moret committed
70

71
#ifdef JACK_MONITOR
72
        MeasureTable fTable;
73
#endif
74
75
76
77
78
79
80
81
82
83
84
85
        //channels
        int fCaptureChannels;
        int fPlaybackChannels;

        //host parameters
        jack_nframes_t fHostBufferSize;
        jack_nframes_t fHostSampleRate;

        //adapted parameters
        jack_nframes_t fAdaptedBufferSize;
        jack_nframes_t fAdaptedSampleRate;

sletz's avatar
sletz committed
86
87
        //PI controler
        JackPIControler fPIControler;
88
89
90

        JackResampler** fCaptureRingBuffer;
        JackResampler** fPlaybackRingBuffer;
sletz's avatar
sletz committed
91

92
        unsigned int fQuality;
sletz's avatar
sletz committed
93
        unsigned int fRingbufferCurSize;
sletz's avatar
sletz committed
94
        jack_time_t fPullAndPushTime;
sletz's avatar
sletz committed
95

96
        bool fRunning;
sletz's avatar
sletz committed
97
        bool fAdaptative;
sletz's avatar
sletz committed
98

sletz's avatar
sletz committed
99
        void ResetRingBuffers();
sletz's avatar
sletz committed
100
101
        void AdaptRingBufferSize();
        void GrowRingBufferSize();
sletz's avatar
sletz committed
102

103
104
    public:

sletz's avatar
sletz committed
105
         JackAudioAdapterInterface ( jack_nframes_t buffer_size, jack_nframes_t sample_rate, jack_nframes_t ring_buffer_size = DEFAULT_ADAPTATIVE_SIZE):
sletz's avatar
sletz committed
106
107
108
109
110
111
112
113
            fCaptureChannels ( 0 ),
            fPlaybackChannels ( 0 ),
            fHostBufferSize ( buffer_size ),
            fHostSampleRate ( sample_rate ),
            fAdaptedBufferSize ( buffer_size),
            fAdaptedSampleRate ( sample_rate ),
            fPIControler(sample_rate / sample_rate, 256),
            fCaptureRingBuffer(NULL), fPlaybackRingBuffer(NULL),
sletz's avatar
sletz committed
114
            fQuality(0),
sletz's avatar
sletz committed
115
            fRingbufferCurSize(ring_buffer_size),
sletz's avatar
sletz committed
116
            fPullAndPushTime(0),
sletz's avatar
sletz committed
117
118
            fRunning(false),
            fAdaptative(true)
sletz's avatar
sletz committed
119
120
121
122
        {}
        JackAudioAdapterInterface ( jack_nframes_t host_buffer_size, 
                                    jack_nframes_t host_sample_rate,
                                    jack_nframes_t adapted_buffer_size,
sletz's avatar
sletz committed
123
124
                                    jack_nframes_t adapted_sample_rate,
                                    jack_nframes_t ring_buffer_size = DEFAULT_ADAPTATIVE_SIZE ) :
sletz's avatar
sletz committed
125
126
127
128
129
130
                fCaptureChannels ( 0 ),
                fPlaybackChannels ( 0 ),
                fHostBufferSize ( host_buffer_size ),
                fHostSampleRate ( host_sample_rate ),
                fAdaptedBufferSize ( adapted_buffer_size),
                fAdaptedSampleRate ( adapted_sample_rate ),
sletz's avatar
sletz committed
131
                fPIControler(host_sample_rate / host_sample_rate, 256),
132
                fQuality(0),
sletz's avatar
sletz committed
133
                fRingbufferCurSize(ring_buffer_size),
sletz's avatar
sletz committed
134
                fPullAndPushTime(0),
135
136
                fRunning(false),
                fAdaptative(true)
137
138
139
140
        {}

        virtual ~JackAudioAdapterInterface()
        {}
moret's avatar
moret committed
141

sletz's avatar
sletz committed
142
        virtual void Reset();
sletz's avatar
sletz committed
143

sletz's avatar
sletz committed
144
145
        virtual void Create();
        virtual void Destroy();
sletz's avatar
sletz committed
146

sletz's avatar
sletz committed
147
        virtual int Open()
148
        {
sletz's avatar
sletz committed
149
            return 0;
150
        }
sletz's avatar
sletz committed
151

sletz's avatar
sletz committed
152
        virtual int Close()
153
        {
sletz's avatar
sletz committed
154
            return 0;
155
        }
156
157
158
159

        virtual int SetHostBufferSize ( jack_nframes_t buffer_size )
        {
            fHostBufferSize = buffer_size;
sletz's avatar
sletz committed
160
            if (fAdaptative)
sletz's avatar
sletz committed
161
                AdaptRingBufferSize();
162
163
164
165
166
167
            return 0;
        }

        virtual int SetAdaptedBufferSize ( jack_nframes_t buffer_size )
        {
            fAdaptedBufferSize = buffer_size;
sletz's avatar
sletz committed
168
            if (fAdaptative)
sletz's avatar
sletz committed
169
                AdaptRingBufferSize();
170
171
172
173
174
175
176
177
178
179
180
181
182
            return 0;
        }

        virtual int SetBufferSize ( jack_nframes_t buffer_size )
        {
            SetHostBufferSize ( buffer_size );
            SetAdaptedBufferSize ( buffer_size );
            return 0;
        }

        virtual int SetHostSampleRate ( jack_nframes_t sample_rate )
        {
            fHostSampleRate = sample_rate;
sletz's avatar
sletz committed
183
            fPIControler.Init(double(fHostSampleRate) / double(fAdaptedSampleRate));
184
185
186
187
188
189
            return 0;
        }

        virtual int SetAdaptedSampleRate ( jack_nframes_t sample_rate )
        {
            fAdaptedSampleRate = sample_rate;
sletz's avatar
sletz committed
190
            fPIControler.Init(double(fHostSampleRate) / double(fAdaptedSampleRate));
191
192
193
194
195
196
197
198
199
            return 0;
        }

        virtual int SetSampleRate ( jack_nframes_t sample_rate )
        {
            SetHostSampleRate ( sample_rate );
            SetAdaptedSampleRate ( sample_rate );
            return 0;
        }
sletz's avatar
sletz committed
200

201
202
203
204
205
206
207
208
209
210
211
212
213
214
        void SetInputs ( int inputs )
        {
            jack_log ( "JackAudioAdapterInterface::SetInputs %d", inputs );
            fCaptureChannels = inputs;
        }

        void SetOutputs ( int outputs )
        {
            jack_log ( "JackAudioAdapterInterface::SetOutputs %d", outputs );
            fPlaybackChannels = outputs;
        }

        int GetInputs()
        {
sletz's avatar
sletz committed
215
            //jack_log("JackAudioAdapterInterface::GetInputs %d", fCaptureChannels);
216
217
218
219
220
            return fCaptureChannels;
        }

        int GetOutputs()
        {
sletz's avatar
sletz committed
221
            //jack_log ("JackAudioAdapterInterface::GetOutputs %d", fPlaybackChannels);
222
223
            return fPlaybackChannels;
        }
sletz's avatar
sletz committed
224
        
sletz's avatar
sletz committed
225
226
        int PushAndPull(jack_default_audio_sample_t** inputBuffer, jack_default_audio_sample_t** outputBuffer, unsigned int frames);
        int PullAndPush(jack_default_audio_sample_t** inputBuffer, jack_default_audio_sample_t** outputBuffer, unsigned int frames);
sletz's avatar
sletz committed
227
  
228
    };
sletz's avatar
sletz committed
229

230
231
232
}

#endif