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

📄 player_lib.c

📁 MP3 Cyclone的source code 利用FPGGA實現MP3的功能
💻 C
📖 第 1 页 / 共 3 页
字号:
 *          MP3 file information.                                      *
 *                                                                     *
 * Input:   Pointer to the MP3 file structure.                         *
 * Output:  Initialized MP3 file structure                             * 
 * Returns: Error Code, 0 = Success                                    *
 **********************************************************************/

int NiosIIMP3_FileInit( nios_mp3_file_struct *mp3_files )
{
  int ret_code = 0;
  int i = 0;
  char name[12];
  
  /* Get some memory for the file buffer */
  mp3_files->file_buffer = ( char* )malloc( FILE_BUFFER_SIZE );
  
  /* Init the FAT Filesystem */
  mp3_files->vol_ptr = ( VOLUME_PTR )doFatInit();
  if( mp3_files->vol_ptr == NULL )
  {
    ret_code = NO_FAT_INIT;
    printf( "ERROR: Couldn't initialize FAT\n" );
  }
  else
  {
    mp3_files->fat_file_handle = ( FILE_HNDL_PTR )get_file_handle();
   
    /* Read the number of files that are in the ROOT directory */
    mp3_files->file_count = FAT_GetNumFiles( mp3_files->fat_file_handle );
  
    /* Return with error if no files were found   */
    if ( mp3_files->file_count == 0 )
    {
      ret_code = NO_FILES_FOUND;
    }
    else
    {
      /* Find the names of all MP3 files in the ROOT directory */
      mp3_files->mp3_file_count = 0;
      for( i = 0; i < mp3_files->file_count; i++ )
      {
        FAT_GetFileByIndex( mp3_files->fat_file_handle, i );
        FAT_GetShortName( mp3_files->fat_file_handle, name );
        if( strncmp( name + 8, "MP3", 3 ) == 0 )
          mp3_files->mp3_file_count++;
      }
  
      /*  Return with error if no MP3 files were found */
      if( mp3_files->mp3_file_count == 0 )
      {
        ret_code = NO_MP3_FILES_FOUND;
      }
    
      /* This will cause the player to play file 0 first */
      mp3_files->file_number = 0;
    
      /* Start playing automagically */
      mp3_files->playing = 1;
    }
  }
  return ret_code;
}

/***********************************************************************
 * Function: NiosIIMP3_GetMP3File                                      *
 *                                                                     *
 * Purpose: Gets a file handle for the MP3 file which has the          *
 *          specified index in the file system                         *
 *                                                                     *
 * Input:   Pointer to the MP3 file structure.                         *
 *          File index                                                 *
 * Output:  File handle to specified MP3 file                          * 
 * Returns: Error Code, 0 = Success                                    *
 **********************************************************************/

int NiosIIMP3_GetMP3File( nios_mp3_file_struct *mp3_files, int file_number )
{
  int i;
  int ret_code;
  
  /* If the file number requested is out-of-bounds, wrap it around */
  if ( mp3_files->file_number >= mp3_files->file_count )
    file_number = 0;
  if( mp3_files->file_number < 0 )
    file_number = mp3_files->file_count - 1;
  
  /* 
   * Look at the file number requested. If it's an MP3, great, we're done.
   * If it's not, index through the files until an MP3 is found.
   */
  for( i = 0; i < mp3_files->file_count; i++ )
  {
    FAT_GetFileByIndex( mp3_files->fat_file_handle, file_number );
    FAT_GetShortName( mp3_files->fat_file_handle, mp3_files->filename );
    if( strncmp( mp3_files->filename + 8, "MP3", 3 ) == 0 )
    {
      mp3_files->file_number = file_number;
      mp3_files->file_length = mp3_files->fat_file_handle->dir_entry->dir_file_size;
      mp3_files->file_position = 0;
      break; /* We found an MP3, get outa here. */
    }
    else
      file_number = ( file_number >= mp3_files->file_count - 1 ) ? 
                    ( 0 ) : ( file_number + 1 ); 
  }
  
  /* If every file got checked, it means we failed to find an MP3  */
  if( i == mp3_files->file_count )
    ret_code = NO_MP3_FILES_FOUND;
  else
    ret_code = SUCCESS;
  
  
  return( ret_code );
}

/***********************************************************************
 * Function: NiosIIMP3_ChangePlayState                                 *
 *                                                                     *
 * Purpose: Changes the state of the player, depending on the value of *
 *          buttons_pressed.  Options are play, pause, skip to next    *
 *          song, and skip to previous song.                           *
 *                                                                     *
 * Input:   Pointer to the MP3 file structure.                         *
 *          Data representing what buttons were pressed                *
 * Output:  A new state of playback, depending on the buttons          * 
 * Returns: Result Code, NEW_FILE  = a new file is now being played    *
 *                       SAME_FILE = same file is still being played   *
 **********************************************************************/

int NiosIIMP3_ChangePlayState( nios_mp3_file_struct *mp3_files,
                                alt_u32 buttons )
{
  int ret_code;
  
  switch( buttons )
  {
    case PLAY:
      if( mp3_files->playing != 1 )
      {
        mp3_files->playing = 1;
        PWM_Start( AUDIO_PWM_BASE );
        printf( "Playing..\n" );
      }
      ret_code = SAME_FILE;
      break;

    case PAUSE:
      if( mp3_files->playing != 0 )
      {
        mp3_files->playing = 0;
        PWM_Stop( AUDIO_PWM_BASE );
        printf( "Paused..\n" );
      }
      ret_code = PAUSE;
      break;

    case NEXT:
      PWM_Stop( AUDIO_PWM_BASE );
      NiosIIMP3_NextFile( mp3_files );
      NiosIIMP3_GetMP3File( mp3_files , mp3_files->file_number );
      NiosIIMP3_DisplayNowPlaying( mp3_files );
      ret_code = NEW_FILE;
      break;

    case PREVIOUS:
      PWM_Stop( AUDIO_PWM_BASE );
      NiosIIMP3_PrevFile( mp3_files );
      NiosIIMP3_GetMP3File( mp3_files , mp3_files->file_number );
      NiosIIMP3_DisplayNowPlaying( mp3_files );
      ret_code = NEW_FILE;
      break;
    
    case NO_BUTTON:
      ret_code = DO_NOTHING;

    default:
      ret_code = BAD_BUTTON;
      break;
  }
  return( ret_code );
}

/***********************************************************************
 * Function: NiosIIMP3_DisplayNowPlaying                               *
 *                                                                     *
 * Purpose: Displays the name and length of the currently playing file *
 *                                                                     *
 * Input:   Pointer to the MP3 file structure.                         *
 * Output:  Character output to STDOUT of the filename and file length *
 *          in bytes                                                   * 
 * Returns: void                                                       *
 **********************************************************************/

void NiosIIMP3_DisplayNowPlaying( nios_mp3_file_struct *mp3_files )
{
  /* 
   * Unfortunatly, we dont have the neccessary information here 
   * to display the bitrate and sample rate of the file.  So we
   * just display the filename and filesize.
   */
  printf( "\nPlaying: %12s\n", mp3_files->filename );
  printf( "Filesize: %8d\n", (int)mp3_files->file_length);
//  printf( "Bitrate: %6d\n", mp3_files->bitrate);
//  printf( "Sample rate: %5d\n", mp3_files->samplerate);
  
}

/***********************************************************************
 * Function: NiosIIMP3_CheckButtons                                    *
 *                                                                     *
 * Purpose: Checks the state of the buttons if it's been one           *
 *          "bounce time", since they were last checked. If they've    *
 *          changed, return their current value.  If not, return 0.    *
 *                                                                     *
 * Input:   Pointer to the MP3 file structure.                         *
 * Output:  none                                                       * 
 * Returns: State of the buttons if they've changed. 0 if they havent  *
 **********************************************************************/

int NiosIIMP3_CheckButtons( nios_mp3_file_struct *mp3_files )
{
  static alt_u32 button_data = 0;
  static alt_u32 last_button_data = 0;
  static alt_u32 last_check = 0;
  
  alt_u32 switch_bounce;
  alt_u32 current_time;
  int ret_code = 0;
  
  /* This accounts for about 15ms of bounce */
  switch_bounce = ( alt_ticks_per_second() >> 6 );

  /* If it's been 1 "bounce time", check the buttons */
  current_time = alt_nticks();
  if ( ( current_time - last_check ) >= switch_bounce )
  {
    button_data = NiosIIMP3_ReadButtons( );
    /* If the buttons have changed, do what they say */
    if ( button_data != last_button_data )
    {
      ret_code = NiosIIMP3_ChangePlayState( mp3_files, button_data );
    }
    last_button_data = button_data;
    last_check = current_time;
  }
  
  return( ret_code ); 
}

/***********************************************************************
 * Function: NiosIIMP3_ReadButtons                                     *
 *                                                                     *
 * Purpose: Reads the value of the button PIO                          *

⌨️ 快捷键说明

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