Skip to content

Commit

Permalink
update audio doc
Browse files Browse the repository at this point in the history
  • Loading branch information
lxowalle committed May 20, 2024
1 parent a85f561 commit e5021cb
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 5 deletions.
77 changes: 77 additions & 0 deletions docs/doc/en/audio/play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
title: MaixPy Playback Audio
update:
- date: 2024-05-20
author: lxowalle
version: 1.0.0
content: Initial document
---

## Introduction

This document provides instructions on how to play audio


## How to use

### Hardware operation

![image-20240520134637905](../../../static/image/maixcam_hardware_back.png)

The `MaixCAM` does not have a built-in speaker, so you will need to solder a `1W` speaker yourself. The pins for soldering the speaker are shown in the diagram above on the `VOP` and `VON` pins corresponding to the Speaker.

Note: If the `MaixCAM` has copper posts attached to these pins, they can be soldered directly to the posts, or on the other side of the board for aesthetic reasons.

### Code

Method of playing a PCM file

```python
from maix import audio, time, app

p = audio.Player()

with open('/root/output.pcm', 'rb') as f:
ctx = f.read()

p.play(bytes(ctx))

while not app.need_exit():
time.sleep_ms(10)

print("play finish!")
```

Steps:


1. Import the audio, time and app modules:

```python
from maix import audio, time, app
```

2. Initialize the player:

```python
p = audio.Player()
```
- Note that the default sample rate is 48k, the sample format is little-endian format - signed 16-bit, and the sample channel is 1. You can also customise the parameters like this `p = audio.Player(sample_rate=48000, format=audio.Format.FMT_S16_LE, channel = 1)`. So far only tested with sample rate 48000, format `FMT_S16_LE`, and number of sampling channels 1.

3. Open and playback a PCM file

```python
with open('/root/output.pcm', 'rb') as f:
ctx = f.read()

p.play(bytes(ctx))

while not app.need_exit():
time.sleep_ms(10)
```
- `with open(‘xxx’,‘rb’) as f:` open file `xxx` and get file object `f`
- `ctx = f.read()` reads the contents of the file into `ctx`
- `p.play(bytes(ctx))` plays the audio, `p` is the opened player object, `ctx` is the `PCM` data converted to type bytes
- `time.sleep_ms(10)` Here there is a loop to wait for the playback to complete, as the playback operation is performed asynchronously, and if the program exits early, then it may result in the audio not being played completely.

4. Done
61 changes: 61 additions & 0 deletions docs/doc/en/audio/record.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
title: MaixPy Audio Record
update:
- date: 2024-05-20
author: lxowalle
version: 1.0.0
content: Initial document
---

## Introduction

This document provides methods for recording

### How to use

An example of a recording

```python
from maix import audio, time, app

r = audio.Recorder()
r.volume(12)
print("sample_rate:{} format:{} channel:{}".format(r.sample_rate(), r.format(), r.channel()))

while not app.need_exit():
data = r.record()
print("data size", len(data))

time.sleep_ms(10)

print("record finish!")
```

Steps:

1. Import the audio, time and app modules:

```python
from maix import audio, time, app
```

2. Initialize Recorder

```python
r = audio.Recorder()
r.volume(12)
```

- Note that the default sample rate is 48k, the sample format is little-endian format - signed 16-bit, and the sample channel is 1. You can also customise the parameters like this `r = audio.Recorder(sample_rate=48000, format=audio.Format.FMT_S16_LE, channel = 1)`. So far only tested with sample rate 48000, format `FMT_S16_LE`, and number of sampling channels 1.

- `r.volume(12)` is used to set the volume, the volume range is [0,24]

3. Start recording

```python
data = r.record()
```

- `data` is `bytes` type data in `PCM` format that holds the currently recorded audio. The `PCM` format is set when initialising the `Recorder` object, see step 2.

4. Done, you can do voice processing on the `PCM` data returned by `r.record()` when doing your own applications.
79 changes: 79 additions & 0 deletions docs/doc/zh/audio/play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
title: MaixPy 播放音频
update:
- date: 2024-05-20
author: lxowalle
version: 1.0.0
content: 初版文档
---

