Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

audiodevice.C

Go to the documentation of this file.
00001 #include "audio1394.h"
00002 #include "audioalsa.h"
00003 #include "audiodevice.h"
00004 #include "audioesound.h"
00005 #include "audiooss.h"
00006 #include "condition.h"
00007 #include "dcoffset.h"
00008 #include "mutex.h"
00009 #include "playbackconfig.h"
00010 #include "preferences.h"
00011 #include "recordconfig.h"
00012 #include "sema.h"
00013 
00014 
00015 
00016 AudioLowLevel::AudioLowLevel(AudioDevice *device)
00017 {
00018         this->device = device;
00019 }
00020 
00021 AudioLowLevel::~AudioLowLevel()
00022 {
00023 }
00024 
00025 
00026 
00027 
00028 
00029 
00030 AudioDevice::AudioDevice()
00031  : Thread(1, 0, 0)
00032 {
00033         initialize();
00034         this->out_config = new AudioOutConfig(0);
00035         this->in_config = new AudioInConfig;
00036         this->vconfig = new VideoInConfig;
00037         startup_lock = new Condition(0, "AudioDevice::startup_lock");
00038         duplex_lock = new Condition(0, "AudioDevice::duplex_lock");
00039         timer_lock = new Mutex("AudioDevice::timer_lock");
00040         for(int i = 0; i < TOTAL_BUFFERS; i++)
00041         {
00042                 play_lock[i] = new Sema(0, "AudioDevice::play_lock");
00043                 arm_lock[i] = new Sema(1, "AudioDevice::arm_lock");
00044         }
00045         dc_offset_thread = new DC_Offset;
00046 }
00047 
00048 AudioDevice::~AudioDevice()
00049 {
00050         delete out_config;
00051         delete in_config;
00052         delete vconfig;
00053         delete startup_lock;
00054         delete duplex_lock;
00055         delete timer_lock;
00056         for(int i = 0; i < TOTAL_BUFFERS; i++)
00057         {
00058                 delete play_lock[i];
00059                 delete arm_lock[i];
00060         }
00061         delete dc_offset_thread;
00062 }
00063 
00064 int AudioDevice::initialize()
00065 {
00066         record_before_play = 0;
00067         r = w = d = 0;
00068 
00069         for(int i = 0; i < TOTAL_BUFFERS; i++)
00070         {
00071                 buffer[i] = 0;
00072                 buffer_size[i] = 0;
00073                 last_buffer[i] = 0;
00074         }
00075 
00076         input_buffer = 0;
00077         duplex_init = 0;
00078         rec_dither = play_dither = 0;
00079         software_position_info = 0;
00080         arm_buffer_num = 0;
00081         is_playing_back = 0;
00082         is_recording = 0;
00083         last_buffer_size = total_samples = position_correction = 0;
00084         last_position = 0;
00085         interrupt = 0;
00086         lowlevel_in = lowlevel_out = lowlevel_duplex = 0;
00087         vdevice = 0;
00088         sharing = 0;
00089         total_samples_read = 0;
00090         out_realtime = 0;
00091         duplex_realtime = 0;
00092         return 0;
00093 }
00094 
00095 int AudioDevice::create_lowlevel(AudioLowLevel* &lowlevel, int driver)
00096 {
00097         this->driver = driver;
00098 
00099 
00100         if(!lowlevel)
00101         {
00102                 switch(driver)
00103                 {
00104 #ifdef HAVE_OSS
00105                         case AUDIO_OSS:
00106                         case AUDIO_OSS_ENVY24:
00107                                 lowlevel = new AudioOSS(this);
00108                                 break;
00109 #endif
00110 
00111 #ifdef HAVE_ESOUND
00112                         case AUDIO_ESOUND:
00113                                 lowlevel = new AudioESound(this);
00114                                 break;
00115 #endif
00116                         case AUDIO_NAS:
00117                                 break;
00118 
00119 #ifdef HAVE_ALSA
00120                         case AUDIO_ALSA:
00121                                 lowlevel = new AudioALSA(this);
00122                                 break;
00123 #endif
00124 
00125 #ifdef HAVE_FIREWIRE    
00126                         case AUDIO_1394:
00127                         case AUDIO_DV1394:
00128                         case AUDIO_IEC61883:
00129                                 lowlevel = new Audio1394(this);
00130                                 break;
00131 #endif
00132                 }
00133         }
00134         return 0;
00135 }
00136 
00137 int AudioDevice::open_input(AudioInConfig *config, 
00138         VideoInConfig *vconfig, 
00139         int rate, 
00140         int samples)
00141 {
00142         r = 1;
00143         duplex_init = 0;
00144         this->in_config->copy_from(config);
00145         this->vconfig->copy_from(vconfig);
00146 //printf("AudioDevice::open_input %s\n", this->in_config->oss_in_device[0]);
00147         in_samplerate = rate;
00148         in_samples = samples;
00149         create_lowlevel(lowlevel_in, config->driver);
00150         lowlevel_in->open_input();
00151         record_timer.update();
00152         return 0;
00153 }
00154 
00155 int AudioDevice::open_output(AudioOutConfig *config, 
00156         int rate, 
00157         int samples, 
00158         int realtime)
00159 {
00160         w = 1;
00161         duplex_init = 0;
00162         *this->out_config = *config;
00163         out_samplerate = rate;
00164         out_samples = samples;
00165         out_realtime = realtime;
00166         create_lowlevel(lowlevel_out, config->driver);
00167         lowlevel_out->open_output();
00168         return 0;
00169 }
00170 
00171 int AudioDevice::open_duplex(AudioOutConfig *out_config, int rate, int samples, int realtime)
00172 {
00173         d = 1;
00174         duplex_init = 1;        // notify playback routines to test the duplex lock
00175         *this->out_config = *out_config;
00176         duplex_samplerate = rate;
00177         duplex_samples = samples;
00178         duplex_channels = 0;
00179         duplex_realtime = realtime;
00180         for(int i = 0; i < MAXDEVICES; i++)
00181         {
00182                 duplex_channels += out_config->oss_out_channels[i];
00183         }
00184         create_lowlevel(lowlevel_duplex, out_config->driver);
00185         lowlevel_duplex->open_duplex();
00186         playback_timer.update();
00187         record_timer.update();
00188         return 0;
00189 }
00190 
00191 
00192 int AudioDevice::interrupt_crash()
00193 {
00194         if(lowlevel_in) return lowlevel_in->interrupt_crash();
00195         return 0;
00196 }
00197 
00198 
00199 int AudioDevice::close_all()
00200 {
00201         if(lowlevel_in) lowlevel_in->close_all();
00202         if(lowlevel_out) lowlevel_out->close_all();
00203         if(lowlevel_duplex) lowlevel_duplex->close_all();
00204 
00205         reset_output();
00206         if(input_buffer) 
00207         {
00208                 delete [] input_buffer; 
00209                 input_buffer = 0; 
00210         }
00211         
00212         is_recording = 0;
00213         rec_dither = play_dither = 0;
00214         software_position_info = position_correction = last_buffer_size = 0;
00215         r = w = d = 0;
00216         duplex_init = 0;
00217         vdevice = 0;
00218         sharing = 0;
00219 
00220         if(lowlevel_in)
00221         {
00222                 delete lowlevel_in;
00223                 lowlevel_in = 0;
00224         }
00225         if(lowlevel_out)
00226         {
00227                 delete lowlevel_out;
00228                 lowlevel_out = 0;
00229         }
00230         if(lowlevel_duplex)
00231         {
00232                 delete lowlevel_duplex;
00233                 lowlevel_duplex = 0;
00234         }
00235         return 0;
00236 }
00237 
00238 int AudioDevice::set_vdevice(VideoDevice *vdevice)
00239 {
00240         this->vdevice = vdevice;
00241         return 0;
00242 }
00243 
00244 
00245 int AudioDevice::get_ichannels()
00246 {
00247         if(r) return in_channels;
00248         else if(d) return duplex_channels;
00249         else return 0;
00250 }
00251 
00252 int AudioDevice::get_ibits()
00253 {
00254         if(r) return in_bits;
00255         else if(d) return duplex_bits;
00256         return 0;
00257 }
00258 
00259 
00260 int AudioDevice::get_obits()
00261 {
00262         if(w) return out_bits;
00263         else if(d) return duplex_bits;
00264         return 0;
00265 }
00266 
00267 int AudioDevice::get_ochannels()
00268 {
00269         if(w) return out_channels;
00270         else if(d) return duplex_channels;
00271         return 0;
00272 }
00273 
00274 AudioLowLevel* AudioDevice::get_lowlevel_out()
00275 {
00276         if(w) return lowlevel_out;
00277         else if(d) return lowlevel_duplex;
00278         return 0;
00279 }
00280 
00281 AudioLowLevel* AudioDevice::get_lowlevel_in()
00282 {
00283         if(r) return lowlevel_in;
00284         else if(d) return lowlevel_duplex;
00285         return 0;
00286 }
00287 
00288 int AudioDevice::get_irate()
00289 {
00290         if(r) return in_samplerate;
00291         else
00292         if(d) return duplex_samplerate;
00293 }
00294 
00295 int AudioDevice::get_orealtime()
00296 {
00297         if(w) return out_realtime;
00298         else if(d) return duplex_realtime;
00299         return 0;
00300 }
00301 
00302 
00303 int AudioDevice::get_orate()
00304 {
00305         if(w) return out_samplerate;
00306         else if(d) return duplex_samplerate;
00307         return 0;
00308 }
00309 
00310 int AudioDevice::get_interrupted()
00311 {
00312         return interrupt;
00313 }
00314 
00315 int AudioDevice::get_device_buffer()
00316 {
00317         return device_buffer;
00318 }
00319 
00320 
00321 

Generated on Sun Jan 8 13:38:50 2006 for Cinelerra-svn by  doxygen 1.4.4