📄 sing.c
字号:
/*
** WASABI-Hot! version 1.2c (Sing unit)
**
**
** -- copyright (c) 2001-2004 by Philips Japan, Ltd. -- All rights reserved --
**
**
** ** This code has been made to check/learn **
** ** the ISP1362/ISP1363 functionalities **
** ** Release 06-Aug-2004 **
**
** OKANO, Akifumi
**
** Computing Segment, Semisonductors Div, Philips Japan Ltd.
** akifumi.okano@philips.com
** +81-3-3740-4668
**
*/
// Iso OUT to addrPtr->EP4
/****************************************************************************/
/* includes */
/****************************************************************************/
#include <stdio.h>
#include <dos.h>
#include <fcntl.h>
#include <string.h>
#include "_hc_core/dev_ep.h"
#include "_hc_core/isr.h"
#include "_hc_core/transfer.h"
#include "_hc_hw/hc_comm.h"
#include "_hc_hw/hw_acces.h"
//#include "_hc_hw/dma.h"
#include "_hc_cls/cls_hndl.h"
#include "class_dr/audio/sing.h"
#include "_otg/otg_demo.h"
#include "general.h"
#include "ui.h"
#include "griffin.h"
#define AUDIO_FROM_STORAGE
#ifdef AUDIO_FROM_STORAGE
#include "class_dr/storage/fs_shell.h"
#include "class_dr/storage/storage.h"
#endif
/****************************************************************************/
/* constants */
/****************************************************************************/
#define AUDIO_DEVICE_NAME_STR_LENGTH 45
#define NUM_OF_AUDIO_COMMANDS 9
#define BUFFER_DEPTH 32 // this value must be power( 2, n )
#define SING_BUFFER_EMPTY 0
#define SING_BUFFER_FULL BUFFER_DEPTH
#define AUDIO_LEVEL_CHANGE_TIMELIMIT 20
#define NO_AUDIO_DEVICE 1
#define NO_AUDIO_FILE 2
#define AUDIO_INITIALIZATION_FAIL 3
#ifdef USE_AUDIO_PEAK_METER
#define DEFAULT_SCALING_LEVEL 14
#endif
/****************************************************************************/
/* macros */
/****************************************************************************/
/****************************************************************************/
/* types */
/****************************************************************************/
typedef short iso_ptd_header;
typedef struct ais
{
unsigned char size;
iso_ptd_header *iso_header_ptr;
}
audio_index_size;
typedef struct _audio_command_vct
{
unsigned char com[ 10 ];
}
audio_command_vct;
/****************************************************************************/
/* global vars */
/****************************************************************************/
char g_audio_device_name[ AUDIO_DEVICE_NAME_STR_LENGTH + 3];
sing_file_info gp_sing_file_strings[ MAX_NUM_OF_AUDIO_FILES ]; // Music file list
char gp_sing_list_file_str[ FILE_NAME_LENGTH ]; // Music list file
audio_command_vct g_audio_start_commands_codec[ NUM_OF_AUDIO_COMMANDS ] = // CODEC Audio device command before play
{
{ 0x01, 0x0B, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 },
{ 0x01, 0x0B, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00 },
{ 0x01, 0x0B, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00 },
{ 0x01, 0x0B, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00 },
{ 0x21, 0x01, 0x01, 0x02, 0x00, 0x09, 0x02, 0x00, 0x00, 0x80 },
{ 0x21, 0x01, 0x02, 0x02, 0x00, 0x09, 0x02, 0x00, 0x00, 0x80 },
{ 0x21, 0x01, 0x00, 0x03, 0x00, 0x0B, 0x01, 0x00, 0x00, },
{ 0x21, 0x01, 0x00, 0x05, 0x00, 0x0B, 0x01, 0x00, 0x00, },
{ 0x01, 0x0B, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00 }
};
audio_command_vct g_audio_start_commands_dac[ NUM_OF_AUDIO_COMMANDS ] = // DAC Audio device command before play
{
{ 0x01, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, //(21)
{ 0x21, 0x01, 0x00, 0x03, 0x00, 0x02, 0x01, 0x00, 0x00 }, //(28)
{ 0x21, 0x01, 0x00, 0x05, 0x00, 0x02, 0x01, 0x00, 0x00 }, //(32)
{ 0x21, 0x01, 0x01, 0x02, 0x00, 0x02, 0x02, 0x00, 0x00, 0xF4 }, //(36)
{ 0x21, 0x01, 0x02, 0x02, 0x00, 0x02, 0x02, 0x00, 0x00, 0xF4 }, //(40)
{ 0x01, 0x0B, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 }, //(49)
{ 0x01, 0x0B, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00 }, //(50)
{ 0x21, 0x01, 0x01, 0x02, 0x00, 0x02, 0x02, 0x00, 0x16, 0xF4 }, //(51)
{ 0x21, 0x01, 0x02, 0x02, 0x00, 0x02, 0x02, 0x00, 0x16, 0xF4 } //(52)
};
audio_command_vct g_audio_start_commands_soundstick[ NUM_OF_AUDIO_COMMANDS ] = // Soundstick initialization
{
{ 0x01, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x21, 0x01, 0x00, 0x03, 0x00, 0x02, 0x01, 0x00, 0x00 },
{ 0x21, 0x01, 0x00, 0x05, 0x00, 0x02, 0x01, 0x00, 0x00 },
// { 0x21, 0x01, 0x01, 0x02, 0x00, 0x02, 0x02, 0x00, 0xC9, 0xF0 },
// { 0x21, 0x01, 0x02, 0x02, 0x00, 0x02, 0x02, 0x00, 0xC9, 0xF0 },
{ 0x21, 0x01, 0x01, 0x02, 0x00, 0x02, 0x02, 0x00, 0xFF, 0xFF },
{ 0x21, 0x01, 0x02, 0x02, 0x00, 0x02, 0x02, 0x00, 0xFF, 0xFF },
{ 0x01, 0x0B, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 },
{ 0x01, 0x0B, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00 },
};
iso_ptd_header g_iso_ptd0[ 4 ] = { 0x0800, 0x4150, 0x04B0, 0x0080 }; // ISO PTD header (default target device : address 2)
iso_ptd_header g_iso_ptd9[ 4 ] = { 0x0800, 0x4950, 0x04B4, 0x0080 }; // ISO PTD header (default target device : address 2)
#ifdef SMALLER_ISO_BUFFER
iso_ptd_header g_iso_ptdL[ 4 ] = { 0x0800, 0x4950, 0x04B0, 0x0080 }; // ISO PTD header (default target device : address 2)
#endif //SMALLER_ISO_BUFFER
audio_index_size g_audio_packet[ 10 ] = // Audio packet parameters
{
{ 176, g_iso_ptd0 },
{ 176, g_iso_ptd0 },
{ 176, g_iso_ptd0 },
{ 176, g_iso_ptd0 },
{ 176, g_iso_ptd0 },
{ 176, g_iso_ptd0 },
{ 176, g_iso_ptd0 },
{ 176, g_iso_ptd0 },
{ 176, g_iso_ptd0 },
{ 180, g_iso_ptd9 }
};
unsigned short (*g_buffer_fill_method)( short *p );
void (*g_stop_process_method)( void );
int g_file_handle = 0; // File handle to access by _dos_open()/_dos_read()/_dos_close()
unsigned char g_audio_source;
unsigned char g_audio_current_target_device_address; // To point the current target aidio device address
unsigned long g_audio_quit_timing; // Flag for play end delay
unsigned short g_iso_buffer_size;
unsigned char g_file_read_buffer_IN = 0;
unsigned char g_file_read_buffer_OUT = 0;
unsigned char g_sing_buffer_EMPTY = True;
#ifdef USE_AUDIO_PEAK_METER
unsigned long g_peak_meter_left[ BUFFER_DEPTH ];
unsigned long g_peak_meter_right[ BUFFER_DEPTH ];
unsigned char g_peak_meter_scaling_level;
#endif
char g_audio_device_addr = 0;
char g_audio_level = 0;
char g_audio_level_changed = 0;
unsigned char g_audio_mute = False;
unsigned long g_total_music_length_in_bytes = 0L;
unsigned long g_total_music_length_in_sec = 0L;
unsigned long g_current_music_time_in_bytes = 0L;
unsigned char g_previous_audio_state = 1;
short *g_audio_buffer = NULL;
short *g_file_read_buffer_ptr[ BUFFER_DEPTH ];
unsigned char g_last_buffer[ BUFFER_DEPTH ];
unsigned char g_audio_abort = False;
unsigned char g_audio_in_play = False;
/****************************************************************************/
/* function prototypes */
/****************************************************************************/
unsigned short open_music_file( char *filename );
void close_music_file( void );
void audio_stop_process( void );
unsigned short sing_buffer_data_read( short *buffer_ptr );
unsigned short audio_command( device_instance *dvi_ptr, unsigned char *com );
unsigned short music_file_read( short *buffer_ptr );
void iso_transfer_service_from_file_by_ISTL( unsigned char buffer_number );
void iso_enable_buffer( unsigned char buffer_number );
void iso_buffer_fill( unsigned char buffer_number, unsigned long frame_count_reference );
short *output_buffer_selection( void );
unsigned char sing_buffer_stored_length( void );
void sing_buffer_pointer_increment( unsigned char *v );
void sing_buffer_reset( void );
void sing_buffer_data_fill( void );
void sing_buffer_volume_control( short *buffer_ptr, unsigned char level );
void audio_status_monitor( unsigned char dummy );
unsigned char get_music_number( void );
/****************************************************************************/
/* function definitions */
/****************************************************************************/
unsigned char audio_main( unsigned char selection )
{
device_instance *dvi_ptr;
if ( is_audio_active() )
{
audio_stop();
audio_stop_process();
mprintf( LIGHTGREEN, CONTINUE, " audio play finished (by user termination).\r\n" );
return ( 0xFF );
}
/* */
/* Audio start message */
/* */
mprintf( LIGHTGREEN, CONTINUE, "\r\n\r\n AudioTest started\r\n" );
/* */
/* Finding USB audio device */
/* */
if ( g_audio_device_addr == 0 )
{
mprintf( LIGHTGREEN, CONTINUE, "\r\n Audio device can not be found.\r\n" );
return ( NO_AUDIO_DEVICE );
}
mprintf( LIGHTGREEN, CONTINUE, " target Audio device address : %d\r\n", g_audio_device_addr );
dvi_ptr = devep_find_device( g_audio_device_addr );
/* */
/* Selection for music file */
/* */
if ( selection == AUDIO_PLAY_MANUAL_SELECTION )
selection = get_music_number();
if ( (selection & 0xF0) == OTG_AUDIO_SPECIAL_CODE )
{
OTG_demo_host_side_2( selection & 0x0F );
return ( 0 );
}
#ifdef AUDIO_FROM_STORAGE
if ( (selection & 0xF0) == STORAGE_AUDIO_SPECIAL_CODE )
{
char s[ 128 ];
ui_get_string_from_console( s, 128, WHITE, " USB storage play, Type path & file name >> ", YELLOW, 0, "/", NULL );
mprintf( LIGHTGREEN, CONTINUE, "\r\n" );
fsts_play( s );
return ( 0 );
}
#endif
if ( !(selection < MAX_NUM_OF_AUDIO_FILES) )
{
mprintf( LIGHTGREEN, CONTINUE, "\r\n Audio file can not be found.\r\n" );
return ( NO_AUDIO_FILE );
}
mprintf( LIGHTGREEN, CONTINUE, " music data source \"%s\" (selection num : %d)\r\n", gp_sing_file_strings[ selection ].file_path_and_name, selection );
/* */
/* Initialize Audio functions */
/* */
if ( 0 != audio_initialize( dvi_ptr, gp_sing_file_strings[ selection ].file_path_and_name, music_file_read, close_music_file ) )
return ( AUDIO_INITIALIZATION_FAIL );
/* */
/* Start to play Audio */
/* */
audio_start();
mprintf( LIGHTGREEN, CONTINUE, " play started." );
mprintf( LIGHTGREEN, CONTINUE, " use keys " );
mprintf( LIGHTRED, CONTINUE, "[,]" );
mprintf( LIGHTGREEN, CONTINUE, " and " );
mprintf( LIGHTRED, CONTINUE, "[.]" );
mprintf( LIGHTGREEN, CONTINUE, " to adjust volume level.\r\n\r\n" );
return ( 0 );
}
unsigned char is_audio_beep_play( void )
{
return ( (g_audio_source == SOURCE_IS_NON_FILE) ? 1 : 0 );
}
unsigned char is_audio_active( void )
{
return ( g_audio_current_target_device_address );
}
unsigned char is_audio_in_play( void )
{
return ( g_audio_in_play );
}
unsigned char is_audio_exist( void )
{
return ( g_audio_buffer ? True : False );
}
unsigned short swap( unsigned short x )
{
unsigned short tmp;
tmp = x << 8;
return ( tmp | (x >> 8) );
}
unsigned short audio_initialize( device_instance *dvi_ptr, char *filename, unsigned short (*buffer_fill_method)( short *p ), void (*stop_process)( void ) )
{
short *buffer;
int i;
if ( NULL == filename )
{
g_audio_source = SOURCE_IS_NON_FILE;
}
else if ( '*' == *filename )
{
g_audio_source = SOURCE_IS_REMOTE_FILE;
}
else
{
g_audio_source = SOURCE_IS_FILE;
if ( open_music_file( filename ) )
return ( 1 );
}
g_audio_current_target_device_address = dvi_ptr->address;
g_buffer_fill_method = buffer_fill_method;
g_stop_process_method = stop_process;
g_iso_buffer_size = read_register16( Com16_HcISTLBufferSize );
#ifdef USE_AUDIO_PEAK_METER
g_peak_meter_scaling_level = DEFAULT_SCALING_LEVEL;
#endif
return ( 0 );
}
unsigned short open_music_file( char *filename )
{
wave_header wave_head;
unsigned int readsz;
/* */
/* Open the music file */
/* */
if ( 0 != (_dos_open( filename, O_RDONLY, &g_file_handle )) )
{
mprintf( LIGHTRED, CONTINUE, "error @ wave file open\r\n" );
return ( 1 );
}
g_total_music_length_in_bytes = 0L;
g_current_music_time_in_bytes = 0L;
/* */
/* Checking "wave" header */
/* */
_dos_read( g_file_handle, &wave_head, sizeof( wave_header ), &readsz );
if ( !strncmp( wave_head.wave_h, "WAVE", 3 ) ) // It may be wave format!
{
if ( wave_head.data_speed != 176400L )
{
mprintf( LIGHTGREEN, CONTINUE, " \".wav file may not WASABI-Hot! compatible format.\"\r\n" );
return ( 2 );
}
else
{
int min,
sec,
total_sec;
g_total_music_length_in_bytes = wave_head.data_sample_size_in_bytes;
g_total_music_length_in_sec = bytes_to_sec( wave_head.data_sample_size_in_bytes );
total_sec = g_total_music_length_in_sec;
min = g_total_music_length_in_sec / 60;
sec = g_total_music_length_in_sec % 60;
mprintf( LIGHTGREEN, CONTINUE, " play time %dmin %02dsec (%dseconds)\"\r\n", min, sec, total_sec );
}
}
else
{
mprintf( LIGHTGREEN, CONTINUE, " The file is not in \".wav\" format. It will play as raw data.\"\r\n" );
// re-open to seek reset
_dos_close( g_file_handle );
g_file_handle = 0;
if ( 0 != (_dos_open( filename, O_RDONLY, &g_file_handle )) )
{
mprintf( LIGHTRED, CONTINUE, "error @ wave file open\r\n" );
return ( 1 );
}
}
return ( 0 );
}
void close_music_file( void )
{
if ( g_file_handle )
_dos_close( g_file_handle );
g_file_handle = 0;
mprintf( LIGHTGRAY, CONTINUE, " audio file closed.\r\n" );
}
unsigned short music_file_read( short *buffer_ptr )
{
unsigned short read_size;
_dos_read( g_file_handle, (unsigned char *)buffer_ptr, (MEMORY_FILLING_SIZE_FROM_FILE << 1), (unsigned *)&read_size );
g_current_music_time_in_bytes += read_size;
return ( read_size >> 1 ); // size convert from bytes to short
}
unsigned char g_seq_play_num;
unsigned char g_num_of_audio_files;
void audio_sequential_play_control( void );
void audio_sequential_play( void )
{
unsigned char key;
if ( NULL == gene_install_asynchronous_periodic_process( 3, NULL ) )
{
mprintf( LIGHTGREEN, CONTINUE, "\r\n Audio jukebox mode started.\r\n" );
for ( g_num_of_audio_files = 0; g_num_of_audio_files < MAX_NUM_OF_AUDIO_FILES; g_num_of_audio_files++ )
if ( !(*gp_sing_file_strings[ g_num_of_audio_files ].file_path_and_name) )
break;
g_seq_play_num = 0;
gene_install_asynchronous_periodic_process( 3, audio_sequential_play_control );
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -