📄 song_drv.c
字号:
return 0;
}
/*F**************************************************************************
* NAME: song_init
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
* header status: SONG_NO_ERR: header ok
* SONG_LAYER_ERR: not a layer 3 file
* SONG_BR_ERR: bit rate not supported
* SONG_FS_ERR: bad frequency sampling
* SONG_SYNC_ERR: no header found
*----------------------------------------------------------------------------
* PURPOSE:
* Search frame header and initialize song playing according to this header
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* Header search is stopped when errors number exceeds
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Byte song_init (void)
{
Byte b, c;
Byte head_status; /* return error byte */
Byte err_count; /* error counter */
bit err_set; /* error flag */
Byte bit_rate; /* Bit Rate */
Byte song_fs; /* Frequency sample */
Byte song_version; /* Song verison : MPEG1 or MPEG2 */
Uint16 song_one_frame_size; /* Size of one frame */
head_status = SONG_NO_ERR;
err_count = SONG_MAX_ERR;
while (!Feof())
{
if (err_count != 0)
{ /* continue searching synchro */
if (Fgetc() == MP3_SYNC_H)
{ /* high order sync found */
b = Fgetc();
if ((b & MP3_MSK_SYNC) == MP3_SYNC_L)
{ /* low order sync found */
if ((b & MP3_MSK_LAYER) != MP3_LAYER_III)
{
Fseek( (Int16) -1 );
head_status |= SONG_LAYER_ERR; /* bad layer */
err_count--;
}
else
{
c = Fgetc();
if (c & 0x02)
song_one_frame_size = 1;
else
song_one_frame_size = 0;
b = (b & MP3_MSK_VER) >> 1; /* keep MPEG Version B3 -> B2 */
if (b)
{
song_version = 0; /* Version MPEG1 */
}
else
{
song_version = 1; /* Version MPEG2 */
}
bit_rate = c >> 4;
song_fs = (c & MP3_MSK_FS) >> 2;
switch (song_fs)
{
case 0x00 : /* 44100 Hz / 22050 Hz */
song_one_frame_size += song_tab_frame_size_00[song_version][bit_rate];
break;
case 0x01 : /* 48000 Hz / 24000 Hz */
song_one_frame_size += song_tab_frame_size_01[song_version][bit_rate];
break;
case 0x10 : /* 32000 Hz / 16000 Hz */
song_one_frame_size += song_tab_frame_size_10[song_version][bit_rate];
break;
}
song_frame_size = (song_tab_seek[song_version][bit_rate]) >> 3;
b |= (c & MP3_MSK_FS) >> 2; /* read FS B3:2 -> B1:0 */
c &= MP3_MSK_BR;
err_set = FALSE;
switch (b)
{
case MP3_FS_44:
case MP3_FS_48:
case MP3_FS_32:
case MP3_FS_22:
case MP3_FS_24:
case MP3_FS_16:
{
if (c == MP3_BR_FREE)
{
head_status |= SONG_BR_ERR; /* bad bit-rate */
err_count--;
err_set = TRUE;
}
break;
}
default:
{
head_status |= SONG_FS_ERR; /* bad sampling frequency */
err_count--;
err_set = TRUE;
break;
}
}
if (!err_set)
{
Fseek((Int16)(song_one_frame_size) - 3);
if (Fgetc() == MP3_SYNC_H) /* first header byte */
{ /* high order sync found */
b = Fgetc(); /* second header byte */
if ((b & MP3_MSK_SYNC) == MP3_SYNC_L)
{ /* low order sync found */
if ((b & MP3_MSK_LAYER) != MP3_LAYER_III)
{
head_status |= SONG_LAYER_ERR; /* bad layer */
err_count--;
Fseek(-(Int16)(song_one_frame_size) - 1 );
}
else
{
c = Fgetc(); /* third header byte */
if ( song_fs == ((c & MP3_MSK_FS) >> 2) )
{
b = (b & MP3_MSK_VER) >> 1; /* keep MPEG Version B3 -> B2 */
b |= (c & MP3_MSK_FS) >> 2; /* read FS B3:2 -> B1:0 */
c &= MP3_MSK_BR;
song_audio_init(); /* init audio interface */
clock_song_init(b); /* program the song clocks */
Fseek(-(Int16)(song_one_frame_size) - 3);
return SONG_NO_ERR;
}
else
{ /* Frequency sample does not match */
Fseek(-(Int16)(song_one_frame_size) - 2);
}
}
}
else
{ /* no low order synchro found */
Fseek(-(Int16)(song_one_frame_size) - 1);
}
}
else
{ /* No high order synchro found */
Fseek(-(Int16)(song_one_frame_size));
}
}
else
{ /* bad bit-rate or bad sampling frequency */
Fseek( (Int16) -2 );
}
}
}
else
{ /* No low order sync found */
Fseek( (Int16) -1 );
}
}
}
else
{ /* too much error */
return head_status;
}
}
return (head_status | SONG_SYNC_ERR);
}
/*F**************************************************************************
* NAME: song_audio_init
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Audio interface initialization in song mode
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* DAC_NB_BIT defined in config.h
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void song_audio_init (void)
{
Aud_set_pcm_32(DAC_NB_BIT);
Aud_set_song();
Aud_enable();
Aud_disable_int(); /* disable audio interrupt */
}
/*F**************************************************************************
* NAME: song_start
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Start song playing
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void song_start (void)
{
Dac_unmute(); /* maestro please! */
Aud_song_play(); /* start sample request */
mp3_init(); /* enable mp3 decoder */
}
/*F**************************************************************************
* NAME: song_pause
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Stop song playing
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* clocks are not disabled by this functions
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void song_pause (void)
{
Aud_song_pause(); /* disable audio */
mp3_stop(); /* disable mp3 macrocell */
}
/*F**************************************************************************
* NAME: song_stop
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Stop song playing
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void song_stop (void)
{
Dac_mute(); /* silence please! */
aud_stop(); /* disable audio */
mp3_stop(); /* disable mp3 macrocell */
clock_disable(); /* disable clocks */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -