174|1

3

帖子

1

TA的资源

一粒金砂(中级)

楼主
 

2024 DigiKey 创意大赛】之智联音响 [复制链接]

  本帖最后由 rertet 于 2024-11-1 09:02 编辑

一、作品简介

本环境项目模块目的是将有线音响赋值成无线wifi音响

  1. 处于同一局域网下即可实现音频传输
  2. 手机端使用酷狗音进行音乐推送

     

二、系统框图

三、功能说明

  1. ESP32 评估板:负责数据处理和传输
  2. LCD 触摸屏显示器:显示信息
  3. 手机app酷狗客户端

 

四、作品源码

#include "esp_log.h"
#include "nvs_flash.h"
#include "board.h"
#include "esp_peripherals.h"
#include "periph_wifi.h"

#include "audio_mem.h"
#include "esp_wifi.h"
#include "esp_ssdp.h"
#include "esp_dlna.h"

#include "esp_audio.h"
#include "esp_decoder.h"
#include "http_stream.h"
#include "i2s_stream.h"
#include "media_lib_adapter.h"
#include "audio_idf_version.h"

#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 0))
#include "esp_netif.h"
#else
#include "tcpip_adapter.h"
#endif

static const char *TAG = "DLNA_EXAMPLE";

#define DLNA_UNIQUE_DEVICE_NAME "ESP32_DMR_8db0797a"
#define DLNA_DEVICE_UUID "8db0797a-f01a-4949-8f59-51188b181809"
#define DLNA_ROOT_PATH "/rootDesc.xml"

static esp_audio_handle_t player = NULL;
static esp_dlna_handle_t  dlna_handle = NULL;

static int vol, mute;
static char *track_uri = NULL;
static const char *trans_state = "STOPPED";

static void player_event_handler(esp_audio_state_t *state, void *ctx)
{
    if (dlna_handle == NULL) {
        return ;
    }

    switch (state->status) {
        case AUDIO_STATUS_RUNNING:
            trans_state = "PLAYING";
            esp_dlna_notify_avt_by_action(dlna_handle, "TransportState");
            break;
        case AUDIO_STATUS_PAUSED:
            trans_state = "PAUSED_PLAYBACK";
            esp_dlna_notify_avt_by_action(dlna_handle, "TransportState");
            break;
        case AUDIO_STATUS_STOPPED:
        case AUDIO_STATUS_FINISHED:
            trans_state = "STOPPED";
            esp_dlna_notify_avt_by_action(dlna_handle, "TransportState");
            break;
        default:
            break;
    }
}

