00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef AUDIODEVICE_H
00023 #define AUDIODEVICE_H
00024
00025 #include <QObject>
00026 #include <QList>
00027 #include <QVector>
00028 #include <QHash>
00029 #include <QStringList>
00030 #include <QByteArray>
00031 #include <QTimer>
00032 #include <QVariant>
00033
00034
00035 #include "RingBufferNPT.h"
00036 #include "APILinkedList.h"
00037 #include "defines.h"
00038
00039 class AudioDeviceThread;
00040 class Driver;
00041 class Client;
00042 class AudioChannel;
00043 class AudioBus;
00044 #if defined (JACK_SUPPORT)
00045 class JackDriver;
00046 #endif
00047
00048 #if defined (COREAUDIO_SUPPORT)
00049 class CoreAudioDriver;
00050 #endif
00051
00052
00053 class AudioDevice : public QObject
00054 {
00055 Q_OBJECT
00056
00057 public:
00058 void set_parameters( int rate,
00059 nframes_t bufferSize,
00060 const QString& driverType,
00061 bool capture=true,
00062 bool playback=true,
00063 const QString& device="hw:0",
00064 const QString& ditherShape="None");
00065
00066 void add_client(Client* client);
00067 void remove_client(Client* client);
00068
00069 void transport_start(Client* client);
00070 void transport_stop(Client* client);
00071 int transport_seek_to(Client* client, TimeRef location);
00072
00083 AudioBus* get_playback_bus(QByteArray name) const
00084 {
00085 return playbackBuses.value(name);
00086 }
00087
00098 AudioBus* get_capture_bus(QByteArray name) const
00099 {
00100 return captureBuses.value(name);
00101 }
00102
00103 QStringList get_capture_buses_names() const;
00104 QStringList get_playback_buses_names() const;
00105
00106 QString get_device_name() const;
00107 QString get_device_longname() const;
00108 QString get_driver_type() const;
00109
00110 QStringList get_available_drivers() const;
00111
00112 uint get_sample_rate() const;
00113 uint get_bit_depth() const;
00114 TimeRef get_buffer_latency();
00115
00120 nframes_t get_buffer_size() const
00121 {
00122 return m_bufferSize;
00123 }
00124
00125
00126 void show_descriptors();
00127 void set_driver_properties(QHash<QString, QVariant>& properties);
00128
00129 int shutdown();
00130
00131 uint capture_buses_count() const;
00132 uint playback_buses_count() const;
00133
00134 trav_time_t get_cpu_time();
00135
00136
00137 private:
00138 AudioDevice();
00139 ~AudioDevice();
00140 AudioDevice(const AudioDevice&) : QObject() {}
00141
00142
00143 friend AudioDevice& audiodevice();
00144
00145 friend class AlsaDriver;
00146 friend class PADriver;
00147 friend class Driver;
00148 friend class PulseAudioDriver;
00149 friend class AudioDeviceThread;
00150 #if defined (COREAUDIO_SUPPORT)
00151 friend class CoreAudioDriver;
00152 #endif
00153
00154 Driver* driver;
00155 AudioDeviceThread* audioThread;
00156 APILinkedList m_clients;
00157 QHash<QByteArray, AudioChannel* > playbackChannels;
00158 QHash<QByteArray, AudioChannel* > captureChannels;
00159 QHash<QByteArray, AudioBus* > playbackBuses;
00160 QHash<QByteArray, AudioBus* > captureBuses;
00161 QStringList availableDrivers;
00162 QTimer m_xrunResetTimer;
00163 #if defined (JACK_SUPPORT)
00164 QTimer jackShutDownChecker;
00165 JackDriver* slaved_jack_driver();
00166 friend class JackDriver;
00167 #endif
00168
00169 RingBufferNPT<trav_time_t>* m_cpuTime;
00170 volatile size_t m_runAudioThread;
00171 trav_time_t m_cycleStartTime;
00172 trav_time_t m_lastCpuReadTime;
00173 uint m_bufferSize;
00174 uint m_rate;
00175 uint m_bitdepth;
00176 uint m_xrunCount;
00177 QString m_driverType;
00178 QString m_ditherShape;
00179 QHash<QString, QVariant> m_driverProperties;
00180
00181 int run_one_cycle(nframes_t nframes, float delayed_usecs);
00182 int create_driver(QString driverType, bool capture, bool playback, const QString& cardDevice);
00183 int transport_control(transport_state_t state);
00184
00185 void setup_buses();
00186 void post_process();
00187 void free_memory();
00188
00189
00190 AudioChannel* register_capture_channel(const QByteArray& busName, const QString& audioType, int flags, uint bufferSize, uint channel );
00191 AudioChannel* register_playback_channel(const QByteArray& busName, const QString& audioType, int flags, uint bufferSize, uint channel );
00192
00193 int run_cycle(nframes_t nframes, float delayed_usecs);
00194
00195 void set_buffer_size(uint size);
00196 void set_sample_rate(uint rate);
00197 void set_bit_depth(uint depth);
00198 void delay(float delay);
00199
00200 void transport_cycle_start(trav_time_t time)
00201 {
00202 m_cycleStartTime = time;
00203 }
00204
00205 void transport_cycle_end(trav_time_t time)
00206 {
00207 trav_time_t runcycleTime = time - m_cycleStartTime;
00208 m_cpuTime->write(&runcycleTime, 1);
00209 }
00210
00211 Driver* get_driver() const {return driver;}
00212
00213 void mili_sleep(int msec);
00214 void xrun();
00215
00216 size_t run_audio_thread() const;
00217
00218 enum {
00219 INFO = 0,
00220 WARNING = 1,
00221 CRITICAL = 2
00222 };
00223
00224 QVariant get_driver_property(const QString& property, QVariant defaultValue);
00225
00226 signals:
00233 void stopped();
00234
00239 void started();
00240
00246 void driverParamsChanged();
00247
00251 void bufferUnderRun();
00252
00257 void clientRemoved(Client*);
00258
00259 void xrunStormDetected();
00260
00261 void message(QString, int);
00262
00263 private slots:
00264 void private_add_client(Client* client);
00265 void private_remove_client(Client* client);
00266 void audiothread_finished();
00267 void switch_to_null_driver();
00268 void reset_xrun_counter() {m_xrunCount = 0;}
00269 void check_jack_shutdown();
00270 };
00271
00272
00273
00274 AudioDevice& audiodevice();
00275
00276
00277 inline size_t AudioDevice::run_audio_thread( ) const {return m_runAudioThread;}
00278
00279
00280 #endif
00281
00282