dialogsoundcard.cpp 5.3 KB
Newer Older
丁劲犇's avatar
丁劲犇 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
#include "dialogsoundcard.h"
#include "ui_dialogsoundcard.h"
#include <QLayout>
#include <QDebug>
#include <QTimer>
#include <QFileDialog>
#include <QMessageBox>

DialogSoundCard::DialogSoundCard(QWidget *parent) :
	QDialog(parent),
	ui(new Ui::DialogSoundCard),
	m_devOutputModule(new QStandardItemModel(this))
{
	ui->setupUi(this);
	ui->comboBox_audioDevices->setModel(m_devOutputModule);
	m_devOutputlist = QAudioDeviceInfo::availableDevices(QAudio::AudioOutput);
	foreach(QAudioDeviceInfo info, m_devOutputlist)
		m_devOutputModule->appendRow(new QStandardItem(info.deviceName()));

	//---------- players ----------
	miMaxValue = 0;
	miVolume = ui->horizontalSlider->value();

	mpAudioOutputSound = nullptr;
	mpOutDevSound = nullptr;

	ui->btn_stop->setDisabled(true);

	connect(ui->btn_start, SIGNAL(clicked()), this,SLOT(OnPlayStart()));
	connect(ui->btn_stop, SIGNAL(clicked()), this,SLOT(OnPlayStop()));

	m_nTimerID = startTimer(20);
}

DialogSoundCard::DialogSoundCard(const TASKBUS::cmdlineParser * pline,QWidget *parent ):
	DialogSoundCard(parent)
{
	m_cmdline = pline;
	if (m_cmdline)
	{
		int sample_rate = m_cmdline->toInt("sample_rate",48000);
		setInstance(m_cmdline->toInt("instance",0));
		QString device = QString::fromStdString( m_cmdline->toString("device","default"));
		int channel = m_cmdline->toInt("channel",2);

		ui->spinbox_channels->setValue(channel);
		ui->spinbox_sprate->setValue(sample_rate);
		QList<QStandardItem*> items = m_devOutputModule->findItems(device);
		if (items.size())
		{
			QModelIndex nitem = m_devOutputModule->indexFromItem(items.first());
			if (nitem.row()>=0 && nitem.row()<m_devOutputModule->rowCount())
				ui->comboBox_audioDevices->setCurrentIndex(nitem.row());
		}

		//Listen thread to recieve messages from platform
		m_pListenThread = new listen_thread(m_cmdline,this);
		connect(m_pListenThread,&listen_thread::quit_app,this,&DialogSoundCard::close);
		connect(m_pListenThread,&listen_thread::sig_play,this,&DialogSoundCard::sltPlay);
		m_pListenThread->start();


		int hiden = m_cmdline->toInt("hide",0);
		int autostart = m_cmdline->toInt("autostart",0);

		if (hiden || autostart)
			OnPlayStart();

	}
}

DialogSoundCard::~DialogSoundCard()
{
	if (m_pListenThread)
		m_pListenThread->terminate();
	delete ui;
}

void DialogSoundCard::OnPlayStart()
{
	m_bFirstPlay = true;
	ui->btn_start->setDisabled(true);
	ui->btn_stop->setDisabled(false);
	InitMonitor();

}


void DialogSoundCard::OnPlayStop()
{
	ui->btn_start->setDisabled(false);
	ui->btn_stop->setDisabled(true);
	if (mpOutDevSound != 0) {
		disconnect(mpOutDevSound, 0, this, 0);
		mpOutDevSound = 0;
	}
}

void DialogSoundCard::OnStateChange(QAudio::State state)
{
	if(state == QAudio::IdleState)
		OnPlayStop();
}

void DialogSoundCard::InitMonitor()
{
	mFormatSound.setSampleSize(16); //set sample sze to 16 bit
	mFormatSound.setSampleType(QAudioFormat::SignedInt ); //Sample type as usigned integer sample
	mFormatSound.setByteOrder(QAudioFormat::LittleEndian); //Byte order
	mFormatSound.setCodec("audio/pcm"); //set codec as simple audio/pcm
	mFormatSound.setSampleRate(ui->spinbox_sprate->value());
	mFormatSound.setChannelCount(ui->spinbox_channels->value());
	QAudioDeviceInfo infoIn;
	if (ui->comboBox_audioDevices->currentIndex()>=0 &&
			ui->comboBox_audioDevices->currentIndex()<m_devOutputModule->rowCount())
		infoIn = m_devOutputlist[ui->comboBox_audioDevices->currentIndex()];
	else
		infoIn = QAudioDeviceInfo::defaultInputDevice();
	if (!infoIn.isFormatSupported(mFormatSound))
	{
		//Default format not supported - trying to use nearest
		mFormatSound = infoIn.nearestFormat(mFormatSound);
	}

	QAudioDeviceInfo infoOut(QAudioDeviceInfo::defaultOutputDevice());
	if (!infoOut.isFormatSupported(mFormatSound))
	{
		//Default format not supported - trying to use nearest
		mFormatSound = infoOut.nearestFormat(mFormatSound);
	}

	CreateAudioOutput();
	mpOutDevSound = mpAudioOutputSound->start();
	connect(ui->horizontalSlider, SIGNAL(valueChanged(int)),
			this, SLOT(OnSliderValueChanged(int)));
}

void DialogSoundCard::CreateAudioOutput()
{
	if (mpOutDevSound != 0) {
		disconnect(mpOutDevSound, 0, this, 0);
		mpOutDevSound = 0;
	}

	QAudioDeviceInfo outputDevice(QAudioDeviceInfo::defaultOutputDevice());
	mpAudioOutputSound = new QAudioOutput(outputDevice, mFormatSound, this);

}
void DialogSoundCard::sltPlay(QByteArray v)
{
	miMaxValue = 0;
	if (mpOutDevSound)
	{
		qint16 * outdata = (qint16 *)v.data();
		int len = v.size()/2;
		miMaxValue = 0;
		for ( int iIndex=0; iIndex < len; iIndex++ ) {
			//Change volume to each integer data in a sample
			int value = ApplyVolumeToSample( outdata[ iIndex ]);
			outdata[ iIndex ] = value;

			miMaxValue = miMaxValue>=value ? miMaxValue : value;
		}
		//mpOutDevSound->write(v);
		m_buffer.append(v);
	}
	ui->progress->setValue(miMaxValue);
}

int DialogSoundCard::ApplyVolumeToSample(short iSample)
{
	//Calculate volume, Volume limited to  max 30000 and min -30000
	return std::max(std::min(((iSample * miVolume) / 50) ,30000), -30000);
}

void DialogSoundCard::OnSliderValueChanged(int value)
{
	miVolume = value;
}


void DialogSoundCard::timerEvent(QTimerEvent * e)
{
	if (e->timerId()==m_nTimerID)
	{
		const int bBuffer20ms = 0.04*ui->spinbox_sprate->value()*2*ui->spinbox_channels->value();
		if (m_buffer.size()>=bBuffer20ms)
		{
			int w = mpOutDevSound->write(m_buffer.data(),bBuffer20ms);
			if (w)
				m_buffer.remove(0,w);
		}
	}
}