int renderer_request(esp_dlna_handle_t dlna, const upnp_attr_t *attr, int attr_num, char *buffer, int max_buffer_len)
{
    int req_type;
    int tmp_data = 0, buffer_len = 0;
    int hour = 0, min = 0, sec = 0;

    if (attr_num != 1) {
        return 0;
    }

    req_type = attr->type & 0xFF;
    switch (req_type) {
        case RCS_GET_MUTE:
            ESP_LOGD(TAG, "get mute = %d", mute);
            return snprintf(buffer, max_buffer_len, "%d", mute);
        case RCS_SET_MUTE:
            mute = atoi(buffer);
            ESP_LOGD(TAG, "set mute = %d", mute);
            if (mute) {
                esp_audio_vol_set(player, 0);
            } else {
                esp_audio_vol_set(player, vol);
            }
            esp_dlna_notify(dlna, "RenderingControl");
            return 0;
        case RCS_GET_VOL:
            esp_audio_vol_get(player, &vol);
            ESP_LOGI(TAG, "get vol = %d", vol);
            return snprintf(buffer, max_buffer_len, "%d", vol);
        case RCS_SET_VOL:
            vol = atoi(buffer);
            ESP_LOGI(TAG, "set vol = %d", vol);
            esp_audio_vol_set(player, vol);
            esp_dlna_notify(dlna, "RenderingControl");
            return 0;
        case AVT_PLAY:
            ESP_LOGI(TAG, "Play with speed=%s trans_state %s", buffer, trans_state);
            esp_audio_state_t state = { 0 };
            esp_audio_state_get(player, &state);
            if (state.status == AUDIO_STATUS_PAUSED) {
                esp_audio_resume(player);
                esp_dlna_notify(dlna, "AVTransport");
                break;
            } else if (track_uri != NULL) {
                esp_audio_play(player, AUDIO_CODEC_TYPE_DECODER, track_uri, 0);
                esp_dlna_notify(dlna, "AVTransport");
            }
            return 0;
        case AVT_STOP:
            ESP_LOGI(TAG, "Stop instance=%s", buffer);
            esp_audio_stop(player, TERMINATION_TYPE_NOW);
            esp_dlna_notify_avt_by_action(dlna_handle, "TransportState");
            return 0;
        case AVT_PAUSE:
            ESP_LOGI(TAG, "Pause instance=%s", buffer);
            esp_audio_pause(player);
            esp_dlna_notify_avt_by_action(dlna_handle, "TransportState");
            return 0;
        case AVT_NEXT:
        case AVT_PREV:
            esp_audio_stop(player, TERMINATION_TYPE_NOW);
            return 0;
        case AVT_SEEK:
            sscanf(buffer, "%d:%d:%d", &hour, &min, &sec);
            tmp_data = hour * 3600 + min * 60 + sec;
            ESP_LOGI(TAG, "Seekto %d s", tmp_data);
            esp_audio_seek(player, tmp_data);
            return 0;
        case AVT_SET_TRACK_URI:
            ESP_LOGI(TAG, "SetAVTransportURI=%s", buffer);
            esp_audio_state_t state_t = { 0 };
            esp_audio_state_get(player, &state_t);
            if ((track_uri != NULL) && (state_t.status == AUDIO_STATUS_RUNNING) && strcasecmp(track_uri, buffer)) {
                esp_audio_stop(player, TERMINATION_TYPE_NOW);
                esp_dlna_notify(dlna, "AVTransport");
            }
            free(track_uri);
            track_uri = NULL;
            if (track_uri == NULL) {
                asprintf(&track_uri, "%s", buffer);
            }
            return 0;
        case AVT_SET_TRACK_METADATA:
            ESP_LOGD(TAG, "CurrentURIMetaData=%s", buffer);
            return 0;
        case AVT_GET_TRACK_URI:
            if (track_uri != NULL) {
                return snprintf(buffer, max_buffer_len, "%s", track_uri);
            } else {
                return 0;
            }
        case AVT_GET_PLAY_SPEED:    /* ["1"] */
            return snprintf(buffer, max_buffer_len, "%d", 1);
        case AVT_GET_PLAY_MODE:
            return snprintf(buffer, max_buffer_len, "NORMAL");
        case AVT_GET_TRANS_STATUS:  /* ["ERROR_OCCURRED", "OK"] */
            return snprintf(buffer, max_buffer_len, "OK");
        case AVT_GET_TRANS_STATE:   /* ["STOPPED", "PAUSED_PLAYBACK", "TRANSITIONING", "NO_MEDIA_PRESENT"] */
            ESP_LOGI(TAG, "_avt_get_trans_state %s", trans_state);
            return snprintf(buffer, max_buffer_len, trans_state);
        case AVT_GET_TRACK_DURATION:
        case AVT_GET_MEDIA_DURATION:
            esp_audio_duration_get(player, &tmp_data);
            tmp_data /= 1000;
            buffer_len = snprintf(buffer, max_buffer_len, "%02d:%02d:%02d", tmp_data / 3600, tmp_data / 60, tmp_data % 60);
            ESP_LOGD(TAG, "_avt_get_duration %s", buffer);
            return buffer_len;
        case AVT_GET_TRACK_NO:
            return snprintf(buffer, max_buffer_len, "%d", 1);
        case AVT_GET_TRACK_METADATA:
            return 0;
        // case AVT_GET_POS_ABSTIME:
        case AVT_GET_POS_RELTIME:
            esp_audio_time_get(player, &tmp_data);
            tmp_data /= 1000;
            buffer_len = snprintf(buffer, max_buffer_len, "%02d:%02d:%02d", tmp_data / 3600, tmp_data / 60, tmp_data % 60);
            ESP_LOGD(TAG, "_avt_get_time %s", buffer);
            return buffer_len;
        // case AVT_GET_POS_ABSCOUNT:
        case AVT_GET_POS_RELCOUNT:
            esp_audio_pos_get(player, &tmp_data);
            buffer_len = snprintf(buffer, max_buffer_len, "%d", tmp_data);
            ESP_LOGD(TAG, "_avt_get_pos %s", buffer);
            return buffer_len;
    }
    return 0;
}

static int _http_stream_event_handle(http_stream_event_msg_t *msg)
{
    if (msg->event_id == HTTP_STREAM_RESOLVE_ALL_TRACKS) {
        return ESP_OK;
    }
    if (msg->event_id == HTTP_STREAM_FINISH_TRACK) {
        return http_stream_next_track(msg->el);
    }
    if (msg->event_id == HTTP_STREAM_FINISH_PLAYLIST) {
        return http_stream_restart(msg->el);
    }
    return ESP_OK;
}

static void audio_player_init(void)
{
    audio_board_handle_t board_handle = audio_board_init();
    audio_hal_ctrl_codec(board_handle->audio_hal, AUDIO_HAL_CODEC_MODE_DECODE, AUDIO_HAL_CTRL_START);

    esp_audio_cfg_t cfg = DEFAULT_ESP_AUDIO_CONFIG();
    cfg.vol_handle = board_handle->audio_hal;
    cfg.vol_set = (audio_volume_set)audio_hal_set_volume;
    cfg.vol_get = (audio_volume_get)audio_hal_get_volume;
    cfg.prefer_type = ESP_AUDIO_PREFER_MEM;
    cfg.resample_rate = 48000;
    player = esp_audio_create(&cfg);

    // Create readers and add to esp_audio
    http_stream_cfg_t http_cfg = HTTP_STREAM_CFG_DEFAULT();
    http_cfg.event_handle = _http_stream_event_handle;
    http_cfg.type = AUDIO_STREAM_READER;
    http_cfg.enable_playlist_parser = true;
    http_cfg.task_prio = 12;
    http_cfg.stack_in_ext = true;
    audio_element_handle_t http_stream_reader = http_stream_init(&http_cfg);
    esp_audio_input_stream_add(player, http_stream_reader);

    audio_decoder_t auto_decode[] = {
        DEFAULT_ESP_MP3_DECODER_CONFIG(),
        DEFAULT_ESP_WAV_DECODER_CONFIG(),
        DEFAULT_ESP_AAC_DECODER_CONFIG(),
        DEFAULT_ESP_M4A_DECODER_CONFIG(),
        DEFAULT_ESP_TS_DECODER_CONFIG(),
    };
    esp_decoder_cfg_t auto_dec_cfg = DEFAULT_ESP_DECODER_CONFIG();
    esp_audio_codec_lib_add(player, AUDIO_CODEC_TYPE_DECODER, esp_decoder_init(&auto_dec_cfg, auto_decode, 5));

    // Create writers and add to esp_audio
    i2s_stream_cfg_t i2s_writer = I2S_STREAM_CFG_DEFAULT();
    i2s_writer.type = AUDIO_STREAM_WRITER;
    i2s_writer.stack_in_ext = true;
    i2s_writer.task_core = 1;
    audio_element_handle_t i2s_stream_writer = i2s_stream_init(&i2s_writer);
    i2s_stream_set_clk(i2s_stream_writer, 48000, 16, 2);
    esp_audio_output_stream_add(player, i2s_stream_writer);

    // Set default volume
    esp_audio_vol_set(player, 35);
}

static void start_dlna()
{
    const ssdp_service_t ssdp_service[] = {
        { DLNA_DEVICE_UUID, "upnp:rootdevice",                                  NULL },
        { DLNA_DEVICE_UUID, "urn:schemas-upnp-org:device:MediaRenderer:1",      NULL },
        { DLNA_DEVICE_UUID, "urn:schemas-upnp-org:service:ConnectionManager:1", NULL },
        { DLNA_DEVICE_UUID, "urn:schemas-upnp-org:service:RenderingControl:1",  NULL },
        { DLNA_DEVICE_UUID, "urn:schemas-upnp-org:service:AVTransport:1",       NULL },
        { NULL, NULL, NULL },
    };

    ssdp_config_t ssdp_config = SSDP_DEFAULT_CONFIG();
    ssdp_config.udn = DLNA_UNIQUE_DEVICE_NAME;
    ssdp_config.location = "http://${ip}"DLNA_ROOT_PATH;
    esp_ssdp_start(&ssdp_config, ssdp_service);

    static httpd_handle_t httpd = NULL;
    httpd_config_t httpd_config = HTTPD_DEFAULT_CONFIG();
    httpd_config.max_uri_handlers = 25;
    httpd_config.stack_size = 6 * 1024;
    if (httpd_start(&httpd, &httpd_config) != ESP_OK) {
        ESP_LOGI(TAG, "Error starting httpd");
    }

    extern const uint8_t logo_png_start[] asm("_binary_logo_png_start");
    extern const uint8_t logo_png_end[] asm("_binary_logo_png_end");

    dlna_config_t dlna_config = {
        .friendly_name = "ESP32 MD (ESP32 Renderer)",
        .uuid = (const char *)DLNA_DEVICE_UUID,
        .logo               = {
            .mime_type  = "image/png",
            .data       = (const char *)logo_png_start,
            .size       = logo_png_end - logo_png_start,
        },
        .httpd          = httpd,
        .httpd_port     = httpd_config.server_port,
        .renderer_req   = renderer_request,
        .root_path      = DLNA_ROOT_PATH,
        .device_list    = false
    };

    dlna_handle = esp_dlna_start(&dlna_config);

    ESP_LOGI(TAG, "DLNA Started...");
}

void app_main()
{
    esp_log_level_set("*", ESP_LOG_INFO);
    esp_log_level_set(TAG, ESP_LOG_INFO);
    esp_log_level_set("AUDIO_ELEMENT", ESP_LOG_WARN);
    esp_log_level_set("AUDIO_PIPELINE", ESP_LOG_ERROR);
    esp_log_level_set("ESP_AUDIO_CTRL", ESP_LOG_WARN);
    media_lib_add_default_adapter();
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 0))
    ESP_ERROR_CHECK(esp_netif_init());
#else
    tcpip_adapter_init();
#endif

    esp_periph_config_t periph_cfg = DEFAULT_ESP_PERIPH_SET_CONFIG();
    esp_periph_set_handle_t set = esp_periph_set_init(&periph_cfg);
    periph_wifi_cfg_t wifi_cfg = {
        .wifi_config.sta.ssid = "WiFi_8G",
        .wifi_config.sta.password = "IIll1122",
    };
    esp_periph_handle_t wifi_handle = periph_wifi_init(&wifi_cfg);
    esp_periph_start(set, wifi_handle);
    periph_wifi_wait_for_connected(wifi_handle, portMAX_DELAY);

    audio_player_init();
    esp_audio_callback_set(player, player_event_handler, NULL);

    start_dlna();
}

下载地址:我的下载_个人中心- - 电子工程世界EEWORLD

五、作品功能演示视频

因为需要安卓手机的酷狗音乐app,我使用的是苹果的,手上暂时,后续补上

 

六、项目总结

通过本项目,我们构建了一个基于esp32wifi音频传输模块使有线音响赋值无限“意义”。

DigiKey_contest_2024_word.doc

275 KB, 下载次数: 1

最新回复

使用的是什么协议进行通信啊?     详情 回复 发表于 2024-11-1 11:06
点赞 关注
 
 

回复
举报

6060

帖子

6

TA的资源

版主

沙发
 

使用的是什么协议进行通信啊?  

个人签名

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
推荐帖子
IC设计建模:设计公司、EDA、封装厂商新的合作方式(个人在项目中的一些体会)

刚进入实验室几个星期,谈谈自己的一些心得体会,希望能与各位设计高手多多交流,请大家多指教:进入深亚微米设计时代,IC设计再 ...

西门子S7-300 150个问题解答之一(转载)

1:使用CPU 315F和ET 200S时应如何避免出现“通讯故障”消息? 使用CPU S7 315F, ET 200S以及故障安全DI/DO模块,那么您将调用 ...

ARM9开发板资料

这个资料要买个开发板才有送的, 本帖最后由 jxb01033016 于 2009-9-17 00:17 编辑 ]

VHDL程序实例

498987

【复旦微FM33LC046N】时钟输出测试

首先要感谢复旦微FAE 上次时钟一篇里吐槽了复旦微的时钟功能,受到了FAE的关注,教给我了FOUT功能,查看了下手册,果然有 ...

[安信可ESP32-Audio-Kit音频开发板] - 4: 在 Ubuntu 20.04 上运行“esp-adf”build

本帖最后由 MianQi 于 2021-10-7 21:51 编辑 注意事项和设置环节有: 1、下载-解压“esp-idf-v4.2.2.zip” ...

Verilog关于inout端口电路的设计与仿真

// 模块功能说明 // 当方向控制信号“wr_”为“1”时,y_作为输出,值为输入信号a_的值 // 当方向控制信 ...

【花雕动手做】有趣好玩的音乐可视化系列小项目(23)--3合1闪点光纤

偶然心血来潮,想要做一个音乐可视化的系列专题。这个专题的难度有点高,涉及面也比较广泛,相关的FFT和FHT等算法也相当复杂,不 ...

06、【CH246&CH241无线充电套件测评】测评总结

本帖最后由 1nnocent 于 2023-1-27 19:39 编辑 做个【CH246&CH241无线充电套件测评】测评帖子汇总,本次测评的重点是自己 ...

中秋国庆双节快乐

中秋国庆马上到了,大家都有什么打算和安排? 杭州亚运会刚好这个时候,江浙一带出行车票很紧张啊,只能掐头去尾中间几天周边 ...

关闭
站长推荐上一条 1/10 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表