提交 b2805694 编写于 作者: M manjaro-xfce

解决一个隐晦的问题。

QMultimedia Audio Input 在Qt6 下,QIODevice::readAll()返回的长度为奇数。如果默认返回长度是short的整数倍,则会出现严重的问题。
上级 0b4e6327
......@@ -160,12 +160,6 @@ void DialogSoundCard::CreateAudioInput()
}
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;
......@@ -182,39 +176,26 @@ void DialogSoundCard::OnReadMore()
const int nins = m_cmdline->toInt("instance",0);
const int channel = m_cmdline->toInt("channel",2);
//Read sound samples from input device to buffer
//注意!Qt6下,这里返回的 mBuffer 字节个数可能是奇数。因此需要额外的处理。
QByteArray mBuffer = mpInputDevSound->readAll();
qint64 l = mBuffer.length();
m_buffer.append(mBuffer);
qint64 l = m_buffer.length();
if(l > 0) {
//Assign sound samples to short array
short* resultingData = (short*)mBuffer.data();
const int len = l/2;
short *outdata=resultingData;
outdata[ 0 ] = resultingData [ 0 ];
int iIndex;
miMaxValue = 0;
for ( 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;
}
if (m_batch_size<=0)
{
int len = m_buffer.size() / 2 / channel;
//write modified sound sample to outputdevice for playback audio
ApplyVolumeToSample((short *)m_buffer.data(),len * channel);
if (nid_timestamp)
TASKBUS::push_subject(nid_timestamp,nins,sizeof(quint64),(unsigned char *)&m_nTotalSps);
if (nid_wav>0)
TASKBUS::push_subject(nid_wav,nins,len*2,(unsigned char *)outdata);
m_nTotalSps += len*2;
TASKBUS::push_subject(nid_wav,nins,len*2*channel,(unsigned char *)m_buffer.data());
m_nTotalSps += len*2*channel;
QTimer::singleShot(1000, this, SLOT(OnTimeOut()));
m_buffer.remove(0,len*2*channel);
}
else
{
m_buffer.append((char *)outdata,len*2);
const int groupBytes = channel*m_batch_size*2;
const int groups = m_buffer.size()/groupBytes;
const char * pdata = m_buffer.constData();
......@@ -224,6 +205,10 @@ void DialogSoundCard::OnReadMore()
if ((m_nTotalSps - listen_thread::m_refTms )<=19200*16 ||
listen_thread::m_refTms==0)
{
ApplyVolumeToSample(
(short *)(pdata+i*groupBytes),
groupBytes/2
);
//write modified sound sample to outputdevice for playback audio
if (nid_timestamp)
TASKBUS::push_subject(nid_timestamp,nins,sizeof(quint64),(unsigned char *)&m_nTotalSps);
......@@ -238,6 +223,7 @@ void DialogSoundCard::OnReadMore()
//tail
m_buffer.remove(0,groups*groupBytes);
}
}
}
......
......@@ -30,7 +30,20 @@ private:
private:
int AddWavHeader(char *);
int ApplyVolumeToSample(short iSample);
inline int ApplyVolumeToSample(short * iSample, int len)
{
//Calculate volume, Volume limited to max 30000 and min -30000
miMaxValue = 0;
for (int i=0;i<len;++i)
{
iSample[i] = std::max(std::min(((iSample[i] * miVolume) / 50) ,30000), -30000);
if (miMaxValue < abs(iSample[i]))
miMaxValue = abs(iSample[i]);
}
return miMaxValue;
}
void InitMonitor();
void CreateAudioInput();
private slots:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册