## 简介

本文档提供播放音频的使用方法




## 使用方法

### 硬件操作

![image-20240520134637905](../../../static/image/maixcam_hardware_back.png)

`MaixCAM`没有内置喇叭,因此需要自行焊接一个功率在`1W`内的喇叭。喇叭焊接的引脚见上图的Speaker对应的`VOP``VON`脚。

注:如果`MaixCAM`在这两个脚上连接了铜柱,则可以直接焊接在铜柱上,为了美观也可以焊接在板子的另一面。

### 编写代码

播放一个PCM文件的方法

```python
from maix import audio, time, app

p = audio.Player()

with open('/root/output.pcm', 'rb') as f:
ctx = f.read()

p.play(bytes(ctx))

while not app.need_exit():
time.sleep_ms(10)

print("play finish!")
```

步骤:


1. 导入audio、time和app模块

```python
from maix import audio, time, app
```

2. 初始化播放器

```python
p = audio.Player()
```
- 注意默认的采样率是48k,采样格式为小端格式-有符号16位,采样通道为1。你也可以像这样自定义参数`p = audio.Player(sample_rate=48000, format=audio.Format.FMT_S16_LE, channel = 1)`。目前只测试过采样率48000,`FMT_S16_LE`格式,和采样通道数为1

3. 打开并播放一个PCM文件

```python
with open('/root/output.pcm', 'rb') as f:
ctx = f.read()

p.play(bytes(ctx))

while not app.need_exit():
time.sleep_ms(10)
```
- `with open('xxx','rb') as f:`打开文件`xxx`, 并获取文件对象`f`
- `ctx = f.read()`将读取文件的内容到`ctx`
- `p.play(bytes(ctx))`播放音频,`p`是已打开的播放器对象, `ctx`是转换为bytes类型的`PCM`数据
- `time.sleep_ms(10)`这里有一个循环来等待播放完成,因为播放操作是异步执行的,如果提前退出了程序,那么可能导致音频不会完全播放。

4. 完成
63 changes: 63 additions & 0 deletions docs/doc/zh/audio/record.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
title: MaixPy 录音
update:
- date: 2024-05-20
author: lxowalle
version: 1.0.0
content: 初版文档
---

## 简介

本文档提供录音的使用方法



### 使用方法

一个录音的示例

```python
from maix import audio, time, app

r = audio.Recorder()
r.volume(12)
print("sample_rate:{} format:{} channel:{}".format(r.sample_rate(), r.format(), r.channel()))

while not app.need_exit():
data = r.record()
print("data size", len(data))

time.sleep_ms(10)

print("record finish!")
```

步骤:

1. 导入audio、time和app模块

```python
from maix import audio, time, app
```

2. 初始化录制器

```python
r = audio.Recorder()
r.volume(12)
```

- 注意默认的采样率是48k,采样格式为小端格式-有符号16位,采样通道为1。你也可以像这样自定义参数`p = audio.Recorder(sample_rate=48000, format=audio.Format.FMT_S16_LE, channel = 1)`。目前只测试过采样率48000,`FMT_S16_LE`格式,和采样通道数为1

- `r.volume(12)`用来设置音量,音量范围为[0,24]

3. 开始录制

```python
data = r.record()
```

- `data``PCM`格式的`bytes`类型数据,保存了当前录入的音频。`PCM`格式在初始化`Recorder`对象时设置,见步骤2.

4. 完成,做自己的应用时可以对`r.record()`返回的`PCM`数据做语音处理。
Binary file added docs/static/image/maixcam_hardware_back.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 0 additions & 5 deletions examples/vision/audio/audio_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@
r.volume(12)
print("sample_rate:{} format:{} channel:{}".format(r.sample_rate(), r.format(), r.channel()))

record_ms = 2000
start_ms = time.time_ms()
while not app.need_exit():
data = r.record()
print("data size", len(data))

if time.time_ms() - start_ms > record_ms:
app.set_exit_flag(True)

time.sleep_ms(10)

print("record finish!")

0 comments on commit e5021cb

Please sign in to comment.