-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfaudio.cpp
More file actions
88 lines (77 loc) · 1.69 KB
/
faudio.cpp
File metadata and controls
88 lines (77 loc) · 1.69 KB
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
#include "faudio.h"
#include <QDebug>
void FAudio::stop()
{
mutex.lock();
if(output){
output->stop();
delete output;
output=nullptr;
io=nullptr;
}
mutex.unlock();
}
void FAudio::start()
{
stop();
mutex.lock();
QAudioFormat fmt;//Qt音频格式
fmt.setSampleRate(this->sampleRate);//1秒 采集48000个声音
fmt.setSampleSize(this->sampleSize);//16位
fmt.setChannelCount(this->channel);//双声道
fmt.setCodec("audio/pcm");//编码格式
fmt.setByteOrder(QAudioFormat::LittleEndian);//次序
fmt.setSampleType(QAudioFormat::UnSignedInt);//样本的类别
output = new QAudioOutput(fmt);
io=output->start();
mutex.unlock();
qDebug()<<"audio start"<<output->error();
}
void FAudio::volume(qreal value)
{
if(output){
qDebug()<<"volume:"<<value;
output->setVolume(value);
}
}
void FAudio::play(bool isPlay)
{
mutex.lock();
if(!output)
{
mutex.unlock();
return;
}
if(isPlay)
output->resume();
else
output->suspend();
mutex.unlock();
}
bool FAudio::write(const char *data, int dataSize)
{
if(!data||dataSize<=0)
return false;
mutex.lock();
if(io)
{
mutex.unlock();
//qDebug()<<"write:"<<dataSize;
io->write(data,dataSize);// //输入输出设备播放声音,将数据从零终止的8位字符字符串写入设备。
return true;
}
mutex.unlock();
}
int FAudio::getFree()
{
mutex.lock();
if(!output)
{
mutex.unlock();
return 0;
}
int free = output->bytesFree();
mutex.unlock();
//qDebug()<<"getFree:"<<free;
return free;
}