r972: Fix aspect ratio of YUV4MPEG streams.
[cinelerra_cv/ct.git] / plugins / timestretch / timestretchengine.C
bloba8c44d3ff0f56b0ab8948b4b8c31759e9216f114
1 #include "timestretchengine.h"
4 #include <stdio.h>
5 #include <string.h>
7 // Size of window in milliseconds
8 #define WINDOW_TIME 40
11 TimeStretchEngine::TimeStretchEngine(double scale, int sample_rate)
13         output = 0;
14         output_allocation = 0;
15         output_size = 0;
16         input = 0;
17         input_allocation = 0;
18         input_size = 0;
19         input_sample = 0;
20         output_sample = 0;
21         this->scale = scale;
22         this->sample_rate = sample_rate;
23         window_time = WINDOW_TIME;
24         window_size = (int64_t)sample_rate * (int64_t)window_time / (int64_t)1000;
25         window_skirt = window_size / 2;
28 TimeStretchEngine::~TimeStretchEngine()
30         if(output) delete [] output;
31         if(input) delete [] input;
34 void TimeStretchEngine::overlay(double *out, double *in, int size, int skirt)
36 // Fade in
37         for(int i = 0; i < skirt; i++)
38         {
39                 *out = *out * (1.0 - (double)i / skirt) + *in * ((double)i / skirt);
40                 out++;
41                 in++;
42         }
44 // Center
45         for(int i = 0; i < size - skirt; i++)
46         {
47                 *out++ = *in++;
48         }
50 // New skirt
51         for(int i = 0; i < skirt; i++)
52         {
53                 *out++ = *in++;
54         }
57 int TimeStretchEngine::process(double *in_buffer, int in_size)
59 //printf("TimeStretchEngine::process 1\n");
60 // Stack on input buffer
61         if(input_size + in_size > input_allocation)
62         {
63                 int new_input_allocation = input_size + in_size;
64                 double *new_input = new double[new_input_allocation];
65                 if(input)
66                 {
67                         memcpy(new_input, input, input_size * sizeof(double));
68                         delete [] input;
69                 }
70                 input = new_input;
71                 input_allocation = new_input_allocation;
72         }
74 //printf("TimeStretchEngine::process 10\n");
76         memcpy(input + input_size, in_buffer, in_size * sizeof(double));
77         input_size += in_size;
79 //printf("TimeStretchEngine::process 20\n");
80 // Overlay windows from input buffer into output buffer
81         int done = 0;
82         do
83         {
84                 int64_t current_out_sample = output_sample + output_size;
85                 int64_t current_in_sample = (int64_t)((double)current_out_sample / scale);
87                 if(current_in_sample - input_sample + window_size + window_skirt > input_size)
88                 {
89 // Shift input buffer so the fragment that would have been copied now will be
90 // in the next iteration.
91 // printf("TimeStretchEngine::process 1 %lld %d\n", 
92 // current_in_sample - input_sample, 
93 // input_size);
94                         if(current_in_sample - input_sample < input_size)
95                                 memcpy(input,
96                                         input + current_in_sample - input_sample,
97                                         (input_size - (current_in_sample - input_sample)) * sizeof(double));
98                         input_size -= current_in_sample - input_sample;
99                         input_sample = current_in_sample;
100                         done = 1;
101 //printf("TimeStretchEngine::process 10\n");
102                 }
103                 else
104                 {
105 //printf("TimeStretchEngine::process 20\n");
106 // Allocate output buffer
107                         if(output_size + window_size + window_skirt > output_allocation)
108                         {
109                                 int new_allocation = output_size + window_size + window_skirt;
110                                 double *new_output = new double[new_allocation];
111                                 bzero(new_output, new_allocation * sizeof(double));
112                                 if(output)
113                                 {
114                                         memcpy(new_output, 
115                                                 output, 
116                                                 (output_size + window_skirt) * sizeof(double));
117                                         delete [] output;
118                                 }
119                                 output = new_output;
120                                 output_allocation = new_allocation;
121                         }
123 // Overlay new window
124                         overlay(output + output_size, 
125                                 input + current_in_sample - input_sample,
126                                 window_size,
127                                 window_skirt);
128                         output_size += window_size;
129 //printf("TimeStretchEngine::process 30 %d\n", output_size);
130                 }
131         }while(!done);
133 //printf("TimeStretchEngine::process 100 %d\n", output_size);
134         return output_size;
137 void TimeStretchEngine::read_output(double *buffer, int size)
139         memcpy(buffer, output, size * sizeof(double));
140         memcpy(output, output + size, (output_size + window_skirt - size) * sizeof(double));
141         output_size -= size;
142         output_sample += size;
145 double* TimeStretchEngine::get_samples()
147         return output;