| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							| 
									
										
										
										
											2023-11-19 06:43:20 +01:00
										 |  |  | // Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany //
 | 
					
						
							|  |  |  | // written by Christian Daniel                                                   //
 | 
					
						
							|  |  |  | // Copyright (C) 2015-2020, 2022-2023 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
 | 
					
						
							|  |  |  | // Copyright (C) 2022 Jon Beniston, M7RCE <jon@beniston.com>                     //
 | 
					
						
							| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // This program is free software; you can redistribute it and/or modify          //
 | 
					
						
							|  |  |  | // it under the terms of the GNU General Public License as published by          //
 | 
					
						
							|  |  |  | // the Free Software Foundation as version 3 of the License, or                  //
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:32:15 +02:00
										 |  |  | // (at your option) any later version.                                           //
 | 
					
						
							| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful,               //
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of                //
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                  //
 | 
					
						
							|  |  |  | // GNU General Public License V3 for more details.                               //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU General Public License             //
 | 
					
						
							|  |  |  | // along with this program. If not, see <http://www.gnu.org/licenses/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef INCLUDE_AUDIODEVICEMANGER_H
 | 
					
						
							|  |  |  | #define INCLUDE_AUDIODEVICEMANGER_H
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QStringList>
 | 
					
						
							|  |  |  | #include <QList>
 | 
					
						
							|  |  |  | #include <QMap>
 | 
					
						
							| 
									
										
										
										
											2023-07-07 04:58:05 +02:00
										 |  |  | #include <QObject>
 | 
					
						
							| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-12 20:30:34 +01:00
										 |  |  | #include "audio/audioinputdevice.h"
 | 
					
						
							| 
									
										
										
										
											2020-11-12 22:13:44 +01:00
										 |  |  | #include "audio/audiooutputdevice.h"
 | 
					
						
							| 
									
										
										
										
											2022-11-17 14:50:16 +00:00
										 |  |  | #include "audio/audiodeviceinfo.h"
 | 
					
						
							| 
									
										
										
										
											2023-07-07 04:58:05 +02:00
										 |  |  | #include "util/messagequeue.h"
 | 
					
						
							| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  | #include "export.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-29 00:49:19 +02:00
										 |  |  | class QThread; | 
					
						
							| 
									
										
										
										
											2018-03-25 11:50:34 +02:00
										 |  |  | class QDataStream; | 
					
						
							| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  | class AudioFifo; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 04:58:05 +02:00
										 |  |  | class SDRBASE_API AudioDeviceManager : public QObject { | 
					
						
							|  |  |  |     Q_OBJECT | 
					
						
							| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  | public: | 
					
						
							|  |  |  |     class InputDeviceInfo | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     public: | 
					
						
							| 
									
										
										
										
											2018-03-25 11:50:34 +02:00
										 |  |  |         InputDeviceInfo() : | 
					
						
							|  |  |  |             sampleRate(m_defaultAudioSampleRate), | 
					
						
							|  |  |  |             volume(m_defaultAudioInputVolume) | 
					
						
							|  |  |  |         {} | 
					
						
							| 
									
										
										
										
											2018-03-26 11:55:45 +02:00
										 |  |  |         void resetToDefaults() { | 
					
						
							|  |  |  |             sampleRate = m_defaultAudioSampleRate; | 
					
						
							|  |  |  |             volume = m_defaultAudioInputVolume; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-05-15 19:37:50 +02:00
										 |  |  |         int sampleRate; | 
					
						
							| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  |         float volume; | 
					
						
							|  |  |  |         friend QDataStream& operator<<(QDataStream& ds, const InputDeviceInfo& info); | 
					
						
							|  |  |  |         friend QDataStream& operator>>(QDataStream& ds, InputDeviceInfo& info); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:50:34 +02:00
										 |  |  |     class OutputDeviceInfo | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     public: | 
					
						
							|  |  |  |         OutputDeviceInfo() : | 
					
						
							|  |  |  |             sampleRate(m_defaultAudioSampleRate), | 
					
						
							|  |  |  |             udpAddress(m_defaultUDPAddress), | 
					
						
							|  |  |  |             udpPort(m_defaultUDPPort), | 
					
						
							|  |  |  |             copyToUDP(false), | 
					
						
							| 
									
										
										
										
											2018-03-26 21:10:09 +02:00
										 |  |  |             udpUseRTP(false), | 
					
						
							| 
									
										
										
										
											2020-11-12 22:13:44 +01:00
										 |  |  |             udpChannelMode(AudioOutputDevice::UDPChannelLeft), | 
					
						
							|  |  |  |             udpChannelCodec(AudioOutputDevice::UDPCodecL16), | 
					
						
							| 
									
										
										
										
											2022-11-08 22:55:40 +01:00
										 |  |  |             udpDecimationFactor(1), | 
					
						
							|  |  |  |             recordToFile(false), | 
					
						
							|  |  |  |             recordSilenceTime(0) | 
					
						
							| 
									
										
										
										
											2018-03-25 11:50:34 +02:00
										 |  |  |         {} | 
					
						
							| 
									
										
										
										
											2018-03-26 11:55:45 +02:00
										 |  |  |         void resetToDefaults() { | 
					
						
							|  |  |  |             sampleRate = m_defaultAudioSampleRate; | 
					
						
							|  |  |  |             udpAddress = m_defaultUDPAddress; | 
					
						
							|  |  |  |             udpPort = m_defaultUDPPort; | 
					
						
							|  |  |  |             copyToUDP = false; | 
					
						
							|  |  |  |             udpUseRTP = false; | 
					
						
							| 
									
										
										
										
											2020-11-12 22:13:44 +01:00
										 |  |  |             udpChannelMode = AudioOutputDevice::UDPChannelLeft; | 
					
						
							|  |  |  |             udpChannelCodec = AudioOutputDevice::UDPCodecL16; | 
					
						
							| 
									
										
										
										
											2019-02-15 07:57:16 +01:00
										 |  |  |             udpDecimationFactor = 1; | 
					
						
							| 
									
										
										
										
											2022-11-08 22:55:40 +01:00
										 |  |  |             fileRecordName.clear(); | 
					
						
							|  |  |  |             recordToFile = false; | 
					
						
							|  |  |  |             recordSilenceTime = 0; | 
					
						
							| 
									
										
										
										
											2018-03-26 11:55:45 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-05-15 19:37:50 +02:00
										 |  |  |         int sampleRate; | 
					
						
							| 
									
										
										
										
											2018-03-25 11:50:34 +02:00
										 |  |  |         QString udpAddress; | 
					
						
							|  |  |  |         quint16 udpPort; | 
					
						
							|  |  |  |         bool copyToUDP; | 
					
						
							|  |  |  |         bool udpUseRTP; | 
					
						
							| 
									
										
										
										
											2020-11-12 22:13:44 +01:00
										 |  |  |         AudioOutputDevice::UDPChannelMode udpChannelMode; | 
					
						
							|  |  |  |         AudioOutputDevice::UDPChannelCodec udpChannelCodec; | 
					
						
							| 
									
										
										
										
											2019-02-15 07:57:16 +01:00
										 |  |  |         uint32_t udpDecimationFactor; | 
					
						
							| 
									
										
										
										
											2022-11-08 22:55:40 +01:00
										 |  |  |         QString fileRecordName; | 
					
						
							|  |  |  |         bool recordToFile; | 
					
						
							|  |  |  |         int recordSilenceTime; //!< 100's ms
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:50:34 +02:00
										 |  |  |         friend QDataStream& operator<<(QDataStream& ds, const OutputDeviceInfo& info); | 
					
						
							|  |  |  |         friend QDataStream& operator>>(QDataStream& ds, OutputDeviceInfo& info); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  | 	AudioDeviceManager(); | 
					
						
							|  |  |  | 	~AudioDeviceManager(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-17 14:50:16 +00:00
										 |  |  |     const QList<AudioDeviceInfo>& getInputDevices() const { return m_inputDevicesInfo; } | 
					
						
							|  |  |  |     const QList<AudioDeviceInfo>& getOutputDevices() const { return m_outputDevicesInfo; } | 
					
						
							| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool getOutputDeviceName(int outputDeviceIndex, QString &deviceName) const; | 
					
						
							| 
									
										
										
										
											2018-03-26 11:55:45 +02:00
										 |  |  |     bool getInputDeviceName(int inputDeviceIndex, QString &deviceName) const; | 
					
						
							| 
									
										
										
										
											2018-03-26 18:41:09 +02:00
										 |  |  |     int getOutputDeviceIndex(const QString &deviceName) const; | 
					
						
							|  |  |  |     int getInputDeviceIndex(const QString &deviceName) const; | 
					
						
							| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:50:34 +02:00
										 |  |  |     void addAudioSink(AudioFifo* audioFifo, MessageQueue *sampleSinkMessageQueue, int outputDeviceIndex = -1); //!< Add the audio sink
 | 
					
						
							| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  |     void removeAudioSink(AudioFifo* audioFifo); //!< Remove the audio sink
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:50:34 +02:00
										 |  |  |     void addAudioSource(AudioFifo* audioFifo, MessageQueue *sampleSourceMessageQueue, int inputDeviceIndex = -1);    //!< Add an audio source
 | 
					
						
							| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  |     void removeAudioSource(AudioFifo* audioFifo); //!< Remove an audio source
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-26 11:55:45 +02:00
										 |  |  |     bool getInputDeviceInfo(const QString& deviceName, InputDeviceInfo& deviceInfo) const; | 
					
						
							|  |  |  |     bool getOutputDeviceInfo(const QString& deviceName, OutputDeviceInfo& deviceInfo) const; | 
					
						
							|  |  |  |     int getInputSampleRate(int inputDeviceIndex = -1); | 
					
						
							|  |  |  |     int getOutputSampleRate(int outputDeviceIndex = -1); | 
					
						
							|  |  |  |     void setInputDeviceInfo(int inputDeviceIndex, const InputDeviceInfo& deviceInfo); | 
					
						
							|  |  |  |     void setOutputDeviceInfo(int outputDeviceIndex, const OutputDeviceInfo& deviceInfo); | 
					
						
							|  |  |  |     void unsetInputDeviceInfo(int inputDeviceIndex); | 
					
						
							|  |  |  |     void unsetOutputDeviceInfo(int outputDeviceIndex); | 
					
						
							|  |  |  |     void inputInfosCleanup();  //!< Remove input info from map for input devices not present
 | 
					
						
							|  |  |  |     void outputInfosCleanup(); //!< Remove output info from map for output devices not present
 | 
					
						
							| 
									
										
										
										
											2023-07-10 11:49:52 +02:00
										 |  |  |     bool setInputDeviceVolume(float volume, int inputDeviceIndex); | 
					
						
							|  |  |  |     bool setOutputDeviceVolume(float volume, int outputDeviceIndex); | 
					
						
							| 
									
										
										
										
											2018-03-26 11:55:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-15 19:37:50 +02:00
										 |  |  |     static const int m_defaultAudioSampleRate = 48000; | 
					
						
							| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  |     static const float m_defaultAudioInputVolume; | 
					
						
							| 
									
										
										
										
											2018-03-25 11:50:34 +02:00
										 |  |  |     static const QString m_defaultUDPAddress; | 
					
						
							|  |  |  |     static const quint16 m_defaultUDPPort = 9998; | 
					
						
							| 
									
										
										
										
											2018-03-26 11:55:45 +02:00
										 |  |  |     static const QString m_defaultDeviceName; | 
					
						
							| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:50:34 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2022-11-17 14:50:16 +00:00
										 |  |  |     QList<AudioDeviceInfo> m_inputDevicesInfo; | 
					
						
							|  |  |  |     QList<AudioDeviceInfo> m_outputDevicesInfo; | 
					
						
							| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:50:34 +02:00
										 |  |  |     QMap<AudioFifo*, int> m_audioSinkFifos; //< audio sink FIFO to audio output device index-1 map
 | 
					
						
							| 
									
										
										
										
											2018-03-26 11:55:45 +02:00
										 |  |  |     QMap<AudioFifo*, MessageQueue*> m_audioFifoToSinkMessageQueues; //!< audio sink FIFO to attached sink message queue
 | 
					
						
							|  |  |  |     QMap<int, QList<MessageQueue*> > m_outputDeviceSinkMessageQueues; //!< sink message queues attached to device
 | 
					
						
							| 
									
										
										
										
											2020-11-12 22:13:44 +01:00
										 |  |  |     QMap<int, AudioOutputDevice*> m_audioOutputs; //!< audio device index to audio output map (index -1 is default device)
 | 
					
						
							| 
									
										
										
										
											2023-06-29 00:49:19 +02:00
										 |  |  |     QMap<int, QThread*> m_audioOutputThreads; //!< audio device index to audio output threads map
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:50:34 +02:00
										 |  |  |     QMap<QString, OutputDeviceInfo> m_audioOutputInfos; //!< audio device name to audio output info
 | 
					
						
							| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:50:34 +02:00
										 |  |  |     QMap<AudioFifo*, int> m_audioSourceFifos; //< audio source FIFO to audio input device index-1 map
 | 
					
						
							| 
									
										
										
										
											2018-03-26 11:55:45 +02:00
										 |  |  |     QMap<AudioFifo*, MessageQueue*> m_audioFifoToSourceMessageQueues; //!< audio source FIFO to attached source message queue
 | 
					
						
							|  |  |  |     QMap<int, QList<MessageQueue*> > m_inputDeviceSourceMessageQueues; //!< sink message queues attached to device
 | 
					
						
							| 
									
										
										
										
											2020-11-12 20:30:34 +01:00
										 |  |  |     QMap<int, AudioInputDevice*> m_audioInputs; //!< audio device index to audio input map (index -1 is default device)
 | 
					
						
							| 
									
										
										
										
											2023-07-10 01:06:19 +02:00
										 |  |  |     QMap<int, QThread*> m_audioInputThreads; //!< audio device index to audio input threads map
 | 
					
						
							| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  |     QMap<QString, InputDeviceInfo> m_audioInputInfos; //!< audio device name to audio input device info
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-25 19:02:33 +01:00
										 |  |  |     bool m_defaultOutputStarted; //!< True if the default audio output (-1) has already been started
 | 
					
						
							|  |  |  |     bool m_defaultInputStarted;  //!< True if the default audio input (-1) has already been started
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 04:58:05 +02:00
										 |  |  |     MessageQueue m_inputMessageQueue; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  |     void resetToDefaults(); | 
					
						
							|  |  |  |     QByteArray serialize() const; | 
					
						
							|  |  |  |     bool deserialize(const QByteArray& data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void startAudioOutput(int outputDeviceIndex); | 
					
						
							|  |  |  |     void stopAudioOutput(int outputDeviceIndex); | 
					
						
							|  |  |  |     void startAudioInput(int inputDeviceIndex); | 
					
						
							|  |  |  |     void stopAudioInput(int inputDeviceIndex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void serializeInputMap(QByteArray& data) const; | 
					
						
							|  |  |  |     void deserializeInputMap(QByteArray& data); | 
					
						
							|  |  |  |     void debugAudioInputInfos() const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void serializeOutputMap(QByteArray& data) const; | 
					
						
							|  |  |  |     void deserializeOutputMap(QByteArray& data); | 
					
						
							|  |  |  |     void debugAudioOutputInfos() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 04:58:05 +02:00
										 |  |  |     bool handleMessage(const Message& cmd); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  | 	friend class MainSettings; | 
					
						
							| 
									
										
										
										
											2023-07-07 04:58:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | private slots: | 
					
						
							|  |  |  |     void handleInputMessages(); | 
					
						
							| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QDataStream& operator<<(QDataStream& ds, const AudioDeviceManager::InputDeviceInfo& info); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:50:34 +02:00
										 |  |  | QDataStream& operator>>(QDataStream& ds, AudioDeviceManager::InputDeviceInfo& info); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QDataStream& operator<<(QDataStream& ds, const AudioDeviceManager::OutputDeviceInfo& info); | 
					
						
							|  |  |  | QDataStream& operator>>(QDataStream& ds, AudioDeviceManager::OutputDeviceInfo& info); | 
					
						
							| 
									
										
										
										
											2018-03-25 04:22:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #endif // INCLUDE_AUDIODEVICEMANGER_H
 |