5 #include "audiodevice.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"
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++)
37 data_type = TRACK_AUDIO;
42 for(int i = 0; i < MAXCHANNELS; 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 // Need the meter history now so AModule can allocate its own history
53 calculate_history_size();
54 CommonRender::arm_command();
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()
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;
82 renderengine->fragment_len /
83 meter_render_fragment;
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++)
98 if(audio_out[i] && !level_history[i])
99 level_history[i] = new double[total_peaks];
102 for(int i = 0; i < total_peaks; i++)
104 level_samples[i] = -1;
107 for(int j = 0; j < MAXCHANNELS; j++)
110 for(int i = 0; i < total_peaks; i++)
111 level_history[j][i] = 0;
116 void ARender::init_output_buffers()
118 if(renderengine->command->realtime)
120 for(int i = 0; i < MAXCHANNELS; i++)
122 // Reset the output buffers in case speed changed
123 delete [] audio_out[i];
126 if(i < renderengine->edl->session->audio_channels)
128 audio_out[i] = new double[renderengine->adjusted_fragment_len];
135 VirtualConsole* ARender::new_vconsole_object()
137 return new VirtualAConsole(renderengine, this);
140 int64_t ARender::tounits(double position, int round)
143 return Units::round(position * renderengine->edl->session->sample_rate);
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,
156 int64_t input_position,
161 this->last_playback = last_buffer;
162 int64_t fragment_position = 0;
163 int64_t fragment_len = input_len;
165 current_position = input_position;
167 while(fragment_position < input_len)
169 for(int i = 0; i < MAXCHANNELS; i++)
172 this->audio_out[i] = buffer_out[i] + fragment_position;
174 this->audio_out[i] = 0;
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,
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;
196 // Don't delete audio_out on completion
197 bzero(this->audio_out, sizeof(double*) * MAXCHANNELS);
206 int ARender::process_buffer(int64_t input_len, int64_t input_position)
208 int result = ((VirtualAConsole*)vconsole)->process_buffer(input_len,
216 session_position += input_len;
220 int ARender::get_history_number(int64_t *table, int64_t position)
222 // Get the entry closest to position
224 int64_t min_difference = 0x7fffffff;
225 for(int i = 0; i < total_peaks; i++)
228 //printf("%lld ", table[i]);
229 if(labs(table[i] - position) < min_difference)
231 min_difference = labs(table[i] - position);
236 //printf("ARender::get_history_number %lld %d\n", position, 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();
254 int64_t current_input_length;
259 start_lock->unlock();
260 //printf("ARender::run 1 %d\n", Thread::calculate_realtime());
262 while(!done && !interrupt && !last_playback)
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)
271 reconfigure = vconsole->test_reconfigure(current_position,
272 current_input_length,
274 if(reconfigure) restart_playback();
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)
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;
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);
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;
315 if(!interrupt) send_last_buffer();
316 if(renderengine->command->realtime) wait_device_completion();
317 vconsole->stop_rendering(0);
338 int ARender::get_datatype()
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)
358 Thread::set_realtime(renderengine->edl->session->real_time_playback);
363 // start reading input and sending to arenderthread
364 // only if there's an audio device
365 if(renderengine->command->realtime)
373 // int ARender::send_reconfigure_buffer()
375 // if(renderengine->command->realtime)
377 // vconsole->output_lock[vconsole->current_input_buffer]->lock("ARender::send_reconfigure_buffer");
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;
384 // vconsole->input_lock[vconsole->current_input_buffer]->unlock();
385 // vconsole->swap_input_buffer();
390 int ARender::reverse_buffer(double *buffer, int64_t len)
392 register int64_t start, end;
395 for(start = 0, end = len - 1; end > start; start++, end--)
397 temp = buffer[start];
398 buffer[start] = buffer[end];
403 int ARender::get_next_peak(int current_peak)
406 if(current_peak >= total_peaks) current_peak = 0;
410 int64_t ARender::get_render_length(int64_t current_render_length)
412 return current_render_length;