📄 song_drv.c
字号:
/*C**************************************************************************
* NAME: song_drv.c
*----------------------------------------------------------------------------
* Copyright (c) 2003 Atmel.
*----------------------------------------------------------------------------
* RELEASE: snd1c-refd-nf-4_0_3
* REVISION: 1.20
*----------------------------------------------------------------------------
* PURPOSE:
* This file contains the song driver routines
*****************************************************************************/
/*_____ I N C L U D E S ____________________________________________________*/
#include "config.h" /* system configuration */
#include "lib_mcu\aud\aud_drv.h" /* dac driver definition */
#include "lib_mcu\mp3\mp3_drv.h" /* mp3 driver definition */
#include "modules\file\file.h" /* file definition */
#include "lib_mcu\clock\clock.h" /* clock definition */
#include "song_drv.h" /* song driver definition */
/*_____ M A C R O S ________________________________________________________*/
/*_____ D E F I N I T I O N ________________________________________________*/
Byte song_sound; /* save the selected sound */
Byte mp3_volume;
xdata Uint16 song_frame_size; /* Frame size value for one second */
idata Byte sound_volume; /* save actual sound level */
/*_____ D E C L A R A T I O N ______________________________________________*/
static void song_audio_init (void);
code Uint16 song_tab_frame_size_00 [2][16] =
{/* 44.1 KHz MPEG1 */
{0, 104, 130, 156, 182, 208, 261, 313, 365, 417, 522, 626, 731, 835, 1044, 0},
/* 22.05 KHz MPEG2 */
{0, 26, 52, 78, 104, 130, 156, 182, 208, 261, 313, 365, 417, 470, 522, 0}
};
code Uint16 song_tab_frame_size_01 [2][16] =
{
/* 48 Khz MPEG1 */
{0, 96, 120, 144, 168, 192, 240, 288, 336, 384, 480, 576, 672, 768, 960, 0},
/* 24 Khz MPEG2 */
{0, 24, 48, 72, 96, 120, 144, 168, 192, 240, 288, 336, 384, 432, 480, 0}
};
code Uint16 song_tab_frame_size_10 [2][16] =
{/* 32 Khz MPEG1 */
{0, 144, 180, 216, 252, 288, 360, 432, 504, 576, 720, 864, 1008, 1152, 1440, 0},
/* 16 Khz MPEG2 */
{0, 36, 72, 108, 144, 180, 216, 252, 288, 360, 432, 504, 576, 648, 720, 0}
};
code Uint16 song_tab_seek [2][16]=
{
{0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0},
{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0}
};
/*F**************************************************************************
* NAME: song_snd_init
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Sound control initialization
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* Initializes the sound controls to their medium value
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void song_snd_init (void)
{
MP3BAS = BASS_MED;
MP3MED = MEDIUM_MED;
MP3TRE = TREBLE_MED;
MP3VOL = VOLUME_MED;
MP3VOR = VOLUME_MED;
mp3_volume = VOLUME_MED;
song_sound = SND_VOLUME; /* volume control selected by default */
}
/*F**************************************************************************
* NAME: song_snd_inc
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Increment the selected sound control
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* The number of the current selected sound control is stored in the
* variable snd_select (0.Volume; 1.Bass; 2.Medium; 3. Trebble)
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void song_snd_inc (void)
{
switch (song_sound)
{
case SND_VOLUME:
if (mp3_volume != VOLUME_MAX)
{
mp3_volume++;
MP3VOL++;
MP3VOR++;
/* if ((MP3CON&MSK_MPBBST) && (MP3VOL > VOLUME_MED || MP3VOR > VOLUME_MED))
Mp3_clr_bass_boost(); /* should be less than -15dB */
}
break;
case SND_BASS:
{
if (MP3BAS != BASS_MAX)
MP3BAS++;
break;
}
case SND_MEDIUM:
{
if (MP3MED != MEDIUM_MAX)
MP3MED++;
break;
}
case SND_TREBLE:
{
if (MP3TRE != TREBLE_MAX)
MP3TRE++;
break;
}
case SND_BBOOST:
if(MP3VOL > VOLUME_MED || MP3VOR > VOLUME_MED) /* should be less than -15dB */
// dac_set_vol(VOLUME_MED);
Mp3_set_bass_boost();
break;
}
}
/*F**************************************************************************
* NAME: song_snd_dec
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Decrement the selected sound control
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* The number of the current selected sound control is stored in the
* variable snd_select (0.Volume; 1.Bass; 2.Medium; 3. Trebble)
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void song_snd_dec (void)
{
switch (song_sound)
{
case SND_VOLUME:
if (mp3_volume != VOLUME_MIN) //dac_set_vol(--mp3_volume);
{
mp3_volume--;
MP3VOL--;
MP3VOR--;
}
break;
case SND_BASS:
{
if (MP3BAS != BASS_MIN)
MP3BAS--;
break;
}
case SND_MEDIUM:
{
if (MP3MED != MEDIUM_MIN)
MP3MED--;
break;
}
case SND_TREBLE:
{
if (MP3TRE != TREBLE_MIN)
MP3TRE--;
break;
}
case SND_BBOOST:
Mp3_clr_bass_boost();
break;
}
}
/*F**************************************************************************
* NAME: song_snd_select
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Select next sound control
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void song_snd_select (void)
{
song_sound = (song_sound + 1) % NB_SOUND;
}
/*F**************************************************************************
* NAME: song_get_sound
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
* sound selected: SND_VOLUME, SND_BASS, SND_MEDIUM, SND_TREBLE
*----------------------------------------------------------------------------
* PURPOSE:
* Returns selected sound control
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Byte song_get_sound (void)
{
return (song_sound);
}
/*F**************************************************************************
* NAME: song_get_level
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
* level of sound selected
*----------------------------------------------------------------------------
* PURPOSE:
* Returns selected sound level
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Byte song_get_level (void)
{
switch (song_sound)
{
case SND_VOLUME: return mp3_volume;
case SND_BASS: return MP3BAS;
case SND_MEDIUM: return MP3MED;
case SND_TREBLE: return MP3TRE;
case SND_BBOOST: return (MP3CON&MSK_MPBBST)?1:0;
}
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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -