Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【增加】建立工程,导入源代码和使用说明 #1

Merged
merged 2 commits into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 143 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,144 @@
# wavplayer
Minimal music player for wav file.

## 1. 简介

**wavplayer** 是一个简易的 wav 格式的音乐播放器,提供播放和录制 wav 文件的功能,支持播放、停止、暂停、恢复,以及音量调节等功能。

### 1.1. 文件结构

| 文件夹 | 说明 |
| ---- | ---- |
| src | 核心源码,主要实现 wav 播放和录音,以及导出 Finsh 命令行 |
| inc | 头文件目录 |

### 1.2 许可证

wavplayer package 遵循 Apache 2.0 许可,详见 `LICENSE` 文件。

### 1.3 依赖

- RT-Thread 4.0+
- RT-Thread Audio 驱动框架
- optparse 命令行参数解析软件包

### 1.4 配置宏说明

```shell
--- WavPlayer: Minimal music player for wav file play and record.
[*] Enable support for play
(sound0) The play device name
[*] Enable support for record
(sound0) The record device name
Version (v1.0.0) --->
```

**Enable support for play**:使能wav播放功能
**The play device name**:指定播放使用的声卡设备,默认`sound0`
**Enable support for record**:使能wav录音功能
**The record device name**:指定录音使用的声卡设备,默认和播放一致,使用`sound0`

## 2. 使用

wavplayer 的常用功能已经导出到 Finsh 命令行,以便开发者测试和使用。命令主要分为播放和录音两个类别,分别提供不同的功能。

**播放命令提供的功能如下 **

```shell
msh />wavplay -help
usage: wavplay [option] [target] ...

usage options:
-h, --help Print defined help message.
-s URI, --start=URI Play wav music with URI(local files).
-t, --stop Stop playing music.
-p, --pause Pause the music.
-r, --resume Resume the music.
-v lvl, --volume=lvl Change the volume(0~99).
-d, --dump Dump play relevant information.
```

**录音命令提供的功能如下**

```shell
msh />wavrecord -h
usage: wavrecord [option] [target] ...

usage options:
-h, --help Print defined help message.
-s file --start=file <samplerate> <channels> <samplefmt>
record wav music to filesystem.
-t, --stop Stop record.
```

### 2.1 播放功能

- 开始播放

```shell
msh />
msh />wavplay -s song_44.wav
Information:
sampletate 44100
channels 2
sample bits width 16
[I/WAV_PLAYER] play start, uri=song_44.wav
```

- 停止播放

```shell
msh />wavplay -t
[I/WAV_PLAYER] play end
```

- 暂停播放

```shell
msh />
msh />wavplay -p
msh />
```

- 恢复播放

```shell
msh />
msh />wavplay -p
msh />
```

- 设置音量

```shell
msh />
msh />wavplay -v 88
msh />
```

### 2.2 录音功能

- 开始录音

```shell
msh />wavrecord -s test.wav
Information:
sampletate 8000
channels 2
```

- 停止录音

```shell
msh />
msh />wavrecord -t
msh />
```

## 3. 注意事项

- 仅支持采样位数为 16bit 的音频

## 4. 联系方式

- 维护:Zero-Free
- 主页:https://github.com/RT-Thread-packages/wavplayer
25 changes: 25 additions & 0 deletions SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Import('rtconfig')
from building import *

cwd = GetCurrentDir()

path = [cwd,
cwd + '/inc']

src = Split('src/wavhdr.c')

if GetDepend(['PKG_WP_USING_PLAY']):
src += Split('''
src/wavplayer.c
src/wavplayer_cmd.c
''')

if GetDepend(['PKG_WP_USING_RECORD']):
src += Split('''
src/wavrecorder.c
src/wavrecorder_cmd.c
''')

group = DefineGroup('wavplayer', src, depend = ['PKG_USING_WAVPLAYER'], CPPPATH = path)

Return('group')
81 changes: 81 additions & 0 deletions inc/wavhdr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Date Author Notes
* 2019-07-15 Zero-Free first implementation
*/

#ifndef __WAVHDR_H__
#define __WAVHDR_H__

#include <stdio.h>

struct wav_header
{
char riff_id[4]; /* "RIFF" */
int riff_datasize; /* RIFF chunk data size,exclude riff_id[4] and riff_datasize,total - 8 */

char riff_type[4]; /* "WAVE" */

char fmt_id[4]; /* "fmt " */
int fmt_datasize; /* fmt chunk data size,16 for pcm */
short fmt_compression_code; /* 1 for PCM */
short fmt_channels; /* 1(mono) or 2(stereo) */
int fmt_sample_rate; /* samples per second */
int fmt_avg_bytes_per_sec; /* sample_rate * channels * bit_per_sample / 8 */
short fmt_block_align; /* number bytes per sample, bit_per_sample * channels / 8 */
short fmt_bit_per_sample; /* bits of each sample(8,16,32). */

char data_id[4]; /* "data" */
int data_datasize; /* data chunk size,pcm_size - 44 */
};

/**
* @brief Initialize wavfile header
*
* @param header the pointer for wavfile header
* @param channels wavfile channels
* @param sample_rate wavfile samplerate
* @param datasize wavfile total data size
*
* @return
* - 0 Success
* - -1 Error
*/
int wavheader_init(struct wav_header *header, int sample_rate, int channels, int datasize);

/**
* @brief Read wavfile head information from file stream
*
* @param header the pointer for wavfile header
* @param fp file stream
*
* @return
* - 0 Success
* - -1 Error
*/
int wavheader_read(struct wav_header *header, FILE *fp);

/**
* @brief Write wavfile head information to file stream
*
* @param header the pointer for wavfile header
* @param fp file stream
*
* @return
* - 0 Success
* - -1 Error
*/
int wavheader_write(struct wav_header *header, FILE *fp);

/**
* @brief Print wavfile header information
*
* @param header the pointer for wavfile header
*
* @return
* - None
*/
void wavheader_print(struct wav_header *header);

#endif
94 changes: 94 additions & 0 deletions inc/wavplayer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Date Author Notes
* 2019-07-15 Zero-Free first implementation
*/

#ifndef __WAVPLAYER_H__
#define __WAVPLAYER_H__

/**
* wav player status
*/
enum PLAYER_STATE
{
PLAYER_STATE_STOPED = 0,
PLAYER_STATE_PLAYING = 1,
PLAYER_STATE_PAUSED = 2,
};

/**
* @brief Play wav music
*
* @param uri the pointer for file path
*
* @return
* - 0 Success
* - others Failed
*/
int wavplayer_play(char *uri);

/**
* @brief Stop music
*
* @return
* - 0 Success
* - others Failed
*/
int wavplayer_stop(void);

/**
* @brief Pause music
*
* @return
* - 0 Success
* - others Failed
*/
int wavplayer_pause(void);

/**
* @brief Resume music
*
* @return
* - 0 Success
* - others Failed
*/
int wavplayer_resume(void);

/**
* @brief Sev volume
*
* @param volume volume value(0 ~ 99)
*
* @return
* - 0 Success
* - others Failed
*/
int wavplayer_volume_set(int volume);

/**
* @brief Get volume
*
* @return volume value(0~00)
*/
int wavplayer_volume_get(void);

/**
* @brief Get wav player state
*
* @return
* - PLAYER_STATE_STOPED stoped status
* - PLAYER_STATE_PLAYING playing status
* - PLAYER_STATE_PAUSED paused
*/
int wavplayer_state_get(void);

/**
* @brief Get the uri that is currently playing
*
* @return uri that is currently playing
*/
char *wavplayer_uri_get(void);

#endif
50 changes: 50 additions & 0 deletions inc/wavrecorder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Date Author Notes
* 2019-07-16 Zero-Free first implementation
*/

#ifndef __WAVRECORDER_H__
#define __WAVRECORDER_H__

#include <rtthread.h>

struct wavrecord_info
{
char *uri;
rt_uint32_t samplerate;
rt_uint16_t channels;
rt_uint16_t samplefmt;
};

/**
* @brief Start to record
*
* @param info wavfile informations
*
* @return
* - RT_EOK Success
* - < 0 Failed
*/
rt_err_t wavrecorder_start(struct wavrecord_info *info);

/**
* @brief Stop record
*
* @return
* - RT_EOK Success
* - < 0 Failed
*/
rt_err_t wavrecorder_stop(void);

/**
* @brief Get recorder status
*
* @return
* - RT_TRUE actived
* - RT_FALSE non-actived
*/
rt_bool_t wavrecorder_is_actived(void);

#endif
Loading