| 1 | /**************************************************************************** |
|---|
| 2 | |
|---|
| 3 | Copyright 2009 Virginia Polytechnic Institute and State University |
|---|
| 4 | |
|---|
| 5 | This file is part of the OSSIE AudioDevice. |
|---|
| 6 | |
|---|
| 7 | OSSIE AudioDevice is free software; you can redistribute it and/or modify |
|---|
| 8 | it under the terms of the GNU General Public License as published by |
|---|
| 9 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | (at your option) any later version. |
|---|
| 11 | |
|---|
| 12 | OSSIE AudioDevice is distributed in the hope that it will be useful, |
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | GNU General Public License for more details. |
|---|
| 16 | |
|---|
| 17 | You should have received a copy of the GNU General Public License |
|---|
| 18 | along with OSSIE AudioDevice; if not, write to the Free Software |
|---|
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | ****************************************************************************/ |
|---|
| 23 | |
|---|
| 24 | #ifndef __OSSIE_AUDIO_DEVICE_H__ |
|---|
| 25 | #define __OSSIE_AUDIO_DEVICE_H__ |
|---|
| 26 | |
|---|
| 27 | #include <stdlib.h> |
|---|
| 28 | #include "ossie/cf.h" |
|---|
| 29 | #include "ossie/PortTypes.h" |
|---|
| 30 | #include "ossie/Device_impl.h" |
|---|
| 31 | #include "ossie/Resource_impl.h" |
|---|
| 32 | #include "ossie/debug.h" |
|---|
| 33 | |
|---|
| 34 | #include "standardinterfaces/complexShort.h" |
|---|
| 35 | #include "standardinterfaces/complexShort_u.h" |
|---|
| 36 | #include "standardinterfaces/complexShort_p.h" |
|---|
| 37 | |
|---|
| 38 | #include "portaudio.h" |
|---|
| 39 | |
|---|
| 40 | static int portaudio_callback_playback( |
|---|
| 41 | const void *inputBuffer, |
|---|
| 42 | void *outputBuffer, |
|---|
| 43 | unsigned long framesPerBuffer, |
|---|
| 44 | const PaStreamCallbackTimeInfo* timeInfo, |
|---|
| 45 | PaStreamCallbackFlags statusFlags, |
|---|
| 46 | void *userData ); |
|---|
| 47 | |
|---|
| 48 | static int portaudio_callback_record( |
|---|
| 49 | const void *inputBuffer, |
|---|
| 50 | void *outputBuffer, |
|---|
| 51 | unsigned long framesPerBuffer, |
|---|
| 52 | const PaStreamCallbackTimeInfo* timeInfo, |
|---|
| 53 | PaStreamCallbackFlags statusFlags, |
|---|
| 54 | void *userData ); |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | /// Main sound card device (capture) definition |
|---|
| 58 | class AudioDevice_i : public virtual Device_impl |
|---|
| 59 | { |
|---|
| 60 | public: |
|---|
| 61 | /// Initializing constructor |
|---|
| 62 | AudioDevice_i(char *uuid, char* label, char* profile, omni_condition *condition); |
|---|
| 63 | |
|---|
| 64 | /// Default destructor |
|---|
| 65 | ~AudioDevice_i(); |
|---|
| 66 | |
|---|
| 67 | /// Sets isRunning to True (start) |
|---|
| 68 | void start() |
|---|
| 69 | throw (CF::Resource::StartError, CORBA::SystemException); |
|---|
| 70 | |
|---|
| 71 | /// static function for omni thread: capture_sound() |
|---|
| 72 | static void run_capture( void * data ); |
|---|
| 73 | |
|---|
| 74 | /// static function for omni thread: playback_sound() |
|---|
| 75 | static void run_playback( void * data ); |
|---|
| 76 | /// Sets isRunning to False (pause) |
|---|
| 77 | |
|---|
| 78 | // |
|---|
| 79 | void SetRecordData(float * _data); |
|---|
| 80 | void GetPlaybackData(float ** _data); |
|---|
| 81 | |
|---|
| 82 | void stop() |
|---|
| 83 | throw (CF::Resource::StopError, CORBA::SystemException); |
|---|
| 84 | |
|---|
| 85 | /// Returns CORBA object pointer to port |
|---|
| 86 | CORBA::Object_ptr getPort(const char* portName) |
|---|
| 87 | throw(CF::PortSupplier::UnknownPort, CORBA::SystemException); |
|---|
| 88 | |
|---|
| 89 | /// Checks if sound card device (capture) is already in use |
|---|
| 90 | void initialize() |
|---|
| 91 | throw (CF::LifeCycle::InitializeError, CORBA::SystemException); |
|---|
| 92 | |
|---|
| 93 | /// Sets the following properties (currently NOT read from .prf.xml) |
|---|
| 94 | /// - Access mode |
|---|
| 95 | /// - Format |
|---|
| 96 | /// - Number of channels |
|---|
| 97 | /// - Sampling rate |
|---|
| 98 | /// - Period size |
|---|
| 99 | /// - Buffer size |
|---|
| 100 | /// - Hardware parameters |
|---|
| 101 | void configure(const CF::Properties &props) |
|---|
| 102 | throw (CORBA::SystemException, |
|---|
| 103 | CF::PropertySet::InvalidConfiguration, |
|---|
| 104 | CF::PropertySet::PartialConfiguration); |
|---|
| 105 | |
|---|
| 106 | /// |
|---|
| 107 | void releaseObject() |
|---|
| 108 | throw (CF::LifeCycle::ReleaseError, CORBA::SystemException); |
|---|
| 109 | |
|---|
| 110 | private: |
|---|
| 111 | /// Disallow default constructor |
|---|
| 112 | AudioDevice_i(); |
|---|
| 113 | |
|---|
| 114 | /// Disallow copy constructor |
|---|
| 115 | AudioDevice_i(AudioDevice_i&); |
|---|
| 116 | |
|---|
| 117 | /// main processing loop for capturing sound |
|---|
| 118 | void capture_sound(); |
|---|
| 119 | |
|---|
| 120 | /// main processing loop for playing sound |
|---|
| 121 | void playback_sound(); |
|---|
| 122 | |
|---|
| 123 | omni_condition *component_running; ///< for component shutdown |
|---|
| 124 | omni_thread *capture_thread; ///< for component writer function |
|---|
| 125 | omni_thread *playback_thread; ///< for component writer function |
|---|
| 126 | |
|---|
| 127 | CORBA::UShort *simple_ptr; |
|---|
| 128 | CORBA::UShort simple_value; |
|---|
| 129 | unsigned int simplesequencelength; |
|---|
| 130 | CORBA::ShortSeq *simplesequence_ptr; |
|---|
| 131 | //simplesequence_ptr = new short[simplesequencelength]; |
|---|
| 132 | |
|---|
| 133 | /// Port: output sound samples |
|---|
| 134 | standardInterfaces_i::complexShort_u *dataOut; |
|---|
| 135 | standardInterfaces_i::complexShort_p *dataIn; |
|---|
| 136 | |
|---|
| 137 | // properties configured flags |
|---|
| 138 | bool configured; |
|---|
| 139 | bool configured_prop_samplerate_record; |
|---|
| 140 | bool configured_prop_samplerate_playback; |
|---|
| 141 | bool configured_prop_buffersize_record; |
|---|
| 142 | bool configured_prop_buffersize_playback; |
|---|
| 143 | |
|---|
| 144 | void ConfigureStreams(); |
|---|
| 145 | void OpenStreams() throw (CF::Resource::StartError); |
|---|
| 146 | void StartStreams() throw (CF::Resource::StartError); |
|---|
| 147 | void StopStreams() throw (CF::Resource::StopError); |
|---|
| 148 | void CloseStreams() throw (CF::Resource::StopError); |
|---|
| 149 | |
|---|
| 150 | // Until I look at omni_condition... -TT |
|---|
| 151 | bool isRunning; ///< starts/stops loop in capture_sound() |
|---|
| 152 | |
|---|
| 153 | // portaudio: specific properties |
|---|
| 154 | PaStreamParameters pa_params_record; |
|---|
| 155 | PaStreamParameters pa_params_playback; |
|---|
| 156 | PaStream *pa_stream_record; |
|---|
| 157 | PaStream *pa_stream_playback; |
|---|
| 158 | |
|---|
| 159 | // |
|---|
| 160 | unsigned int samplerate_record; |
|---|
| 161 | unsigned int samplerate_playback; |
|---|
| 162 | unsigned int buffersize_record; |
|---|
| 163 | unsigned int buffersize_playback; |
|---|
| 164 | |
|---|
| 165 | PortTypes::ShortSequence *playback_data_L; |
|---|
| 166 | PortTypes::ShortSequence *playback_data_R; |
|---|
| 167 | |
|---|
| 168 | PortTypes::ShortSequence record_data_L; |
|---|
| 169 | PortTypes::ShortSequence record_data_R; |
|---|
| 170 | |
|---|
| 171 | float * buffer_playback; |
|---|
| 172 | float * buffer_record; |
|---|
| 173 | |
|---|
| 174 | }; |
|---|
| 175 | |
|---|
| 176 | #endif // __OSSIE_AUDIO_DEVICE_H__ |
|---|
| 177 | |
|---|