⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mp3_playback.c

📁 编译后直接运行的MP3播放器全部C语言源代码 一个包含FAT文件系统、系统引导 Boot、FLASH Driver等内容的
💻 C
📖 第 1 页 / 共 2 页
字号:
    mas_writereg(MAS_REG_KPRESCALE, prescale_table[prescale/10]);        /* gain up the analog volume to compensate the prescale reduction gain */    l = current_left_volume + prescale;    r = current_right_volume + prescale;    dac_volume(tenthdb2reg(l), tenthdb2reg(r), false);}#endif /* HAVE_MAS3507D */#endif /* !SIMULATOR */void mpeg_sound_set(int setting, int value){#ifdef SIMULATOR    setting = value;#else#ifdef HAVE_MAS3507D    int l, r;#else    int tmp;#endif    if(!mpeg_is_initialized)        return;        switch(setting)    {        case SOUND_VOLUME:#ifdef HAVE_MAS3587F            tmp = 0x7f00 * value / 100;            mas_codec_writereg(0x10, tmp & 0xff00);#else            l = value;            r = value;                        if(current_balance > 0)            {                l -= current_balance;                if(l < 0)                    l = 0;            }                        if(current_balance < 0)            {                r += current_balance;                if(r < 0)                    r = 0;            }                        l = 0x38 * l / 100;            r = 0x38 * r / 100;            /* store volume in tenth of dB */            current_left_volume = ( l < 0x08 ? l*30 - 780 : l*15 - 660 );            current_right_volume = ( r < 0x08 ? r*30 - 780 : r*15 - 660 );            set_prescaled_volume();#endif            break;        case SOUND_BALANCE:#ifdef HAVE_MAS3587F            tmp = ((value * 127 / 100) & 0xff) << 8;            mas_codec_writereg(0x11, tmp & 0xff00);#else            /* Convert to percent */            current_balance = value * 2;#endif            break;        case SOUND_BASS:#ifdef HAVE_MAS3587F            tmp = (((value-12) * 8) & 0xff) << 8;            mas_codec_writereg(0x14, tmp & 0xff00);#else                mas_writereg(MAS_REG_KBASS, bass_table[value]);            current_bass = (value-15) * 10;            set_prescaled_volume();#endif            break;        case SOUND_TREBLE:#ifdef HAVE_MAS3587F            tmp = (((value-12) * 8) & 0xff) << 8;            mas_codec_writereg(0x15, tmp & 0xff00);#else                mas_writereg(MAS_REG_KTREBLE, treble_table[value]);            current_treble = (value-15) * 10;            set_prescaled_volume();#endif            break;            #ifdef HAVE_MAS3587F        case SOUND_SUPERBASS:            if (value) {                tmp = MAX(MIN(value * 12, 0x7f), 0);                mas_codec_writereg(MAS_REG_KMDB_STR, (tmp & 0xff) << 8);                tmp = 0x30; /* MDB_HAR: Space for experiment here */                mas_codec_writereg(MAS_REG_KMDB_HAR, (tmp & 0xff) << 8);                tmp = 60 / 10; /* calculate MDB_FC, 60hz - experiment here,                                  this would depend on the earphones...                                  perhaps make it tunable? */                mas_codec_writereg(MAS_REG_KMDB_FC, (tmp & 0xff) << 8);                tmp = (3 * tmp) / 2; /* calculate MDB_SHAPE */                mas_codec_writereg(MAS_REG_KMDB_SWITCH,                    ((tmp & 0xff) << 8) /* MDB_SHAPE */                    | 2);  /* MDB_SWITCH enable */            } else {                mas_codec_writereg(MAS_REG_KMDB_STR, 0);                mas_codec_writereg(MAS_REG_KMDB_HAR, 0);                mas_codec_writereg(MAS_REG_KMDB_SWITCH, 0); /* MDB_SWITCH disable */            }            break;                    case SOUND_LOUDNESS:            tmp = MAX(MIN(value * 4, 0x44), 0);            mas_codec_writereg(MAS_REG_KLOUDNESS, (tmp & 0xff) << 8);            break;                    case SOUND_AVC:            switch (value) {                case 1: /* 2s */                    tmp = (0x2 << 8) | (0x8 << 12);                    break;                case 2: /* 4s */                    tmp = (0x4 << 8) | (0x8 << 12);                    break;                case 3: /* 8s */                    tmp = (0x8 << 8) | (0x8 << 12);                    break;                case -1: /* turn off and then turn on again to decay quickly */                    tmp = mas_codec_readreg(MAS_REG_KAVC);                    mas_codec_writereg(MAS_REG_KAVC, 0);                    break;                default: /* off */                    tmp = 0;                    break;              }            mas_codec_writereg(MAS_REG_KAVC, tmp);            break;#endif        case SOUND_CHANNELS:            mpeg_sound_channel_config(value);            break;    }#endif /* SIMULATOR */}int mpeg_val2phys(int setting, int value){    int result = 0;        switch(setting)    {        case SOUND_VOLUME:            result = value;            break;                case SOUND_BALANCE:            result = value * 2;            break;                case SOUND_BASS:#ifdef HAVE_MAS3587F            result = value - 12;#else            result = value - 15;#endif            break;                case SOUND_TREBLE:#ifdef HAVE_MAS3587F            result = value - 12;#else            result = value - 15;#endif            break;#ifdef HAVE_MAS3587F        case SOUND_LOUDNESS:            result = value;            break;                    case SOUND_SUPERBASS:            result = value * 10;            break;        case SOUND_LEFT_GAIN:        case SOUND_RIGHT_GAIN:            result = (value - 2) * 15;            break;        case SOUND_MIC_GAIN:            result = value * 15 + 210;            break;#endif    }    return result;}int mpeg_phys2val(int setting, int value){    int result = 0;        switch(setting)    {        case SOUND_VOLUME:            result = value;            break;                case SOUND_BALANCE:            result = value / 2;            break;                case SOUND_BASS:#ifdef HAVE_MAS3587F            result = value + 12;#else            result = value + 15;#endif            break;                case SOUND_TREBLE:#ifdef HAVE_MAS3587F            result = value + 12;#else            result = value + 15;#endif            break;#ifdef HAVE_MAS3587F        case SOUND_SUPERBASS:            result = value / 10;            break;        case SOUND_LOUDNESS:        case SOUND_AVC:        case SOUND_LEFT_GAIN:        case SOUND_RIGHT_GAIN:        case SOUND_MIC_GAIN:            result = value;            break;#endif    }    return result;}void mpeg_sound_channel_config(int configuration){#ifdef SIMULATOR    (void)configuration;#else    unsigned long val_ll = 0x80000;    unsigned long val_lr = 0;    unsigned long val_rl = 0;    unsigned long val_rr = 0x80000;        switch(configuration)    {        case MPEG_SOUND_STEREO:            val_ll = 0x80000;            val_lr = 0;            val_rl = 0;            val_rr = 0x80000;            break;        case MPEG_SOUND_MONO:            val_ll = 0xc0000;            val_lr = 0xc0000;            val_rl = 0xc0000;            val_rr = 0xc0000;            break;        case MPEG_SOUND_MONO_LEFT:            val_ll = 0x80000;            val_lr = 0x80000;            val_rl = 0;            val_rr = 0;            break;        case MPEG_SOUND_MONO_RIGHT:            val_ll = 0;            val_lr = 0;            val_rl = 0x80000;            val_rr = 0x80000;            break;        case MPEG_SOUND_STEREO_NARROW:            val_ll = 0xa0000;            val_lr = 0xe0000;            val_rl = 0xe0000;            val_rr = 0xa0000;            break;        case MPEG_SOUND_STEREO_WIDE:            val_ll = 0x80000;            val_lr = 0x40000;            val_rl = 0x40000;            val_rr = 0x80000;            break;        case MPEG_SOUND_KARAOKE:            val_ll = 0x80001;            val_lr = 0x7ffff;            val_rl = 0x7ffff;            val_rr = 0x80001;            break;    }#ifdef HAVE_MAS3587F    mas_writemem(MAS_BANK_D0, 0x7fc, &val_ll, 1); /* LL */    mas_writemem(MAS_BANK_D0, 0x7fd, &val_lr, 1); /* LR */    mas_writemem(MAS_BANK_D0, 0x7fe, &val_rl, 1); /* RL */    mas_writemem(MAS_BANK_D0, 0x7ff, &val_rr, 1); /* RR */#else    mas_writemem(MAS_BANK_D1, 0x7f8, &val_ll, 1); /* LL */    mas_writemem(MAS_BANK_D1, 0x7f9, &val_lr, 1); /* LR */    mas_writemem(MAS_BANK_D1, 0x7fa, &val_rl, 1); /* RL */    mas_writemem(MAS_BANK_D1, 0x7fb, &val_rr, 1); /* RR */#endif#endif}#ifdef HAVE_MAS3587F/* This function works by telling the decoder that we have another   crystal frequency than we actually have. It will adjust its internal   parameters and the result is that the audio is played at another pitch.   The pitch value is in tenths of percent.*/void mpeg_set_pitch(int pitch){    unsigned long val;    /* invert pitch value */    pitch = 1000000/pitch;    /* Calculate the new (bogus) frequency */    val = 18432*pitch/1000;        mas_writemem(MAS_BANK_D0,0x7f3,&val,1);    /* We must tell the MAS that the frequency has changed.       This will unfortunately cause a short silence. */    val = 0x25;    mas_writemem(MAS_BANK_D0,0x7f1,&val,1);}#endifvoid mp3_init(int volume, int bass, int treble, int balance, int loudness,     int bass_boost, int avc, int channel_config){#ifdef SIMULATOR    volume = bass = treble = balance = loudness        = bass_boost = avc = channel_config;#else#ifdef HAVE_MAS3507D    unsigned long val;    loudness = bass_boost = avc;#endif    setup_sci0();#ifdef HAVE_MAS3587F    or_b(0x08, &PAIORH); /* output for /PR */    init_playback();        mas_version_code = mas_readver();    DEBUGF("MAS3587 derivate %d, version B%d\n",           (mas_version_code & 0xff00) >> 8, mas_version_code & 0xff);#endif#ifdef HAVE_DAC3550A    dac_init();#endif    #ifdef HAVE_MAS3507D    and_b(~0x20, &PBDRL);    sleep(HZ/5);    or_b(0x20, &PBDRL);    sleep(HZ/5);        /* set IRQ6 to edge detect */    ICR |= 0x02;    /* set IRQ6 prio 8 */    IPRB = ( IPRB & 0xff0f ) | 0x0080;    mas_readmem(MAS_BANK_D1, 0xff7, &mas_version_code, 1);        mas_writereg(0x3b, 0x20); /* Don't ask why. The data sheet doesn't say */    mas_run(1);    sleep(HZ);    /* Clear the upper 12 bits of the 32-bit samples */    mas_writereg(0xc5, 0);    mas_writereg(0xc6, 0);        /* We need to set the PLL for a 14.1318MHz crystal */    if(mas_version_code == 0x0601) /* Version F10? */    {        val = 0x5d9d0;        mas_writemem(MAS_BANK_D0, 0x32d, &val, 1);        val = 0xfffceceb;        mas_writemem(MAS_BANK_D0, 0x32e, &val, 1);        val = 0x0;        mas_writemem(MAS_BANK_D0, 0x32f, &val, 1);        mas_run(0x475);    }    else    {        val = 0x5d9d0;        mas_writemem(MAS_BANK_D0, 0x36d, &val, 1);        val = 0xfffceceb;        mas_writemem(MAS_BANK_D0, 0x36e, &val, 1);        val = 0x0;        mas_writemem(MAS_BANK_D0, 0x36f, &val, 1);        mas_run(0xfcb);    }    #endif#ifdef HAVE_MAS3507D    mas_poll_start(1);    mas_writereg(MAS_REG_KPRESCALE, 0xe9400);    dac_enable(true);    mpeg_sound_channel_config(channel_config);#endif#ifdef HAVE_MAS3587F    ICR &= ~0x0010; /* IRQ3 level sensitive */    PACR1 = (PACR1 & 0x3fff) | 0x4000; /* PA15 is IRQ3 */#endif    /* Must be done before calling mpeg_sound_set() */    mpeg_is_initialized = true;        mpeg_sound_set(SOUND_BASS, bass);    mpeg_sound_set(SOUND_TREBLE, treble);    mpeg_sound_set(SOUND_BALANCE, balance);    mpeg_sound_set(SOUND_VOLUME, volume);    #ifdef HAVE_MAS3587F    mpeg_sound_channel_config(channel_config);    mpeg_sound_set(SOUND_LOUDNESS, loudness);    mpeg_sound_set(SOUND_SUPERBASS, bass_boost);    mpeg_sound_set(SOUND_AVC, avc);#endif#endif /* !SIMULATOR */    playing = false;    paused = true;}/* new functions, to be exported to plugin API */#ifndef SIMULATORvoid mp3_play_init(void){#ifdef HAVE_MAS3587F    init_playback();#endif    playing = false;    paused = true;    callback_for_more = NULL;    mp3_reset_playtime();}void mp3_play_data(unsigned char* start, int size,    void (*get_more)(unsigned char** start, int* size) /* callback fn */){    /* init DMA */    DAR3 = 0x5FFFEC3;    CHCR3 &= ~0x0002; /* Clear interrupt */    CHCR3 = 0x1504; /* Single address destination, TXI0, IE=1 */    DMAOR = 0x0001; /* Enable DMA */        callback_for_more = get_more;    SAR3 = (unsigned int)start;    DTCR3 = size & 0xffff;    playing = true;    paused = true;    CHCR3 |= 0x0001; /* Enable DMA IRQ */#ifdef HAVE_MAS3587F    demand_irq_enable(true);#endif}void mp3_play_pause(bool play){    if (paused && play)    {   /* resume playback */        SCR0 |= 0x80;        paused = false;        playstart_tick = current_tick;    }    else if (!paused && !play)    {   /* stop playback */        SCR0 &= 0x7f;        paused = true;        cumulative_ticks += current_tick - playstart_tick;    }}void mp3_play_stop(void){    playing = false;    mp3_play_pause(false);    CHCR3 &= ~0x0001; /* Disable the DMA interrupt */#ifdef HAVE_MAS3587F    demand_irq_enable(false);#endif}long mp3_get_playtime(void){    if (paused)        return cumulative_ticks;    else        return cumulative_ticks + current_tick - playstart_tick;}void mp3_reset_playtime(void){    cumulative_ticks = 0;    playstart_tick = current_tick;}bool mp3_is_playing(void){    return playing;}#endif /* #ifndef SIMULATOR */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -