PicoでMP3を鳴らす
はてなブログの解説記事に対応するソースコードです。コピペの手間を省く即実行ファイルや完全版はcodocエリアから取得できます。
ソースコード
※表示用プレビューです。一括ファイルはページ下部より取得できます。 ソースの説明は本家ブログの方でどうぞ。
📄 main.cpp
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "hardware/interp.h"
#include "pico/cyw43_arch.h"
#include "hardware/uart.h"
#include "lib/pwmwav/pwmwav.hpp"
#include "lib/msp2807/msp2807.hpp"
#include "lib/fatfs/diskio.h"
#include "lib/fatfs/ff.h"
FATFS FatFSMmc;
// 異常発生時警告点滅(継続できないとき)
void error_blink(void) {
bool led_on = true;
while (true) {
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, led_on ? 1 : 0);
led_on = !led_on;
sleep_ms(150);
}
}
extern "C" int main()
{
stdio_init_all();
printf("\n\n");
printf("=========================\n");
printf(" Play Sound\n");
printf(" Version 0.0.0\n");
printf("=========================\n\n");
// Initialise the Wi-Fi chip
if (cyw43_arch_init()) {
printf("Wi-Fi init failed\n");
return -1;
}
// Interpolator example code
interp_config cfg = interp_default_config();
// Now use the various interpolator library functions for your use case
// e.g. interp_config_clamp(&cfg, true);
// interp_config_shift(&cfg, 2);
// Then set the config
interp_set_config(interp0, 0, &cfg);
// For examples of interpolator use see https://github.com/raspberrypi/pico-examples/tree/master/interp
PWMWAV pwmwav = PWMWAV(17, 44100);
pwmwav.set_output_gain(0.75f);
// init filesystem
SPIIF spiif = SPIIF(spi0, PIN_SCK, PIN_MOSI, PIN_MISO);
MSP2807 tft = MSP2807(&spiif, PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_RST);
// init filesystem
DSTATUS stat = mmc_disk_init();
if (stat != RES_OK) {
printf("disk init failed with %d\n", stat);
error_blink();
}
// mount MMC card
FRESULT stat_f;
if ((stat_f = f_mount(&FatFSMmc, "0:", 1)) != FR_OK) {
printf("disk mount failed with %d\n", stat_f);
error_blink();
}
// List directories out
FRESULT res;
DIR dir;
FILINFO fno;
int nfile, ndir;
const char *path = "0:/";
res = f_opendir(&dir, path);
if (res == FR_OK) {
nfile = ndir = 0;
for (;;) {
res = f_readdir(&dir, &fno);
if (fno.fname[0] == 0) break;
if (fno.fattrib & AM_DIR) {
printf(" <DIR> %s\n", fno.fname);
ndir++;
} else {
printf("%10u %s\n", fno.fsize, fno.fname);
nfile++;
}
}
f_closedir(&dir);
printf("%d dirs, %d files.\n", ndir, nfile);
} else {
printf("Failed to open \"%s\". (%u)\n", path, res);
}
// メインループ
bool tft_on = true;
while (true) {
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, tft_on ? 1 : 0);
tft_on = !tft_on;
sleep_ms(1000);
}
}
📄 lib/include/pdmp3.h
/*
* pdmp3.h - A simple MPEG1 Layer 3 decoder for embedded systems.
* This header file is part of the pdmp3 library, which provides functions for decoding MP3 audio streams.
*
* cloned from https://github.com/technosaurus/PDMP3
*/
#ifndef __PDMP3_H__
#define __PDMP3_H__
extern "C" {
enum {
PDMP3_OK = 0,
PDMP3_ERR = -1,
PDMP3_NEED_MORE = -10,
PDMP3_NEW_FORMAT = -11,
PDMP3_NO_SPACE = 7
};
/* Types used in this header */
typedef unsigned short uint16_t;
typedef unsigned char uint8_t;
/* Following Header has been brought in from the original PDMP3 library */
/* Types used in the frame header */
typedef enum { /* Layer number */
mpeg1_layer_reserved = 0,
mpeg1_layer_3 = 1,
mpeg1_layer_2 = 2,
mpeg1_layer_1 = 3
}
t_mpeg1_layer;
typedef enum { /* Modes */
mpeg1_mode_stereo = 0,
mpeg1_mode_joint_stereo,
mpeg1_mode_dual_channel,
mpeg1_mode_single_channel
}
t_mpeg1_mode;
typedef struct { /* MPEG1 Layer 1-3 frame header */
unsigned id; /* 1 bit */
t_mpeg1_layer layer; /* 2 bits */
unsigned protection_bit; /* 1 bit */
unsigned bitrate_index; /* 4 bits */
unsigned sampling_frequency; /* 2 bits */
unsigned padding_bit; /* 1 bit */
unsigned private_bit; /* 1 bit */
t_mpeg1_mode mode; /* 2 bits */
unsigned mode_extension; /* 2 bits */
unsigned copyright; /* 1 bit */
unsigned original_or_copy; /* 1 bit */
unsigned emphasis; /* 2 bits */
}
t_mpeg1_header;
typedef struct { /* MPEG1 Layer 3 Side Information : [2][2] means [gr][ch] */
unsigned main_data_begin; /* 9 bits */
unsigned private_bits; /* 3 bits in mono,5 in stereo */
unsigned scfsi[2][4]; /* 1 bit */
unsigned part2_3_length[2][2]; /* 12 bits */
unsigned big_values[2][2]; /* 9 bits */
unsigned global_gain[2][2]; /* 8 bits */
unsigned scalefac_compress[2][2]; /* 4 bits */
unsigned win_switch_flag[2][2]; /* 1 bit */
/* if(win_switch_flag[][]) */ //use a union dammit
unsigned block_type[2][2]; /* 2 bits */
unsigned mixed_block_flag[2][2]; /* 1 bit */
unsigned table_select[2][2][3]; /* 5 bits */
unsigned subblock_gain[2][2][3]; /* 3 bits */
/* else */
/* table_select[][][] */
unsigned region0_count[2][2]; /* 4 bits */
unsigned region1_count[2][2]; /* 3 bits */
/* end */
unsigned preflag[2][2]; /* 1 bit */
unsigned scalefac_scale[2][2]; /* 1 bit */
unsigned count1table_select[2][2];/* 1 bit */
unsigned count1[2][2]; /* Not in file,calc. by huff.dec.! */
}
t_mpeg1_side_info;
typedef struct { /* MPEG1 Layer 3 Main Data */
unsigned scalefac_l[2][2][21]; /* 0-4 bits */
unsigned scalefac_s[2][2][12][3]; /* 0-4 bits */
float is[2][2][576]; /* Huffman coded freq. lines */
}
t_mpeg1_main_data;
typedef struct hufftables{
const unsigned short * hufftable;
uint16_t treelen;
uint8_t linbits;
}
hufftables;
typedef struct { /* Scale factor band indices,for long and short windows */
unsigned l[23];
unsigned s[14];
}
t_sf_band_indices;
#define INBUF_SIZE (4*4096)
typedef struct
{
size_t processed;
unsigned istart,iend,ostart;
unsigned char in[INBUF_SIZE];
unsigned out[2][576];
t_mpeg1_header g_frame_header;
t_mpeg1_side_info g_side_info; /* < 100 words */
t_mpeg1_main_data g_main_data;
unsigned hsynth_init;
unsigned synth_init;
/* Bit reservoir for main data */
unsigned g_main_data_vec[2*1024];/* Large static data */
unsigned *g_main_data_ptr;/* Pointer into the reservoir */
unsigned g_main_data_idx;/* Index into the current byte(0-7) */
unsigned g_main_data_top;/* Number of bytes in reservoir(0-1024) */
/* Bit reservoir for side info */
unsigned side_info_vec[32+4];
unsigned *side_info_ptr; /* Pointer into the reservoir */
unsigned side_info_idx; /* Index into the current byte(0-7) */
char new_header;
}
pdmp3_handle;
pdmp3_handle* pdmp3_new(const char *decoder,int *error);
void pdmp3_delete(pdmp3_handle *id);
int pdmp3_open_feed(pdmp3_handle *id);
int pdmp3_feed(pdmp3_handle *id,const unsigned char *in,size_t size);
int pdmp3_read(pdmp3_handle *id,unsigned char *outmemory,size_t outsize,size_t *done);
int pdmp3_decode(pdmp3_handle *id,const unsigned char *in,size_t insize,unsigned char *out,size_t outsize,size_t *done);
int pdmp3_getformat(pdmp3_handle *id,long *rate,int *channels,int *encoding);
void pdmp3(char * const *mp3s);
}
#endif /* __PDMP3_h__ */