+
+ProxyBeep::ProxyBeep(MWindow *mwindow)
+ : Thread(1, 0, 0)
+{
+ this->mwindow = mwindow;
+ audio = new AudioDevice(mwindow);
+ playing_audio = 0;
+ interrupted = -1;
+}
+
+ProxyBeep::~ProxyBeep()
+{
+ delete audio;
+}
+
+void ProxyBeep::run()
+{
+ int channels = 2;
+ int64_t bfrsz = BEEP_SAMPLE_RATE;
+ EDL *edl = mwindow->edl;
+ EDLSession *session = edl->session;
+ AudioOutConfig *aconfig = session->playback_config->aconfig;
+ audio->open_output(aconfig, BEEP_SAMPLE_RATE, bfrsz, channels, 0);
+ audio->start_playback();
+
+ double out0[bfrsz], out1[bfrsz], *out[2] = { out0, out1 };
+ const double two_pi = 2*M_PI;
+ int64_t audio_size = BEEP_SAMPLE_RATE * secs;
+ int audio_len = audio_size/sizeof(audio_data_t);
+ const double dt = two_pi * freq/BEEP_SAMPLE_RATE;
+ double th = 0;
+
+ audio_pos = 0;
+ playing_audio = 1;
+ while( !interrupted ) {
+ int len = audio_len - audio_pos;
+ if( len <= 0 ) break;
+ if( len > bfrsz ) len = bfrsz;
+ int k = audio_pos;
+ for( int i=0; i<len; ++i,++k,th+=dt ) {
+ double t = th - two_pi;
+ if( t >= 0 ) th = t;
+ out0[i] = out1[i] = sin(th) * gain;
+ }
+ audio->write_buffer(out, channels, len);
+ audio_pos = k;
+ }
+
+ if( !interrupted )
+ audio->set_last_buffer();
+ audio->stop_audio(interrupted ? 0 : 1);
+ playing_audio = 0;
+
+ audio->close_all();
+}
+
+void ProxyBeep::start()
+{
+ if( running() ) return;
+ audio_pos = -1;
+ interrupted = 0;
+ Thread::start();
+}
+
+void ProxyBeep::stop(int wait)
+{
+ if( running() && !interrupted ) {
+ interrupted = 1;
+ audio->stop_audio(wait);
+ }
+ Thread::join();
+}
+
+void ProxyBeep::tone(double freq, double secs, double gain)
+{
+ stop(0);
+ this->freq = freq;
+ this->secs = secs;
+ this->gain = gain;
+ start();
+}
+