Skip to content
Open
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
62 changes: 62 additions & 0 deletions port/boards/labplus_for_xuejing/bsp_audio_board.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ static const audio_codec_ctrl_if_t *codec_ctrl_if = NULL;
static const audio_codec_if_t *codec_if = NULL;
static esp_codec_dev_handle_t codec_dev = NULL;

static int s_play_sample_rate = 16000;
static int s_play_channel_format = 1;
static int s_bits_per_chan = 16;


static esp_err_t bsp_i2s_init(uint32_t sample_rate, int channel_format, int bits_per_sample)
{
esp_err_t ret_val = ESP_OK;
Expand Down Expand Up @@ -274,6 +279,63 @@ esp_err_t bsp_audio_play(const int8_t *data, int length, TickType_t ticks_to_wa

return ret;
}
esp_err_t bsp_audio_play2(const int16_t* data, int length, TickType_t ticks_to_wait)
{
size_t bytes_write = 0;
esp_err_t ret = ESP_OK;
if (!codec_dev) {
return ESP_FAIL;
}

int out_length= length;
int audio_time = 1;
audio_time *= (16000 / s_play_sample_rate);
audio_time *= (2 / s_play_channel_format);

int *data_out = NULL;
if (s_bits_per_chan != 32) {
out_length = length * 2;
data_out = malloc(out_length);
for (int i = 0; i < length / sizeof(int16_t); i++) {
int ret = data[i];
data_out[i] = ret << 16;
}
}

int *data_out_1 = NULL;
if (s_play_channel_format != 2 || s_play_sample_rate != 16000) {
out_length *= audio_time;
data_out_1 = malloc(out_length);
int *tmp_data = NULL;
if (data_out != NULL) {
tmp_data = data_out;
} else {
tmp_data = data;
}

for (int i = 0; i < out_length / (audio_time * sizeof(int)); i++) {
for (int j = 0; j < audio_time; j++) {
data_out_1[audio_time * i + j] = tmp_data[i];
}
}
if (data_out != NULL) {
free(data_out);
data_out = NULL;
}
}

if (data_out != NULL) {
ret = esp_codec_dev_write(codec_dev, (void *)data_out, out_length);
free(data_out);
} else if (data_out_1 != NULL) {
ret = esp_codec_dev_write(codec_dev, (void *)data_out_1, out_length);
free(data_out_1);
} else {
ret = esp_codec_dev_write(codec_dev, (void *)data, length);
}

return ret;
}

/* 做为afe is_get_raw_channel = true :16K 16bit 左声道 + 右声道,无ref,
is_get_raw_channel = false :16K 16bit 左声道 + 右声道 + 一路ref,*/
Expand Down
3 changes: 3 additions & 0 deletions port/boards/labplus_for_xuejing/mpconfigboard.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ set(MICROPY_SOURCE_BOARD
${MPY_PORT_DIR}/drivers/startup/oled.c
${MPY_PORT_DIR}/drivers/startup/startup.c
${MPY_PORT_DIR}/drivers/rfid/mfrc522.c
${MPY_PORT_DIR}/drivers/tts/tts_pcm.c
${MPY_PORT_DIR}/lib/utils/pyexec.c
${MPY_PORT_DIR}/builtins/modmusictunes.c
${MPY_PORT_DIR}/builtins/modmusic.c
Expand All @@ -51,6 +52,7 @@ set(MICROPY_SOURCE_BOARD
${MPY_PORT_DIR}/builtins/modframebuf.c
${MPY_PORT_DIR}/builtins/modaudio.c
${MPY_PORT_DIR}/builtins/modrfid.c
${MPY_PORT_DIR}/builtins/modtest.c
# ${MPY_PORT_DIR}/builtins/mod_audio/audio_player.c
# ${MPY_PORT_DIR}/builtins/mod_audio/audio_recorder.c
# ${MPY_PORT_DIR}/builtins/mod_audio/vfs_stream.c
Expand All @@ -60,6 +62,7 @@ set(MICROPY_SOURCE_BOARD
set(MICROPY_SOURCE_BOARD_DIR
${MPY_PORT_DIR}/drivers
${MPY_PORT_DIR}/drivers/audio/include
${MPY_PORT_DIR}/drivers/tts
${MPY_PORT_DIR}/lib
${MPY_PORT_DIR}/builtins
${MPY_PORT_DIR}/boards
Expand Down
15 changes: 7 additions & 8 deletions port/boards/labplus_for_xuejing/partitions-8MiB.csv
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Notes: the offset of the partition table itself is set in
# $IDF_PATH/components/partition_table/Kconfig.projbuild.
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x6000,
phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 0x200000,
vfs, data, spiffs, 0x210000, 0x1f0000,
font, data, nvs, 0x400000, 0x200000,
user_nvs, data, nvs, 0x7fc000, 0x4000,

# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x6000,
phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 0x2f0000,
vfs, data, fat, 0x300000, 0x100000,
font, data, fat, 0x400000, 0x3fc000,
user_nvs, data, nvs, 0x7fc000, 0x4000,
3 changes: 2 additions & 1 deletion port/builtins/modaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ static MP_DEFINE_CONST_FUN_OBJ_0(audio_loudness_obj, audio_loudness);

static mp_obj_t audio_record(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args)
{
audio_init();
enum { ARG_file_name, ARG_record_time, ARG_bits_per_sample, ARG_channels, ARG_sampleRate};
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_file_name, MP_ARG_REQUIRED | MP_ARG_OBJ },
Expand Down Expand Up @@ -193,4 +194,4 @@ const mp_obj_module_t mp_module_audio = {
};

MP_REGISTER_MODULE(MP_QSTR_audio, mp_module_audio);
// #endif //#if MICROPY_PY_AUDIO
// #endif //#if MICROPY_PY_AUDIO
39 changes: 39 additions & 0 deletions port/builtins/modtest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "py/obj.h"
#include "py/runtime.h"
#include "esp_tts.h"
#include "tts_pcm.h"

#ifndef STATIC
#define STATIC static
#endif

// MicroPython 绑定函数
STATIC mp_obj_t mp_tts_generate(mp_obj_t text_obj) {
// 获取文本参数
const char* text = mp_obj_str_get_str(text_obj);

// 直接调用 text_to_speech 播放音频
text_to_speech(text);

return mp_const_none; // 播放完成后返回 None
}

STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_tts_generate_obj, mp_tts_generate);


// 模块定义
STATIC const mp_rom_map_elem_t tts_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_tts) },
{ MP_ROM_QSTR(MP_QSTR_generate), MP_ROM_PTR(&mp_tts_generate_obj) },
};
STATIC MP_DEFINE_CONST_DICT(tts_module_globals, tts_module_globals_table);

// 模块结构体
const mp_obj_module_t tts_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&tts_module_globals,
};

