void SoundInput::start(qint32 device)
{
	stop();

//---------------------------------------------------- Soundcard Setup
	m_callbackData.kin=0;                              //Buffer pointer
	m_callbackData.ncall=0;                            //Number of callbacks
	m_callbackData.bzero=false;                        //Flag to request reset of kin
	m_callbackData.monitoring=m_monitoring;

	//### Temporary: hardwired device selection
	QAudioDeviceInfo  DeviceInfo;
	QList<QAudioDeviceInfo> m_InDevices;
	QAudioDeviceInfo  m_InDeviceInfo;
	m_InDevices = DeviceInfo.availableDevices(QAudio::AudioInput);
	inputDevice = m_InDevices.at(0);
	//###
//  qDebug() << "B" << m_InDevices.length() << inputDevice.deviceName();

	const char* pcmCodec = "audio/pcm";
	QAudioFormat audioFormat = inputDevice.preferredFormat();
	audioFormat.setChannelCount(1);
	audioFormat.setCodec(pcmCodec);
	audioFormat.setSampleRate(12000);
	audioFormat.setSampleType(QAudioFormat::SignedInt);
	audioFormat.setSampleSize(16);

//  qDebug() << "C" << audioFormat << audioFormat.isValid();

	if (!audioFormat.isValid()) {
		emit error(tr("Requested audio format is not available."));
		return;
	}

	audioInput = new QAudioInput(inputDevice, audioFormat);
//  qDebug() << "D" << audioInput->error() << QAudio::NoError;
  if (audioInput->error() != QAudio::NoError) {
		emit error(reportAudioError(audioInput->error()));
		return;
	}

	stream = audioInput->start();
//  qDebug() << "E" << stream->errorString();

	m_ntr0 = 99;		     // initial value higher than any expected
	m_nBusy = 0;
	m_intervalTimer.start(100);
	m_ms0 = QDateTime::currentMSecsSinceEpoch();
	m_nsps0 = 0;
}
Example #2
0
/////////////////////////////////////////////////////////////////////
// Starts up soundcard output thread using soundcard at list OutDevIndx
/////////////////////////////////////////////////////////////////////
bool CSoundOut::Start(int OutDevIndx, bool StereoOut, double UsrDataRate, bool BlockingMode)
{
QAudioDeviceInfo  DeviceInfo;
	long mvolume;
	m_StereoOut = StereoOut;
	m_BlockingMode = BlockingMode;
	//Get required soundcard from list
	m_OutDevices = DeviceInfo.availableDevices(QAudio::AudioOutput);

	if (-1 == OutDevIndx) GetAlsaMasterVolume(&mvolume);
		qDebug()<<"Soundcard volume" << mvolume;

	if (-1 == OutDevIndx) m_OutDeviceInfo = QAudioDeviceInfo::defaultOutputDevice();
	else m_OutDeviceInfo = m_OutDevices.at(OutDevIndx);

#if 1 //RRK get a list of audio devices and the default
	foreach (const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(QAudio::AudioOutput)) {
	     qDebug() << "l:" << deviceInfo.deviceName();
	}

	QAudioDeviceInfo info = QAudioDeviceInfo::defaultOutputDevice();
	qDebug() << "res:" << info.deviceName();
#endif

	//Setup fixed format for sound ouput
	m_OutAudioFormat.setCodec("audio/pcm");
	//m_OutAudioFormat.setFrequency(SOUNDCARD_RATE);
	m_OutAudioFormat.setSampleRate(SOUNDCARD_RATE);
	m_OutAudioFormat.setSampleSize(16);
	m_OutAudioFormat.setSampleType(QAudioFormat::SignedInt);
	m_OutAudioFormat.setByteOrder(QAudioFormat::LittleEndian);
	if(m_StereoOut)
		//RRK m_OutAudioFormat.setChannels(2);
		m_OutAudioFormat.setChannelCount(2);
	else
		m_OutAudioFormat.setChannelCount(1);

	m_pAudioOutput = new QAudioOutput(m_OutDeviceInfo, m_OutAudioFormat, this);
	if(!m_pAudioOutput)
	{
		qDebug()<<"Soundcard output error";
		return false;
	}
	if(QAudio::NoError == m_pAudioOutput->error() )
	{
		//initialize the data queue variables
		m_UserDataRate = 1;	//force user data rate to be changed
		ChangeUserDataRate(UsrDataRate);
		m_pOutput = m_pAudioOutput->start(); //start QT AudioOutput

		//RRK workaround for default, for some reason choosing default
		//sets the master volume to max!
		if (-1 == OutDevIndx) SetAlsaMasterVolume(50);

		//determine how long to sleep between low level reads based on samplerate and period size
		m_BlockTime = ( 250*m_pAudioOutput->periodSize() )/
						( SOUNDCARD_RATE*m_OutAudioFormat.channelCount() );
						//RRK ( SOUNDCARD_RATE*m_OutAudioFormat.channels() );
//qDebug()<<"periodSize "<<m_pAudioOutput->periodSize();
//qDebug()<<"BlockTime "<<m_BlockTime;
		m_ThreadQuit = FALSE;
		start(QThread::HighestPriority);	//start worker thread and set its priority
//		start(QThread::TimeCriticalPriority);	//start worker thread and set its priority
		return true;
	}
	else
	{
		qDebug()<<"Soundcard output error";
		return false;
	}
}