📄 sing.c
字号:
/*
** Kenobi2 version 1.3 (Sing unit)
**
** ** This code has been made to check/learn the 1161 functionalities **
** ** Release 25-Feb-2002 **
**
** OKANO, Akifumi
**
** Computing Segment, Semisonductors Div, Philips Japan Ltd.
** akifumi.okano@philips.com
** +81-3-3740-4668
*/
// Iso OUT to addrPtr->EP4
// 23-Oct-2001 Cleaned
// 31-Oct-2001 _MiniKit conditional compilation added Okano
// 08-Nov-2001 Cleaned again
// 13-Nov-2001 BugFixed for malloc size
// 13-Nov-2001 "ITL_WRITE_MARGIN" value is changed from 8 to 12. Noise was found monitoring with headphones
/****************************************************************************/
/* includes */
/****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
#include <dos.h>
#include <fcntl.h>
#include <string.h>
#include "ui.h"
#include "sing.h"
#include "tr_gene.h"
#include "dev_ep.h"
#include "isr.h"
#include "hw_acces.h"
#include "hc_comm.h"
#include "dma.h"
#include "general.h"
/****************************************************************************/
/* constants */
/****************************************************************************/
#define NUM_OF_AUDIO_COMMANDS 9
#define APPL_BUFFER_FILLING_OFFSET 1
#define PTD_HEADER_SIZE 8
#define ITL_WRITE_MARGIN 12 // This is made to remove noise 13Nov2001
//#define AsyncBuffFill // Do not enable "AsyncBuffFill"
//
// Asynchronus buffer filling will be the cause of the noise while
// another port is doing the enumeration.
//
// Buffer filling and ITL service must have top priority to keep
// playing music.
// DOS does not have such task management mechanism. Then all those
// process need to be done in the ISR invoked by SOF.
//#define DEBUG
/****************************************************************************/
/* types */
/****************************************************************************/
typedef short iso_ptd_header;
typedef struct ais
{
unsigned char size;
iso_ptd_header *iso_header_ptr;
}
audio_index_size;
/****************************************************************************/
/* global vars */
/****************************************************************************/
char g_sing_file_str[ MAX_NUM_OF_AUDIO_FILES ][ FILE_NAME_LENGTH ]; // Music file list
char g_sing_list_file_str[ FILE_NAME_LENGTH ]; // Music list file
unsigned char g_ITL_test_value = ITL_WRITE_MARGIN;
unsigned char g_audio_start_commands[ NUM_OF_AUDIO_COMMANDS ][ 10 ] = // Audio device command before play
{
// { 0x00, 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 }, //(13)
{ 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)
};
iso_ptd_header g_iso_ptd0[ 4 ] = { 0x0800, 0x4950, 0x04B0, 0x0082 }; // ISO PTD header (default target device : address 2)
iso_ptd_header g_iso_ptd9[ 4 ] = { 0x0800, 0x4950, 0x04B4, 0x0082 }; // ISO PTD header (default target device : address 2)
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 }
};
short *g_sample_buffer0_ptr; // Pointer to application buffer 0
short *g_sample_buffer1_ptr; // Pointer to application buffer 1
short *g_audio_buffer_ptr; // Application buffer base
short *g_sample_buffer_end_ptr;
unsigned int (*g_buffer_fill_method)();
int g_file_handle; // File handle to access by _dos_open()/_dos_read()/_dos_close()
unsigned short *g_file_load_target_ptr = NULL; // Not used. Only used in the setting "AsyncBuffFill"
unsigned char g_audio_current_target_device_address; // To point the current target aidio device address
int g_cnt_iso_quit; // Flag for play end delay
unsigned char g_audio_source;
int g_file_access_reserve_timing;
/****************************************************************************/
/* function prototypes */
/****************************************************************************/
unsigned short open_music_file( char *filename );
unsigned int sing_buffer_fill_from_file( unsigned char *buffer_ptr );
unsigned short send_init_commands( device_instance *dvi_ptr );
unsigned short audio_command( device_instance *dvi_ptr, unsigned char *com );
unsigned short swap( unsigned short x );
void iso_transfer_service_from_file_to_ITL( void );
void ITL_fill_from_app_buffer( short *buff_base, unsigned long *offset_ptr, int *counter_ptr );
unsigned char ITL_fill( short *buff, int index );
void app_buffer_fill( int counter );
void fileaccess_reserve( void );
void fileaccess_free( void );
/****************************************************************************/
/* function definitions */
/****************************************************************************/
unsigned char audio_main( void )
{
device_instance *dvi_ptr;
unsigned char selection;;
if ( !is_audio_active() )
{
/* */
/* Audio start message */
/* */
#ifndef _MiniKit
mprintf( LIGHTGREEN, CONTINUE, "\r\n" );
mprintf( LIGHTGREEN, CONTINUE, "\r\n" );
mprintf( LIGHTGREEN, CONTINUE, " AudioTest started\r\n" );
#else
printf("\nAudioTest started\r\n" );
#endif
/* */
/* Finding USB audio device */
/* */
if ( NULL == (dvi_ptr = find_class_interface( AUDIO_CLASS_INTERFACE )) )
{
mprintf( LIGHTGREEN, CONTINUE, "\r\n Audio device can not be found.\r\n" );
return ( 1 );
}
#ifndef _MiniKit
mprintf( LIGHTGREEN, CONTINUE, " target Audio device address : %d\r\n", dvi_ptr->address );
#else
printf( "target Audio device address : %d\r\n", dvi_ptr->address );
#endif
/* */
/* Selection for music file */
/* */
#ifndef _MiniKit
selection = get_music_number();
if ( !(selection < MAX_NUM_OF_AUDIO_FILES) )
{
mprintf( LIGHTGREEN, CONTINUE, "\r\n Audio file can not be found.\r\n" );
return ( 2 );
}
mprintf( LIGHTGREEN, CONTINUE, " music data source \"%s\" (selection num : %d)\r\n", g_sing_file_str[ selection ], selection );
#else
selection = 0;
strcpy( g_sing_file_str[ selection ], "default.wav" );
printf("music data source \"%s\"\r\n", g_sing_file_str );
#endif
/* */
/* Initialize Audio functions */
/* */
if ( 0 != audio_initialize( dvi_ptr, g_sing_file_str[ selection ], sing_buffer_fill_from_file ) )
return ( 3 );
/* */
/* Start to play Audio */
/* */
audio_start();
#ifndef _MiniKit
mprintf( LIGHTGREEN, CONTINUE, " play started.\r\n\r\n" );
#else
printf("audio play started.\r\n\r\n" );
#endif
}
else
{
audio_stop();
#ifndef _MiniKit
mprintf( LIGHTGREEN, CONTINUE, " audio play finished (by user termination).\r\n" );
#else
printf("Audio play finished (by user termination).\r\n" );
#endif
}
return ( 0 );
}
unsigned char is_audio_active( void )
{
return ( g_audio_current_target_device_address );
}
unsigned short send_init_commands( device_instance *dvi_ptr )
{
unsigned short err;
int i;
for ( i = 0; i < NUM_OF_AUDIO_COMMANDS; i++ )
if ( audio_command( dvi_ptr, g_audio_start_commands[ i ] ) )
return ( err );
return ( 0 );
}
unsigned short audio_command( device_instance *dvi_ptr, unsigned char *com )
{
unsigned short request,
wValue,
wIndex,
wLength;
request = swap( *((unsigned short *)(com + 0)) );
wValue = *((unsigned short *)(com + 2));
wIndex = *((unsigned short *)(com + 4));
wLength = *((unsigned short *)(com + 6));
return ( std_request( (com + 8), dvi_ptr, request, wValue, wIndex, &wLength ) );
}
unsigned short swap( unsigned short x )
{
unsigned short tmp;
tmp = x << 8;
return ( tmp | (x >> 8) );
}
void audio_start( void )
{
unsigned short intr;
g_cnt_iso_quit = 0;
install_SOF_service_routine( 0, iso_transfer_service_from_file_to_ITL );
}
void audio_stop( void )
{
install_SOF_service_routine( 0, NULL ); // removing SOF service
if ( g_audio_source == SOURCE_IS_FILE )
{
_dos_close( g_file_handle );
#ifndef _MiniKit
// clrscr();
mprintf( LIGHTGREEN, CONTINUE, "\r\n audio play finished.\r\n" );
#else
printf("\r\naudio play finished.\r\n" );
#endif
}
fileaccess_free();
free ( g_audio_buffer_ptr );
g_buffer_fill_method = NULL;
g_file_load_target_ptr = NULL;
g_audio_current_target_device_address = 0;
}
unsigned short audio_initialize( device_instance *dvi_ptr, char *filename, unsigned int (*buffer_fill_method)() )
{
short *buffer;
int i;
/* */
/* Register curent target device */
/* */
g_audio_current_target_device_address = dvi_ptr->address;
/* */
/* Register buffer filling method */
/* */
g_buffer_fill_method = buffer_fill_method;
/* */
/* Switch for message @ audio stop */
/* */
g_audio_source = (filename == NULL) ? SOURCE_IS_NON_FILE : SOURCE_IS_FILE;
/* */
/* PTD address field set */
/* */
*((unsigned char *)g_iso_ptd0 + 6) = dvi_ptr->address | 0x80; // Set device address in PTD with ISO flag
*((unsigned char *)g_iso_ptd9 + 6) = dvi_ptr->address | 0x80; // Set device address in PTD with ISO flag
/* */
/* Open the music file */
/* */
if ( NULL != filename )
if ( open_music_file( filename ) )
return ( 1 );
/* */
/* Allocating memory */
/* */
if ( NULL == (buffer = (short *)malloc( sizeof( unsigned char ) * (TOTTAL_BUFFER_SIZE__BYTES + PTD_HEADER_SIZE + g_ITL_test_value))) )
{
mprintf( RED, CONTINUE, "error @ audio_initialize malloc, tried to allocate %d bytes. \r\n", ( sizeof( unsigned char ) * (TOTTAL_BUFFER_SIZE__BYTES + 4)) );
return ( 1 );
}
g_audio_buffer_ptr = buffer; // remember to free()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -