5 #include "audiodevice.h"
11 #include "edlsession.h"
12 #include "levelwindow.h"
13 #include "mainsession.h"
14 #include "playabletracks.h"
15 #include "playbackengine.h"
16 #include "preferences.h"
17 #include "renderengine.h"
19 #include "transportque.h"
20 #include "virtualaconsole.h"
21 #include "virtualconsole.h"
22 #include "virtualnode.h"
24 ARender::ARender(RenderEngine *renderengine)
25 : CommonRender(renderengine)
27 // Clear output buffers
28 for(int i = 0; i < MAXCHANNELS; i++)
36 data_type = TRACK_AUDIO;
41 for(int i = 0; i < MAXCHANNELS; i++)
43 //printf("ARender::~ARender %p %p\n", audio_out[i], level_history[i]);
44 if(audio_out[i]) delete [] audio_out[i];
45 if(level_history[i]) delete [] level_history[i];
47 if(level_samples) delete [] level_samples;
50 void ARender::arm_command()
52 //printf("ARender::arm_command 1\n");
53 CommonRender::arm_command();
54 //printf("ARender::arm_command 1\n");
56 //printf("ARender::arm_command 1\n");
58 //printf("ARender::arm_command 2\n");
62 int ARender::get_total_tracks()
64 return renderengine->edl->tracks->total_audio_tracks();
67 Module* ARender::new_module(Track *track)
69 //printf("VirtualAConsole::new_module %p\n", track);
70 return new AModule(renderengine, this, 0, track);
73 int ARender::history_size()
79 meter_render_fragment = renderengine->edl->session->audio_module_fragment;
80 while(meter_render_fragment > renderengine->edl->session->sample_rate / 10)
81 meter_render_fragment /= 2;
83 renderengine->edl->session->audio_read_length /
84 meter_render_fragment;
89 int ARender::init_meters()
91 // not providing enough peaks results in peaks that are ahead of the sound
92 //printf("ARender::init_meters 1\n");
93 if(level_samples) delete [] level_samples;
94 level_samples = new int64_t[history_size()];
95 for(int i = 0; i < MAXCHANNELS;i++)
98 if(audio_out[i] && !level_history[i]) level_history[i] = new double[total_peaks];
101 //printf("ARender::init_meters 2\n");
102 for(int i = 0; i < history_size(); i++)
104 level_samples[i] = -1;
107 for(int j = 0; j < MAXCHANNELS; j++)
110 for(int i = 0; i < history_size(); i++)
111 level_history[j][i] = 0;
116 void ARender::init_output_buffers()
118 //printf("ARender::init_output_buffers %d\n", renderengine->adjusted_fragment_len);
119 if(renderengine->command->realtime)
121 for(int i = 0; i < MAXCHANNELS; i++)
123 // Reset the output buffers in case speed changed
126 delete [] audio_out[i];
130 //printf("ARender::init_output_buffers %d %d\n", i, renderengine->config->aconfig->do_channel[i]);
131 if(renderengine->config->aconfig->do_channel[i])
133 audio_out[i] = new double[renderengine->adjusted_fragment_len];
140 VirtualConsole* ARender::new_vconsole_object()
142 return new VirtualAConsole(renderengine, this);
145 int64_t ARender::tounits(double position, int round)
148 return Units::round(position * renderengine->edl->session->sample_rate);
150 return (int64_t)(position * renderengine->edl->session->sample_rate);
153 double ARender::fromunits(int64_t position)
155 return (double)position / renderengine->edl->session->sample_rate;
159 int ARender::process_buffer(double **buffer_out,
161 int64_t input_position,
165 //printf("ARender::process_buffer 1\n");
166 this->last_playback = last_buffer;
167 int64_t fragment_position = 0;
168 int64_t fragment_len = input_len;
170 current_position = input_position;
172 //printf("ARender::process_buffer 2\n");
173 while(fragment_position < input_len)
175 for(int i = 0; i < MAXCHANNELS; i++)
178 this->audio_out[i] = buffer_out[i] + fragment_position;
180 this->audio_out[i] = 0;
183 fragment_len = input_len;
184 if(fragment_position + fragment_len > input_len)
185 fragment_len = input_len - fragment_position;
187 //printf("ARender::process_buffer 3 %d\n", fragment_len);
188 reconfigure = vconsole->test_reconfigure(input_position,
192 //printf("ARender::process_buffer 4 %d %d %d\n", input_position, fragment_len, reconfigure);
195 if(reconfigure) restart_playback();
196 //printf("ARender::process_buffer 6\n");
198 result = process_buffer(fragment_len, input_position);
199 //printf("ARender::process_buffer 7\n");
201 fragment_position += fragment_len;
202 input_position += fragment_len;
203 current_position = input_position;
206 // Don't delete audio_out on completion
207 bzero(this->audio_out, sizeof(double*) * MAXCHANNELS);
208 // printf("ARender::process_buffer 1 %.2f %.2f %.2f %.2f %.2f\n",
213 // buffer_out[0][4]);
214 //printf("ARender::process_buffer 8\n");
220 int ARender::process_buffer(int64_t input_len, int64_t input_position)
222 //printf("ARender::process_buffer 1\n");
223 int result = ((VirtualAConsole*)vconsole)->process_buffer(input_len,
234 session_position += input_len;
238 int ARender::restart_playback()
240 // Use for rebuilding the virtual console during playback.
241 // Send last buffer to old thread.
244 send_reconfigure_buffer();
245 vconsole->wait_for_completion();
248 CommonRender::restart_playback();
252 int ARender::get_history_number(int64_t *table, int64_t position)
254 // Get the entry closest to position
256 int64_t min_difference = 0x7fffffff;
257 for(int i = 0; i < history_size(); i++)
259 //printf("%d %d ", i, table[i]);
260 if(labs(table[i] - position) < min_difference)
263 min_difference = labs(table[i] - position);
268 //printf("ARender::get_history_number %ld %d %d\n", position, result, min_difference);
273 int ARender::wait_device_completion()
275 // audio device should be entirely cleaned up by vconsole
276 renderengine->audio->wait_for_completion();
282 int64_t current_input_length;
284 //printf("ARender::run 1\n");
289 //printf("ARender::run 2 %d %d %d\n", done, interrupt, last_playback);
290 while(!done && !interrupt && !last_playback)
292 current_input_length = renderengine->edl->session->audio_read_length;
293 //printf("ARender::run 2 %d\n", current_input_length);
294 get_boundaries(current_input_length);
295 //printf("ARender::run 3 %d\n", current_input_length);
297 if(current_input_length)
299 //printf("ARender::run 4 %d\n", current_input_length);
300 reconfigure = vconsole->test_reconfigure(current_position,
301 current_input_length,
303 //printf("ARender::run 5 %d %d\n", reconfigure, current_input_length);
309 // test_virtualnodes(current_position,
310 // current_input_length,
312 if(reconfigure) restart_playback();
314 //printf("ARender::run 4 %d %d %d\n", current_position, current_input_length, reconfigure);
317 // Update tracking if no video is playing.
318 if(renderengine->command->realtime &&
319 renderengine->playback_engine &&
320 !renderengine->do_video)
322 double position = (double)renderengine->audio->current_position() /
323 renderengine->edl->session->sample_rate *
324 renderengine->command->get_speed();
326 if(renderengine->command->get_direction() == PLAY_FORWARD)
327 position += renderengine->command->playbackstart;
329 position = renderengine->command->playbackstart - position;
331 //printf("ARender::run 6 %f\n", renderengine->audio->current_position());
332 // This number is not compensated for looping. It's compensated in
333 // PlaybackEngine::get_tracking_position when interpolation also happens.
334 renderengine->playback_engine->update_tracking(position);
336 //printf("ARender::run 7 %d\n", current_position);
343 process_buffer(current_input_length, current_position);
344 //printf("ARender::run 8 %d %d %d\n", done, interrupt, last_playback);
346 advance_position(get_render_length(current_input_length));
347 if(vconsole->interrupt) interrupt = 1;
348 //printf("ARender::run 9 %d %d %d\n", done, interrupt, last_playback);
351 //printf("ARender::run 10\n");
352 if(renderengine->command->realtime) wait_device_completion();
353 //printf("ARender::run 11\n");
354 vconsole->stop_rendering(0);
355 //printf("ARender::run 12\n");
375 int ARender::get_datatype()
380 int ARender::arm_playback(int64_t current_position,
381 int64_t input_length,
382 int64_t amodule_render_fragment,
383 int64_t playback_buffer,
384 int64_t output_length)
386 this->current_position = current_position;
387 this->input_length = input_length;
388 session_position = 0;
391 source_length = renderengine->end_position - renderengine->start_position;
394 if(renderengine->command->realtime)
396 Thread::set_realtime(renderengine->edl->session->real_time_playback);
401 // start reading input and sending to arenderthread
402 // only if there's an audio device
403 if(renderengine->command->realtime)
411 int ARender::send_reconfigure_buffer()
413 if(renderengine->command->realtime)
415 vconsole->output_lock[vconsole->current_input_buffer]->lock();
417 vconsole->input_len[vconsole->current_input_buffer] = 0;
418 vconsole->input_position[vconsole->current_input_buffer] = 0;
419 vconsole->last_playback[vconsole->current_input_buffer] = 0;
420 vconsole->last_reconfigure[vconsole->current_input_buffer] = 1;
422 vconsole->input_lock[vconsole->current_input_buffer]->unlock();
423 vconsole->swap_input_buffer();
428 int ARender::reverse_buffer(double *buffer, int64_t len)
430 register int64_t start, end;
433 for(start = 0, end = len - 1; end > start; start++, end--)
435 temp = buffer[start];
436 buffer[start] = buffer[end];
441 int ARender::get_next_peak(int current_peak)
444 if(current_peak == total_peaks) current_peak = 0;
448 int64_t ARender::get_render_length(int64_t current_render_length)
450 return current_render_length;
453 int ARender::wait_for_startup()
456 startup_lock.unlock();