Interpolation -> LibSamplerateInterpolation, keep state per channel
[ardour.git] / libs / ardour / interpolation.cc
1 #include <stdint.h>
2
3 #include "ardour/interpolation.h"
4
5 using namespace ARDOUR;
6
7 LibSamplerateInterpolation::LibSamplerateInterpolation()  : _speed (1.0L), state (0)
8 {
9 }
10
11 LibSamplerateInterpolation::~LibSamplerateInterpolation() 
12 {
13         for (int i = 0; i < state.size(); i++) {
14                 state[i] = src_delete (state[i]);
15         }
16 }
17
18 void
19 LibSamplerateInterpolation::set_speed (double new_speed)
20
21         _speed = new_speed; 
22         for (int i = 0; i < state.size(); i++) {
23                 src_set_ratio (state[i], 1.0/_speed);
24         }
25 }
26
27 void
28 LibSamplerateInterpolation::reset_state ()
29 {
30         printf("INTERPOLATION: reset_state()\n");
31         for (int i = 0; i < state.size(); i++) {
32                 if (state[i]) {
33                         src_reset (state[i]);
34                 } else {
35                         state[i] = src_new (SRC_SINC_FASTEST, 1, &error);
36                 }
37         }
38 }
39
40 void
41 LibSamplerateInterpolation::add_channel_to (int input_buffer_size, int output_buffer_size) 
42 {
43         SRC_DATA* newdata = new SRC_DATA;
44         
45         /* Set up sample rate converter info. */
46         newdata->end_of_input = 0 ; 
47
48         newdata->input_frames  = input_buffer_size;
49         newdata->output_frames = output_buffer_size;
50
51         newdata->input_frames_used = 0 ;
52         newdata->output_frames_gen = 0 ;
53
54         newdata->src_ratio = 1.0/_speed;
55         
56         data.push_back (newdata);
57         state.push_back (0);
58         
59         reset_state ();
60 }
61
62 void
63 LibSamplerateInterpolation::remove_channel_from () 
64 {
65         delete data.back ();
66         data.pop_back ();
67         delete state.back ();
68         state.pop_back ();
69         reset_state ();
70 }
71
72 nframes_t
73 LibSamplerateInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
74 {       
75         if (!data.size ()) {
76                 printf ("ERROR: trying to interpolate with no channels\n");
77                 return 0;
78         }
79         
80         data[channel]->data_in       = input;
81         data[channel]->data_out      = output;
82         
83         data[channel]->input_frames  = nframes * _speed;
84         data[channel]->output_frames = nframes;
85         data[channel]->src_ratio     = 1.0/_speed; 
86
87         if ((error = src_process (state[channel], data[channel]))) {    
88                 printf ("\nError : %s\n\n", src_strerror (error));
89                 exit (1);
90         }
91         
92         //printf("INTERPOLATION: channel %d input_frames_used: %d\n", channel, data[channel]->input_frames_used);
93         
94         return data[channel]->input_frames_used;
95 }