r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / cinelerra / setaudio.C
blob76ebdd583bb0e61965e0200ac779a6295cfed577
1 #include "clip.h"
2 #include "colors.h"
3 #include "cplayback.h"
4 #include "cwindow.h"
5 #include "cwindowgui.h"
6 #include "datatype.h"
7 #include "edl.h"
8 #include "edlsession.h"
9 #include "levelwindow.h"
10 #include "levelwindowgui.h"
11 #include "mainundo.h"
12 #include "meterpanel.h"
13 #include "mwindow.h"
14 #include "mwindowgui.h"
15 #include "new.h"
16 #include "rotateframe.h"
17 #include "setaudio.h"
18 #include "theme.h"
19 #include "transportque.h"
20 #include "units.h"
21 #include "vframe.h"
22 #include "vwindow.h"
23 #include "vwindowgui.h"
26 #include <libintl.h>
27 #define _(String) gettext(String)
28 #define gettext_noop(String) String
29 #define N_(String) gettext_noop (String)
35 SetAudio::SetAudio(MWindow *mwindow)
36  :BC_MenuItem(_("Audio Setup..."))
38         this->mwindow = mwindow;
39         thread = new SetAudioThread(mwindow);
41 SetAudio::~SetAudio()
43         delete thread;
46 int SetAudio::handle_event()
48         if(!thread->running())
49         {
50                 thread->start();
51         }
52         else
53         {
54                 thread->window->raise_window();
55         }
56         return 1;
60 SetAudioThread::SetAudioThread(MWindow *mwindow)
61  : Thread()
63         this->mwindow = mwindow;
64         window = 0;
66 SetAudioThread::~SetAudioThread()
68         if(window)
69         {
70                 window->set_done(1);
71                 join();
72                 delete window;
73         }
76 void SetAudioThread::run()
78         new_settings = new EDL;
79         new_settings->create_objects();
80         new_settings->copy_session(mwindow->edl);
81         window = new SetAudioWindow(mwindow, this);
82         window->create_objects();
84         int result = window->run_window();
85         if(!result)
86         {
87                 long old_rate = mwindow->edl->session->sample_rate;
88                 long new_rate = new_settings->session->sample_rate;
89                 int old_channels = mwindow->edl->session->audio_channels;
90                 int new_channels = new_settings->session->audio_channels;
91         
92                 mwindow->undo->update_undo_before(_("set audio"), LOAD_EDITS | LOAD_SESSION);
93                 mwindow->edl->copy_session(new_settings);
94                 mwindow->edl->resample(old_rate, new_rate, TRACK_AUDIO);
95                 mwindow->undo->update_undo_after();
98 // Update GUIs
99                 mwindow->gui->lock_window();
100                 mwindow->gui->update(1,
101                         1,
102                         1,
103                         1,
104                         0, 
105                         1,
106                         0);
107                 mwindow->gui->unlock_window();
110                 mwindow->cwindow->gui->lock_window();
111                 mwindow->cwindow->gui->resize_event(mwindow->cwindow->gui->get_w(), 
112                         mwindow->cwindow->gui->get_h());
113                 mwindow->cwindow->gui->meters->set_meters(new_channels, 1);
114                 mwindow->cwindow->gui->flush();
115                 mwindow->cwindow->gui->unlock_window();
117                 mwindow->vwindow->gui->lock_window();
118                 mwindow->vwindow->gui->resize_event(mwindow->vwindow->gui->get_w(), 
119                         mwindow->vwindow->gui->get_h());
120                 mwindow->vwindow->gui->meters->set_meters(new_channels, 1);
121                 mwindow->vwindow->gui->flush();
122                 mwindow->vwindow->gui->unlock_window();
124                 mwindow->lwindow->gui->lock_window();
125                 mwindow->lwindow->gui->panel->set_meters(new_channels, 1);
126                 mwindow->lwindow->gui->flush();
127                 mwindow->lwindow->gui->unlock_window();
133 // Flash frame
134                 mwindow->cwindow->gui->lock_window();
135                 mwindow->cwindow->gui->slider->set_position();
136                 mwindow->cwindow->playback_engine->que->send_command(CURRENT_FRAME,
137                         CHANGE_ALL,
138                         mwindow->edl,
139                         1);
140                 mwindow->cwindow->gui->unlock_window();
141                 mwindow->save_backup();
142         }
143         delete window;
144         delete new_settings;
145         window = 0;
152 SetAudioWindow::SetAudioWindow(MWindow *mwindow, SetAudioThread *thread)
153  : BC_Window(PROGRAM_NAME ": Audio Setup",
154         mwindow->gui->get_abs_cursor_x(),
155         mwindow->gui->get_abs_cursor_y(),
156         320,
157         460,
158         -1,
159         -1,
160         1,
161         0,
162         1)
164         this->mwindow = mwindow;
165         this->thread = thread;
167 SetAudioWindow::~SetAudioWindow()
171 int SetAudioWindow::create_objects()
173         int x = 10, y = 10, x1;
174         BC_TextBox *textbox;
176         x1 = x;
177         add_subwindow(new BC_Title(x1, y, _("Samplerate:")));
178         x1 += 100;
179         add_subwindow(textbox = new SetSampleRateTextBox(thread, x1, y));
180         x1 += textbox->get_w();
181         add_subwindow(new SampleRatePulldown(mwindow, textbox, x1, y));
183         x1 = x;
184         y += textbox->get_h() + 5;
185         add_subwindow(new BC_Title(x1, y, _("Channels:")));
186         x1 += 100;
187         add_subwindow(textbox = new SetChannelsTextBox(thread, x1, y));
188         x1 += textbox->get_w();
189         add_subwindow(new BC_ITumbler(textbox, 1, MAXCHANNELS, x1, y));
191         x1 = x;
192         y += textbox->get_h() + 15;
193         add_subwindow(new BC_Title(x1, y, _("Channel positions:")));
194         y += 20;
195         add_subwindow(canvas = new SetChannelsCanvas(mwindow, thread, x, y));
196         canvas->draw();
198         add_subwindow(new BC_OKButton(this));
199         add_subwindow(new BC_CancelButton(this));
200         show_window();
201         return 0;
204 SetSampleRateTextBox::SetSampleRateTextBox(SetAudioThread *thread, int x, int y)
205  : BC_TextBox(x, y, 100, 1, (long)thread->new_settings->session->sample_rate)
207         this->thread = thread;
209 int SetSampleRateTextBox::handle_event()
211         thread->new_settings->session->sample_rate = CLIP(atol(get_text()), 1, 1000000);
212         return 1;
215 SetChannelsTextBox::SetChannelsTextBox(SetAudioThread *thread, int x, int y)
216  : BC_TextBox(x, y, 100, 1, thread->new_settings->session->audio_channels)
218         this->thread = thread;
220 int SetChannelsTextBox::handle_event()
222         thread->new_settings->session->audio_channels = CLIP(atol(get_text()), 0, MAXCHANNELS);
223         thread->window->canvas->draw();
224         return 1;
228 SetChannelsCanvas::SetChannelsCanvas(MWindow *mwindow, SetAudioThread *thread, int x, int y)
229  : BC_SubWindow(x, y, mwindow->theme->channel_bg_data->get_w(), mwindow->theme->channel_bg_data->get_h())
231 //printf("SetChannelsCanvas::SetChannelsCanvas 1\n");
232         this->thread = thread;
233         this->mwindow = mwindow;
234         active_channel = -1;
235 //printf("SetChannelsCanvas::SetChannelsCanvas 1\n");
236         box_r = mwindow->theme->channel_position_data->get_w() / 2;
237 //printf("SetChannelsCanvas::SetChannelsCanvas 1\n");
238         temp_picon = new VFrame(0, 
239                 mwindow->theme->channel_position_data->get_w(),
240                 mwindow->theme->channel_position_data->get_h(),
241                 mwindow->theme->channel_position_data->get_color_model());
242 //printf("SetChannelsCanvas::SetChannelsCanvas 1\n");
243         rotater = new RotateFrame(mwindow->edl->session->smp + 1,
244                 mwindow->theme->channel_position_data->get_w(),
245                 mwindow->theme->channel_position_data->get_h());
246 //printf("SetChannelsCanvas::SetChannelsCanvas 2\n");
248 SetChannelsCanvas::~SetChannelsCanvas()
250         delete temp_picon;
251         delete rotater;
254 int SetChannelsCanvas::draw(int angle)
256         set_color(RED);
257         int real_w = get_w() - box_r * 2;
258         int real_h = get_h() - box_r * 2;
259         int real_x = box_r;
260         int real_y = box_r;
262         draw_top_background(get_top_level(), 0, 0, get_w(), get_h());
263         draw_vframe(mwindow->theme->channel_bg_data, 0, 0);
268         int x, y, w, h;
269         char string[32];
270         set_color(mwindow->theme->channel_position_color);
271         for(int i = 0; i < thread->new_settings->session->audio_channels; i++)
272         {
273 //printf("SetChannelsCanvas::draw %d\n", thread->new_settings->session->achannel_positions[i]);
274                 get_dimensions(thread->new_settings->session->achannel_positions[i], x, y, w, h);
275                 double rotate_angle = thread->new_settings->session->achannel_positions[i];
276                 rotate_angle = -rotate_angle;
277                 while(rotate_angle < 0) rotate_angle += 360;
278                 rotater->rotate(temp_picon, 
279                         mwindow->theme->channel_position_data, 
280                         rotate_angle, 
281                         0);
283                 BC_Pixmap temp_pixmap(this, 
284                         temp_picon, 
285                         PIXMAP_ALPHA,
286                         0);
287                 draw_pixmap(&temp_pixmap, x, y);
288                 sprintf(string, "%d", i + 1);
289                 draw_text(x + 2, y + box_r * 2 - 2, string);
290         }
292         if(angle > -1)
293         {
294                 sprintf(string, _("%d degrees"), angle);
295                 draw_text(this->get_w() / 2 - 40, this->get_h() / 2, string);
296         }
298         flash();
299         return 0;
302 int SetChannelsCanvas::get_dimensions(int channel_position, 
303         int &x, 
304         int &y, 
305         int &w, 
306         int &h)
308         int real_w = this->get_w() - box_r * 2 - 50;
309         int real_h = this->get_h() - box_r * 2 - 50;
310         float corrected_position = channel_position;
311         if(corrected_position < 0) corrected_position += 360;
312         Units::polar_to_xy((float)corrected_position, real_w / 2, x, y);
313         x += real_w / 2 + 25;
314         y += real_h / 2 + 25;
315         w = box_r * 2;
316         h = box_r * 2;
317         return 0;
320 int SetChannelsCanvas::button_press_event()
322         if(!cursor_inside()) return 0;
323 // get active channel
324         for(int i = 0; 
325                 i < thread->new_settings->session->audio_channels; 
326                 i++)
327         {
328                 int x, y, w, h;
329                 get_dimensions(thread->new_settings->session->achannel_positions[i], 
330                         x, 
331                         y, 
332                         w, 
333                         h);
334                 if(get_cursor_x() > x && get_cursor_y() > y && 
335                         get_cursor_x() < x + w && get_cursor_y() < y + h)
336                 {
337                         active_channel = i;
338                         degree_offset = (int)Units::xy_to_polar(get_cursor_x() - this->get_w() / 2, get_cursor_y() - this->get_h() / 2);
339                         degree_offset += 90;
340                         if(degree_offset >= 360) degree_offset -= 360;
341                         degree_offset -= thread->new_settings->session->achannel_positions[i];
342                         draw(thread->new_settings->session->achannel_positions[i]);
343                         return 1;
344                 }
345         }
346         return 0;
349 int SetChannelsCanvas::button_release_event()
351         if(active_channel >= 0)
352         {
353                 active_channel = -1;
354                 draw(-1);
355                 return 1;
356         }
357         return 0;
360 int SetChannelsCanvas::cursor_motion_event()
362         if(active_channel >= 0)
363         {
364 // get degrees of new channel
365                 int new_d;
366                 new_d = (int)Units::xy_to_polar(get_cursor_x() - this->get_w() / 2, get_cursor_y() - this->get_h() / 2);
367                 new_d += 90;
368                 if(new_d >= 360) new_d -= 360;
369                 new_d -= degree_offset;
370                 if(new_d < 0) new_d += 360;
371                 if(thread->new_settings->session->achannel_positions[active_channel] != new_d)
372                 {
373                         thread->new_settings->session->achannel_positions[active_channel] = new_d;
374                         draw(thread->new_settings->session->achannel_positions[active_channel]);
375                 }
376                 return 1;
377         }
378         return 0;