Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

解码完pcm全是空

Open
#124 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
18/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
c, embedded-iot

Research direction

This is a usage question against the single-header decoder rather than a library defect: the pasted decode() wrapper calls mp3dec_decode_frame (minimp3.h) and then memcpy's into pcm_buffer. Start by checking whether the call returns a non-zero sample count and whether info.frame_bytes and the remaining-length bookkeeping are consistent before the copy. Done would be a reply plus a small ESP32/I2S example showing non-zero PCM, not a change to the decoder. The thread has sat unanswered for months, so treat it as triage rather than a first patch.

Written by the indexing model from the issue text.

Description

typedef struct {
    unsigned char *pcm_buf;
    int pcm_length; 
} MusicTaskParams;

void music(void *pvParameters)
{
    MusicTaskParams *params = (MusicTaskParams*)pvParameters;
    unsigned char *pcm_buf = params->pcm_buf;
    int pcm_length = params->pcm_length;
    volatile long long int i2s_table_size = 0;
    while (1)
    {
        for (int i = 0; i < 1000; ++i) {
            printf("PCM 数据[%d]: %d\n", i, ((short*)pcm_buf)[i]);
        }

        if (i2s_table_size >= pcm_length)
        {
            audio_stop();
            vTaskDelay(10);
            break;
        }
        i2s_table_size = i2s_table_size + i2s_tx_write(pcm_buf, WAV_TX_BUFSIZE);
        vTaskDelay(1);
        
    }
    vTaskDelete(NULL);
}

int decode(mp3dec_t *dec, mp3dec_frame_info_t *info, unsigned char *data, int *length, unsigned char *decoded, int *decoded_length) {
    int samples;
    short pcm[MINIMP3_MAX_SAMPLES_PER_FRAME];
    samples = mp3dec_decode_frame(dec, data, *length, pcm, info);
    *decoded_length = samples * info->channels * 2;
    *length -= info->frame_bytes;
    unsigned char buffer[samples * info->channels * 2];
    memcpy(buffer, (unsigned char*)&(pcm), sizeof(short) * samples * info->channels);
    memcpy(decoded, buffer, sizeof(short) * samples * info->channels);
    return info->frame_bytes;
}
int mp3_play(const unsigned char* audio_data, int audio_length){
    es8388_adda_cfg(1, 0);                                      /* 开启DAC关闭ADC */
    es8388_output_cfg(1, 1);                                    /* DAC选择通道1输出 */
    int remaining_audio_length = audio_length; 
    printf("解码的音频长度为%d\n",audio_length);
    mp3dec_init(&mp3d);
    mp3dec_frame_info_t info;
    int total_bytes_consumed = 0;
    int pcm_total_length = 0;
    int max_pcm_size = (audio_length / 1152 + 1) * MINIMP3_MAX_SAMPLES_PER_FRAME;
    unsigned char* pcm_buffer = (unsigned char*)malloc(max_pcm_size * sizeof(short));
    if (!pcm_buffer) {
        printf("内存分配失败\n");
        return 1;
    }

    while (audio_length > 0) {
        unsigned char decoded[MINIMP3_MAX_SAMPLES_PER_FRAME * 2];
        int decoded_length = 0;
        
        int frame_bytes = decode(&mp3d, &info, (unsigned char *)(audio_data + total_bytes_consumed), &remaining_audio_length, decoded, &decoded_length);
        if (frame_bytes == 0) {
            break; // 如果 frame_bytes 为 0,表示数据不足以解码一帧
        }

        memcpy(pcm_buffer + pcm_total_length,decoded, decoded_length);
        pcm_total_length += decoded_length;
        total_bytes_consumed += frame_bytes;
    }
    es8388_sai_cfg(0, 3);
    i2s_set_samplerate_bits_sample(info.hz,I2S_BITS_PER_SAMPLE_16BIT);
        // 创建任务参数结构体
    MusicTaskParams *taskParams = (MusicTaskParams*)malloc(sizeof(MusicTaskParams));
    taskParams->pcm_buf = pcm_buffer;
    taskParams->pcm_length = pcm_total_length;
    audio_stop();
    if (MUSICTask_Handler == NULL)
    {
        taskENTER_CRITICAL(&my_spinlock);
                /* 创建任务1 */
        xTaskCreatePinnedToCore((TaskFunction_t )music,                 /* 任务函数 */
                                (const char*    )"music",               /* 任务名称 */
                                (uint16_t       )MUSIC_STK_SIZE,        /* 任务堆栈大小 */
                                (void*          )taskParams,            /* 传入给任务函数的参数 */
                                (UBaseType_t    )MUSIC_PRIO,            /* 任务优先级 */
                                (TaskHandle_t*  )&MUSICTask_Handler,    /* 任务句柄 */
                                (BaseType_t     ) 0);                   /* 该任务哪个内核运行 */
        taskEXIT_CRITICAL(&my_spinlock);
    }
    audio_start();
    vTaskDelay(100);
    return 0;
}

size_t i2s_tx_write(uint8_t *buffer, uint32_t frame_size)
{
    size_t bytes_written;
    i2s_write(I2S_NUM, buffer, frame_size, &bytes_written, 100);
    return bytes_written;
}

这是我利用minimp3库进行mp3解码然后输送到i2s进行播放,开发板式esp32,开发工具是esp-idf,但是解码到得到的pcm数据始终为空,是我使用方法有问题吗?也找不到具体的使用文档

Dominant language
C
Stars
2k
Forks
271
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from lieff/minimp3

All issues in lieff/minimp3

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.