SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
AudioInput Class Reference

#include <audioinput.h>

Inherits QIODevice.

Public Member Functions

 AudioInput ()
 
virtual ~AudioInput ()
 
bool start (int device, int rate)
 
void stop ()
 
void addFifo (AudioFifo *audioFifo)
 
void removeFifo (AudioFifo *audioFifo)
 
int getNbFifos () const
 
uint getRate () const
 
void setOnExit (bool onExit)
 
void setVolume (float volume)
 

Private Member Functions

virtual qint64 readData (char *data, qint64 maxLen)
 
virtual qint64 writeData (const char *data, qint64 len)
 

Private Attributes

QMutex m_mutex
 
QAudioInput * m_audioInput
 
uint m_audioUsageCount
 
bool m_onExit
 
float m_volume
 
std::list< AudioFifo * > m_audioFifos
 
std::vector< qint32 > m_mixBuffer
 
QAudioFormat m_audioFormat
 

Friends

class AudioOutputPipe
 

Detailed Description

Definition at line 33 of file audioinput.h.

Constructor & Destructor Documentation

◆ AudioInput()

AudioInput::AudioInput ( )

Definition at line 25 of file audioinput.cpp.

25  :
26  m_mutex(QMutex::Recursive),
27  m_audioInput(0),
29  m_onExit(false),
30  m_volume(0.5f),
31  m_audioFifos()
32 {
33 }
uint m_audioUsageCount
Definition: audioinput.h:52
float m_volume
Definition: audioinput.h:54
std::list< AudioFifo * > m_audioFifos
Definition: audioinput.h:56
QMutex m_mutex
Definition: audioinput.h:50
bool m_onExit
Definition: audioinput.h:53
QAudioInput * m_audioInput
Definition: audioinput.h:51

◆ ~AudioInput()

AudioInput::~AudioInput ( )
virtual

Definition at line 35 of file audioinput.cpp.

References m_audioFifos, m_mutex, and stop().

36 {
37  stop();
38 
39  QMutexLocker mutexLocker(&m_mutex);
40 
41  for (std::list<AudioFifo*>::iterator it = m_audioFifos.begin(); it != m_audioFifos.end(); ++it)
42  {
43  delete *it;
44  }
45 
46  m_audioFifos.clear();
47 }
std::list< AudioFifo * > m_audioFifos
Definition: audioinput.h:56
QMutex m_mutex
Definition: audioinput.h:50
void stop()
Definition: audioinput.cpp:121
+ Here is the call graph for this function:

Member Function Documentation

◆ addFifo()

void AudioInput::addFifo ( AudioFifo audioFifo)

Definition at line 142 of file audioinput.cpp.

References m_audioFifos, and m_mutex.

Referenced by FCDProPlusInput::openFCDAudio(), and FCDProInput::openFCDAudio().

143 {
144  QMutexLocker mutexLocker(&m_mutex);
145 
146  m_audioFifos.push_back(audioFifo);
147 }
std::list< AudioFifo * > m_audioFifos
Definition: audioinput.h:56
QMutex m_mutex
Definition: audioinput.h:50
+ Here is the caller graph for this function:

◆ getNbFifos()

int AudioInput::getNbFifos ( ) const
inline

Definition at line 43 of file audioinput.h.

43 { return m_audioFifos.size(); }
std::list< AudioFifo * > m_audioFifos
Definition: audioinput.h:56

◆ getRate()

uint AudioInput::getRate ( ) const
inline

Definition at line 45 of file audioinput.h.

Referenced by FCDProPlusInput::openFCDAudio(), FCDProInput::openFCDAudio(), and AudioDeviceManager::setInputDeviceInfo().

45 { return m_audioFormat.sampleRate(); }
QAudioFormat m_audioFormat
Definition: audioinput.h:59
+ Here is the caller graph for this function:

◆ readData()

qint64 AudioInput::readData ( char *  data,
qint64  maxLen 
)
privatevirtual

Definition at line 156 of file audioinput.cpp.

157 {
158  Q_UNUSED(data);
159  Q_UNUSED(maxLen);
160  return 0;
161 }

◆ removeFifo()

void AudioInput::removeFifo ( AudioFifo audioFifo)

Definition at line 149 of file audioinput.cpp.

References m_audioFifos, and m_mutex.

Referenced by FCDProPlusInput::closeFCDAudio(), and FCDProInput::closeFCDAudio().

150 {
151  QMutexLocker mutexLocker(&m_mutex);
152 
153  m_audioFifos.remove(audioFifo);
154 }
std::list< AudioFifo * > m_audioFifos
Definition: audioinput.h:56
QMutex m_mutex
Definition: audioinput.h:50
+ Here is the caller graph for this function:

◆ setOnExit()

void AudioInput::setOnExit ( bool  onExit)
inline

Definition at line 46 of file audioinput.h.

46 { m_onExit = onExit; }
bool m_onExit
Definition: audioinput.h:53

◆ setVolume()

void AudioInput::setVolume ( float  volume)
inline

Definition at line 47 of file audioinput.h.

Referenced by AudioDeviceManager::setInputDeviceInfo().

47 { m_volume = volume; }
float m_volume
Definition: audioinput.h:54
+ Here is the caller graph for this function:

◆ start()

bool AudioInput::start ( int  device,
int  rate 
)

Definition at line 49 of file audioinput.cpp.

References m_audioFormat, m_audioInput, m_audioUsageCount, m_mutex, and m_volume.

Referenced by FCDProPlusInput::openFCDAudio(), FCDProInput::openFCDAudio(), and AudioDeviceManager::setInputDeviceInfo().

50 {
51  if (m_audioUsageCount == 0)
52  {
53  QMutexLocker mutexLocker(&m_mutex);
54  QAudioDeviceInfo devInfo;
55 
56  if (device < 0)
57  {
58  devInfo = QAudioDeviceInfo::defaultInputDevice();
59  qWarning("AudioInput::start: using default device %s", qPrintable(devInfo.defaultInputDevice().deviceName()));
60  }
61  else
62  {
63  QList<QAudioDeviceInfo> devicesInfo = QAudioDeviceInfo::availableDevices(QAudio::AudioInput);
64 
65  if (device < devicesInfo.size())
66  {
67  devInfo = devicesInfo[device];
68  qWarning("AudioInput::start: using audio device #%d: %s", device, qPrintable(devInfo.deviceName()));
69  }
70  else
71  {
72  devInfo = QAudioDeviceInfo::defaultInputDevice();
73  qWarning("AudioInput::start: audio device #%d does not exist. Using default device %s", device, qPrintable(devInfo.deviceName()));
74  }
75  }
76 
77  //QAudioDeviceInfo devInfo(QAudioDeviceInfo::defaultOutputDevice());
78 
79  m_audioFormat.setSampleRate(rate);
80  m_audioFormat.setChannelCount(2);
81  m_audioFormat.setSampleSize(16);
82  m_audioFormat.setCodec("audio/pcm");
83  m_audioFormat.setByteOrder(QAudioFormat::LittleEndian);
84  m_audioFormat.setSampleType(QAudioFormat::SignedInt); // Unknown, SignedInt, UnSignedInt, Float
85 
86  if (!devInfo.isFormatSupported(m_audioFormat))
87  {
88  m_audioFormat = devInfo.nearestFormat(m_audioFormat);
89  qWarning("AudioInput::start: %d Hz S16_LE audio format not supported. Nearest is sampleRate: %d channelCount: %d sampleSize: %d sampleType: %d",
90  rate, m_audioFormat.sampleRate(), m_audioFormat.channelCount(), m_audioFormat.sampleSize(), (int) m_audioFormat.sampleType());
91  }
92  else
93  {
94  qInfo("AudioInput::start: audio format OK");
95  }
96 
97  if (m_audioFormat.sampleSize() != 16)
98  {
99  qWarning("AudioInput::start: Audio device '%s' failed", qPrintable(devInfo.defaultInputDevice().deviceName()));
100  return false;
101  }
102 
103  m_audioInput = new QAudioInput(devInfo, m_audioFormat);
104  m_audioInput->setVolume(m_volume);
105 
106  QIODevice::open(QIODevice::ReadWrite);
107 
108  m_audioInput->start(this);
109 
110  if (m_audioInput->state() != QAudio::ActiveState)
111  {
112  qWarning("AudioInput::start: cannot start");
113  }
114  }
115 
117 
118  return true;
119 }
uint m_audioUsageCount
Definition: audioinput.h:52
float m_volume
Definition: audioinput.h:54
QMutex m_mutex
Definition: audioinput.h:50
QAudioFormat m_audioFormat
Definition: audioinput.h:59
QAudioInput * m_audioInput
Definition: audioinput.h:51
+ Here is the caller graph for this function:

◆ stop()

void AudioInput::stop ( )

Definition at line 121 of file audioinput.cpp.

References m_audioInput, m_audioUsageCount, m_mutex, and m_onExit.

Referenced by FCDProPlusInput::closeFCDAudio(), FCDProInput::closeFCDAudio(), AudioDeviceManager::setInputDeviceInfo(), and ~AudioInput().

122 {
123  qDebug("AudioInput::stop");
124 
125  if (m_audioUsageCount > 0)
126  {
128 
129  if (m_audioUsageCount == 0)
130  {
131  qDebug("AudioInput::stop: effectively close QIODevice");
132  QMutexLocker mutexLocker(&m_mutex);
133  QIODevice::close();
134 
135  if (!m_onExit) {
136  delete m_audioInput;
137  }
138  }
139  }
140 }
uint m_audioUsageCount
Definition: audioinput.h:52
QMutex m_mutex
Definition: audioinput.h:50
bool m_onExit
Definition: audioinput.h:53
QAudioInput * m_audioInput
Definition: audioinput.h:51
+ Here is the caller graph for this function:

◆ writeData()

qint64 AudioInput::writeData ( const char *  data,
qint64  len 
)
privatevirtual

Definition at line 163 of file audioinput.cpp.

References m_audioFifos, and m_audioFormat.

164 {
165  // Study this mutex on OSX, for now deadlocks possible
166  // Removed as it may indeed cause lockups and is in fact useless.
167 //#ifndef __APPLE__
168 // QMutexLocker mutexLocker(&m_mutex);
169 //#endif
170 
171  if ((m_audioFormat.sampleSize() != 16)
172  || (m_audioFormat.sampleType() != QAudioFormat::SignedInt)
173  || (m_audioFormat.byteOrder() != QAudioFormat::LittleEndian))
174  {
175  qCritical("AudioInput::writeData: invalid format not S16LE");
176  return 0;
177  }
178 
179  if (m_audioFormat.channelCount() != 2) {
180  qCritical("AudioInput::writeData: invalid format not stereo");
181  return 0;
182  }
183 
184  for (std::list<AudioFifo*>::iterator it = m_audioFifos.begin(); it != m_audioFifos.end(); ++it)
185  {
186  (*it)->write(reinterpret_cast<const quint8*>(data), len/4);
187  }
188 
189  return len;
190 }
std::list< AudioFifo * > m_audioFifos
Definition: audioinput.h:56
QAudioFormat m_audioFormat
Definition: audioinput.h:59

Friends And Related Function Documentation

◆ AudioOutputPipe

friend class AudioOutputPipe
friend

Definition at line 65 of file audioinput.h.

Member Data Documentation

◆ m_audioFifos

std::list<AudioFifo*> AudioInput::m_audioFifos
private

Definition at line 56 of file audioinput.h.

Referenced by addFifo(), removeFifo(), writeData(), and ~AudioInput().

◆ m_audioFormat

QAudioFormat AudioInput::m_audioFormat
private

Definition at line 59 of file audioinput.h.

Referenced by start(), and writeData().

◆ m_audioInput

QAudioInput* AudioInput::m_audioInput
private

Definition at line 51 of file audioinput.h.

Referenced by start(), and stop().

◆ m_audioUsageCount

uint AudioInput::m_audioUsageCount
private

Definition at line 52 of file audioinput.h.

Referenced by start(), and stop().

◆ m_mixBuffer

std::vector<qint32> AudioInput::m_mixBuffer
private

Definition at line 57 of file audioinput.h.

◆ m_mutex

QMutex AudioInput::m_mutex
private

Definition at line 50 of file audioinput.h.

Referenced by addFifo(), removeFifo(), start(), stop(), and ~AudioInput().

◆ m_onExit

bool AudioInput::m_onExit
private

Definition at line 53 of file audioinput.h.

Referenced by stop().

◆ m_volume

float AudioInput::m_volume
private

Definition at line 54 of file audioinput.h.

Referenced by start().


The documentation for this class was generated from the following files: