Skip to content

Commit a222e07

Browse files
committed
完成app模块的log添加
1 parent 51df37f commit a222e07

2 files changed

Lines changed: 85 additions & 13 deletions

File tree

app/framework/mic.py

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
# -*- coding: utf-8 -*-
2-
3-
42
import pyaudio
53
import logging
64

75
logger = logging.getLogger(__file__)
86

97

108
class Audio(object):
9+
'''
10+
录音类(基于pyaudio)
11+
'''
1112

1213
def __init__(self, rate=16000, frames_size=None, channels=None, device_index=None):
14+
'''
15+
录音类初始化
16+
:param rate:采样率
17+
:param frames_size:数据帧大小
18+
:param channels:通道数
19+
:param device_index:录音设备id
20+
'''
1321
self.sample_rate = rate
1422
self.frames_size = frames_size if frames_size else rate / 100
1523
self.channels = channels if channels else 1
@@ -39,30 +47,56 @@ def __init__(self, rate=16000, frames_size=None, channels=None, device_index=Non
3947
channels=self.channels,
4048
rate=int(self.sample_rate),
4149
frames_per_buffer=int(self.frames_size),
42-
stream_callback=self._callback,
50+
stream_callback=self.__callback,
4351
input=True
4452
)
4553

4654
self.sinks = []
4755

48-
def _callback(self, in_data, frame_count, time_info, status):
49-
for sink in self.sinks:
50-
sink.put(in_data)
51-
return None, pyaudio.paContinue
52-
5356
def start(self):
57+
'''
58+
开始录音
59+
:return:
60+
'''
5461
self.stream.start_stream()
5562

5663
def stop(self):
64+
'''
65+
结束录音
66+
:return:
67+
'''
5768
self.stream.stop_stream()
5869

5970
def link(self, sink):
71+
'''
72+
绑定录音接收实体
73+
:param sink: 录音接收实体
74+
:return:
75+
'''
6076
if hasattr(sink, 'put') and callable(sink.put):
6177
self.sinks.append(sink)
6278
else:
6379
raise ValueError('Not implement put() method')
6480

6581
def unlink(self, sink):
82+
'''
83+
录音实体解除绑定
84+
:param sink: 录音接收实体
85+
:return:
86+
'''
6687
self.sinks.remove(sink)
6788

89+
def __callback(self, in_data, frame_count, time_info, status):
90+
'''
91+
录音数据(pmc)回调
92+
:param in_data:录音数据
93+
:param frame_count:
94+
:param time_info:
95+
:param status:
96+
:return:
97+
'''
98+
for sink in self.sinks:
99+
sink.put(in_data)
100+
return None, pyaudio.paContinue
101+
68102

app/framework/player.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
# -*- coding: utf-8 -*-
22

3-
"""Player using gstreamer."""
3+
"""
4+
基于GStreamer的播放模块
5+
"""
46

5-
import time
67
import gi
8+
79
gi.require_version('Gst', '1.0')
810
from gi.repository import Gst
911

10-
1112
Gst.init(None)
1213

1314

1415
class Player(object):
16+
'''
17+
播放器实现类
18+
'''
19+
1520
def __init__(self):
1621
self.player = Gst.ElementFactory.make("playbin", "player")
1722

@@ -21,21 +26,43 @@ def __init__(self):
2126
# self.bus.connect('sync-message::eos', self.on_eos)
2227

2328
def play(self, uri):
29+
'''
30+
播放
31+
:param uri:播放资源地址
32+
:return:
33+
'''
2434
self.player.set_state(Gst.State.NULL)
2535
self.player.set_property('uri', uri)
2636
self.player.set_state(Gst.State.PLAYING)
2737

2838
def stop(self):
39+
'''
40+
停止
41+
:return:
42+
'''
2943
self.player.set_state(Gst.State.NULL)
3044

3145
def pause(self):
46+
'''
47+
暂停
48+
:return:
49+
'''
3250
self.player.set_state(Gst.State.PAUSED)
3351

3452
def resume(self):
53+
'''
54+
回复播放
55+
:return:
56+
'''
3557
self.player.set_state(Gst.State.PLAYING)
3658

37-
# name: {eos, ...}
3859
def add_callback(self, name, callback):
60+
'''
61+
播放状态回调
62+
:param name: {eos, ...}
63+
:param callback: 回调函数
64+
:return:
65+
'''
3966
if not callable(callback):
4067
return
4168

@@ -46,12 +73,20 @@ def on_message(bus, message):
4673

4774
@property
4875
def duration(self):
76+
'''
77+
播放时长
78+
:return:
79+
'''
4980
success, duration = self.player.query_duration(Gst.Format.TIME)
5081
if success:
5182
return int(duration / Gst.MSECOND)
5283

5384
@property
5485
def position(self):
86+
'''
87+
播放位置
88+
:return:
89+
'''
5590
success, position = self.player.query_position(Gst.Format.TIME)
5691
if not success:
5792
position = 0
@@ -60,6 +95,10 @@ def position(self):
6095

6196
@property
6297
def state(self):
98+
'''
99+
播放状态
100+
:return:
101+
'''
63102
# GST_STATE_VOID_PENDING no pending state.
64103
# GST_STATE_NULL the NULL state or initial state of an element.
65104
# GST_STATE_READY the element is ready to go to PAUSED.
@@ -68,4 +107,3 @@ def state(self):
68107
# GST_STATE_PLAYING the element is PLAYING, the GstClock is running and the data is flowing.
69108
_, state, _ = self.player.get_state(Gst.SECOND)
70109
return 'FINISHED' if state != Gst.State.PLAYING else 'PLAYING'
71-

0 commit comments

Comments
 (0)