// 注册模块
MP_REGISTER_MODULE(MP_QSTR_tts, tts_module);

44 changes: 44 additions & 0 deletions port/builtins/modtest.c.bk
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "py/obj.h"
#include "py/runtime.h"
#include "esp_tts.h"
#include "tts_pcm.h"

#ifndef STATIC
#define STATIC static
#endif

// MicroPython绑定函数
STATIC mp_obj_t mp_tts_generate(mp_obj_t text_obj) {
// 获取参数
const char* text = mp_obj_str_get_str(text_obj);

// 生成 PCM 数据
int pcm_len = 0;
short* pcm_data = text_to_pcm(text, &pcm_len);

if (!pcm_data || pcm_len == 0) {
return mp_const_none; // 失败返回 None
}

// 返回 PCM 数据为字节数组
return mp_obj_new_bytearray_by_ref(pcm_len * sizeof(short), pcm_data);
}

STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_tts_generate_obj, mp_tts_generate);

// 模块定义
STATIC const mp_rom_map_elem_t tts_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_tts) },
{ MP_ROM_QSTR(MP_QSTR_generate), MP_ROM_PTR(&mp_tts_generate_obj) },
};
STATIC MP_DEFINE_CONST_DICT(tts_module_globals, tts_module_globals_table);

// 模块结构体
const mp_obj_module_t tts_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&tts_module_globals,
};

// 注册模块
MP_REGISTER_MODULE(MP_QSTR_tts, tts_module);

1 change: 1 addition & 0 deletions port/drivers/audio/include/bsp_audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ esp_err_t bsp_codec_dev_open(int sample_rate, int channel_format, int bits_per_s
esp_err_t bsp_codec_dev_close(void);

esp_err_t bsp_audio_play(const int8_t *data, int length, TickType_t ticks_to_wait);
esp_err_t bsp_audio_play2(const int16_t *data, int length, TickType_t ticks_to_wait);

/**
* @brief Get the record pcm data.
Expand Down
2 changes: 2 additions & 0 deletions port/drivers/audio/include/recorder.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __RECORDER_
#define __RECORDER_
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/event_groups.h"
#include "ringbuf.h"
#include "py/obj.h"
Expand All @@ -27,5 +28,6 @@ typedef struct
void recorder_record(const char *filename, wav_fmt_t fmt, int time);

extern recorder_handle_t *recorder;
extern SemaphoreHandle_t xBinarySemaphore;

#endif
3 changes: 3 additions & 0 deletions port/drivers/audio/include/wav_codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
#include "py/obj.h"
#include "freertos/ringbuf.h"
#include "wave_head.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"


#ifdef __cplusplus
extern "C" {
Expand Down
6 changes: 6 additions & 0 deletions port/drivers/audio/recorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <ctype.h>
#include <math.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_system.h"
#include "esp_log.h"
#include "py/stream.h"
Expand All @@ -23,6 +25,7 @@
#include "player.h"
#include "msg.h"


// #define TAG "recorder"

recorder_handle_t *recorder = NULL;
Expand Down Expand Up @@ -65,7 +68,10 @@ void recorder_record(const char *filename, wav_fmt_t fmt, int time)
recorder->total_frames = recorder->time * (recorder->wav_fmt.sampleRate * recorder->wav_fmt.channels * recorder->wav_fmt.bits_per_sample / 8) / READ_RINGBUF_BLOCK_SIZE;
// ESP_LOGE(TAG, "record total frame: %d, time: %d", recorder->total_frames, recorder->time);
recorder->file_uri = filename;
xBinarySemaphore = xSemaphoreCreateBinary();
xTaskCreatePinnedToCore(&stream_i2s_read_task, "stream_i2s_read_task", 4 * 1024, (void*)recorder, 8, NULL, CORE_NUM1);
xSemaphoreTake(xBinarySemaphore, portMAX_DELAY);
vSemaphoreDelete(xBinarySemaphore);
}

void record_deinit(void)
Expand Down
6 changes: 5 additions & 1 deletion port/drivers/audio/wav_codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -29,6 +30,8 @@
#include "player.h"
#include "recorder.h"

SemaphoreHandle_t xBinarySemaphore; // 定义信号量

static const char *TAG = "wav_codec";

wav_codec_t *wav_file_open(const char *path_in, int mode)
Expand Down Expand Up @@ -99,7 +102,7 @@ void wav_file_read_task(void *arg)
if(!wav_codec){ break; }
player->wav_codec = wav_codec;

if(!wav_codec_ringbuff){
if(!wav_codec_ringbuff){/
wav_codec_ringbuff = xRingbufferCreate(STREAM_OUT_RINGBUF_SIZE, RINGBUF_TYPE_BYTEBUF);
if(!wav_codec_ringbuff){ break; }
}else{
Expand Down Expand Up @@ -291,5 +294,6 @@ void stream_i2s_read_task(void *arg)
free(stream_buff);
}
// ESP_LOGE(TAG, "stream in task end, RAM left: %ld", esp_get_free_heap_size());
xSemaphoreGive(xBinarySemaphore);
vTaskDelete(NULL);
}
Loading