Merge branch 'ct' of git.pipapo.org:cinelerra-ct into ct
[cinelerra_cv/ct.git] / cinelerra / arender.C
blob4c911b4182033395ca6221dfc837f7ca65944e0a
1 #include "amodule.h"
2 #include "arender.h"
3 #include "atrack.h"
4 #include "atrack.h"
5 #include "audiodevice.h"
6 #include "auto.h"
7 #include "autos.h"
8 #include "cache.h"
9 #include "condition.h"
10 #include "edit.h"
11 #include "edl.h"
12 #include "edlsession.h"
13 #include "levelwindow.h"
14 #include "mainsession.h"
15 #include "playabletracks.h"
16 #include "playbackengine.h"
17 #include "preferences.h"
18 #include "renderengine.h"
19 #include "tracks.h"
20 #include "transportque.h"
21 #include "virtualaconsole.h"
22 #include "virtualconsole.h"
23 #include "virtualnode.h"
25 ARender::ARender(RenderEngine *renderengine)
26  : CommonRender(renderengine)
28 // Clear output buffers
29         for(int i = 0; i < MAXCHANNELS; i++)
30         {
31                 audio_out[i] = 0;
32                 level_history[i] = 0;
33         }
34         level_samples = 0;
35         total_peaks = 0;
37         data_type = TRACK_AUDIO;
40 ARender::~ARender()
42         for(int i = 0; i < MAXCHANNELS; i++)
43         {
44                 if(audio_out[i]) delete [] audio_out[i];
45                 if(level_history[i]) delete [] level_history[i];
46         }
47         if(level_samples) delete [] level_samples;
50 void ARender::arm_command()
52 // Need the meter history now so AModule can allocate its own history
53         calculate_history_size();
54         CommonRender::arm_command();
55         asynchronous = 1;
56         init_meters();
60 int ARender::get_total_tracks()
62         return renderengine->edl->tracks->total_audio_tracks();
65 Module* ARender::new_module(Track *track)
67         return new AModule(renderengine, this, 0, track);
70 int ARender::calculate_history_size()
72         if(total_peaks > 0)
73                 return total_peaks;
74         else
75         {
76                 meter_render_fragment = renderengine->fragment_len;
77 // This number and the timer in tracking.C determine the rate
78                 while(meter_render_fragment > 
79                         renderengine->edl->session->sample_rate / TRACKING_RATE) 
80                         meter_render_fragment /= 2;
81                 total_peaks = 16 * 
82                         renderengine->fragment_len / 
83                         meter_render_fragment;
84                 return total_peaks;
85         }
88 int ARender::init_meters()
90 // not providing enough peaks results in peaks that are ahead of the sound
91         if(level_samples) delete [] level_samples;
92         calculate_history_size();
93         level_samples = new int64_t[total_peaks];
95         for(int i = 0; i < MAXCHANNELS;i++)
96         {
97                 current_level[i] = 0;
98                 if(audio_out[i] && !level_history[i]) 
99                         level_history[i] = new double[total_peaks];
100         }
102         for(int i = 0; i < total_peaks; i++)
103         {
104                 level_samples[i] = -1;
105         }
106         
107         for(int j = 0; j < MAXCHANNELS; j++)
108         {
109                 if(audio_out[j]) 
110                         for(int i = 0; i < total_peaks; i++)
111                                 level_history[j][i] = 0;
112         }
113         return 0;
116 void ARender::init_output_buffers()
118         if(renderengine->command->realtime)
119         {
120                 for(int i = 0; i < MAXCHANNELS; i++)
121                 {
122 // Reset the output buffers in case speed changed
123                         delete [] audio_out[i];
124                         audio_out[i] = 0;
126                         if(i < renderengine->edl->session->audio_channels)
127                         {
128                                 audio_out[i] = new double[renderengine->adjusted_fragment_len];
129                         }
130                 }
131         }
135 VirtualConsole* ARender::new_vconsole_object() 
137         return new VirtualAConsole(renderengine, this);
140 int64_t ARender::tounits(double position, int round)
142         if(round)
143                 return Units::round(position * renderengine->edl->session->sample_rate);
144         else
145                 return (int64_t)(position * renderengine->edl->session->sample_rate);
148 double ARender::fromunits(int64_t position)
150         return (double)position / renderengine->edl->session->sample_rate;
154 int ARender::process_buffer(double **buffer_out, 
155         int64_t input_len, 
156         int64_t input_position, 
157         int last_buffer)
159         int result = 0;
161         this->last_playback = last_buffer;
162         int64_t fragment_position = 0;
163         int64_t fragment_len = input_len;
164         int reconfigure = 0;
165         current_position = input_position;
167         while(fragment_position < input_len)
168         {
169                 for(int i = 0; i < MAXCHANNELS; i++)
170                 {
171                         if(buffer_out[i])
172                                 this->audio_out[i] = buffer_out[i] + fragment_position;
173                         else
174                                 this->audio_out[i] = 0;
175                 }
177                 fragment_len = input_len;
178                 if(fragment_position + fragment_len > input_len)
179                         fragment_len = input_len - fragment_position;
181                 reconfigure = vconsole->test_reconfigure(input_position, 
182                         fragment_len,
183                         last_playback);
185 //printf("ARender::process_buffer 1 %lld %d\n", input_position, reconfigure);
187                 if(reconfigure) restart_playback();
189                 result = process_buffer(fragment_len, input_position);
191                 fragment_position += fragment_len;
192                 input_position += fragment_len;
193                 current_position = input_position;
194         }
196 // Don't delete audio_out on completion
197         bzero(this->audio_out, sizeof(double*) * MAXCHANNELS);
201         return result;
202         return 0;
206 int ARender::process_buffer(int64_t input_len, int64_t input_position)
208         int result = ((VirtualAConsole*)vconsole)->process_buffer(input_len,
209                 input_position,
210                 last_playback,
211                 session_position);
215 // advance counters
216         session_position += input_len;
217         return result;
220 int ARender::get_history_number(int64_t *table, int64_t position)
222 // Get the entry closest to position
223         int result = 0;
224         int64_t min_difference = 0x7fffffff;
225         for(int i = 0; i < total_peaks; i++)
226         {
228 //printf("%lld ", table[i]);
229                 if(labs(table[i] - position) < min_difference)
230                 {
231                         min_difference = labs(table[i] - position);
232                         result = i;
233                 }
234         }
235 //printf("\n");
236 //printf("ARender::get_history_number %lld %d\n", position, result);
237         return result;
240 void ARender::send_last_buffer()
242         renderengine->audio->set_last_buffer();
245 int ARender::wait_device_completion()
247 // audio device should be entirely cleaned up by vconsole
248         renderengine->audio->wait_for_completion();
249         return 0;
252 void ARender::run()
254         int64_t current_input_length;
255         int reconfigure = 0;
257         first_buffer = 1;
259         start_lock->unlock();
260 //printf("ARender::run 1 %d\n", Thread::calculate_realtime());
262         while(!done && !interrupt && !last_playback)
263         {
264                 current_input_length = renderengine->fragment_len;
266                 get_boundaries(current_input_length);
268 //printf("ARender::run 10 %lld %lld\n", current_position, current_input_length);
269                 if(current_input_length)
270                 {
271                         reconfigure = vconsole->test_reconfigure(current_position, 
272                                 current_input_length,
273                                 last_playback);
274                         if(reconfigure) restart_playback();
275                 }
276 //printf("ARender::run 20 %lld %lld\n", current_position, current_input_length);
279 // Update tracking if no video is playing.
280                 if(renderengine->command->realtime && 
281                         renderengine->playback_engine &&
282                         !renderengine->do_video)
283                 {
284                         double position = (double)renderengine->audio->current_position() / 
285                                 renderengine->edl->session->sample_rate * 
286                                 renderengine->command->get_speed();
288                         if(renderengine->command->get_direction() == PLAY_FORWARD) 
289                                 position += renderengine->command->playbackstart;
290                         else
291                                 position = renderengine->command->playbackstart - position;
293 // This number is not compensated for looping.  It's compensated in 
294 // PlaybackEngine::get_tracking_position when interpolation also happens.
295                         renderengine->playback_engine->update_tracking(position);
296                 }
300 //printf("ARender::run 30 %lld\n", current_input_length);
304                 process_buffer(current_input_length, current_position);
305 //printf("ARender::run 40\n");
308                 advance_position(get_render_length(current_input_length));
309 //printf("ARender::run 50\n");
312                 if(vconsole->interrupt) interrupt = 1;
313         }
315         if(!interrupt) send_last_buffer();
316         if(renderengine->command->realtime) wait_device_completion();
317         vconsole->stop_rendering(0);
318         stop_plugins();
338 int ARender::get_datatype()
340         return TRACK_AUDIO;
343 int ARender::arm_playback(int64_t current_position,
344                         int64_t input_length, 
345                         int64_t amodule_render_fragment, 
346                         int64_t playback_buffer, 
347                         int64_t output_length)
349         this->current_position = current_position;
350         this->input_length = input_length;
351         session_position = 0;
353         source_length = renderengine->end_position - renderengine->start_position;
356         if(renderengine->command->realtime)
357         {
358                 Thread::set_realtime(renderengine->edl->session->real_time_playback);
359                 init_meters();
360         }
363 // start reading input and sending to arenderthread
364 // only if there's an audio device
365         if(renderengine->command->realtime)     
366         {
367                 set_synchronous(1);
368                 start();
369         }
370         return 0;
373 // int ARender::send_reconfigure_buffer()
374 // {
375 //      if(renderengine->command->realtime)
376 //      {
377 //              vconsole->output_lock[vconsole->current_input_buffer]->lock("ARender::send_reconfigure_buffer");
378 // 
379 //              vconsole->input_len[vconsole->current_input_buffer] = 0;
380 //              vconsole->input_position[vconsole->current_input_buffer] = 0;
381 //              vconsole->last_playback[vconsole->current_input_buffer] = 0;
382 //              vconsole->last_reconfigure[vconsole->current_input_buffer] = 1;
383 // 
384 //              vconsole->input_lock[vconsole->current_input_buffer]->unlock();
385 //              vconsole->swap_input_buffer();
386 //      }
387 //      return 0;
388 // }
390 int ARender::reverse_buffer(double *buffer, int64_t len)
392         register int64_t start, end;
393         double temp;
395         for(start = 0, end = len - 1; end > start; start++, end--)
396         {
397                 temp = buffer[start];
398                 buffer[start] = buffer[end];
399                 buffer[end] = temp;
400         }
403 int ARender::get_next_peak(int current_peak)
405         current_peak++;
406         if(current_peak >= total_peaks) current_peak = 0;
407         return current_peak;
410 int64_t ARender::get_render_length(int64_t current_render_length)
412         return current_render_length;
416 //      Local Variables:
417 //      mode: C++
418 //      c-file-style: "linux"
419 //      End: