📄 audiosvr.c
字号:
#include <stdio.h>
#include <string.h>
#include <asixwin.h>
#include <asixapp.h>
#include <sys\lmalloc.h>
#include <resource\bitmap.h>
#include <resource\picture.h>
#include <hardware\drball\audiodrv.h>
#include <service\audio\audiosvr.h>
#include "midi_dec.h"
#include "wavetbl\wavetbl.h"
#include <resource\audio.h>
#define MIDI_STOP_STATUS 0
#define MIDI_RUNNING_STATUS 1
static U8 MIDI_PLAY_STATUS = MIDI_STOP_STATUS;
AUDIO_SCHEME SysAudioScheme =
{
{AUDIO_SYMBOL, MIDI, tiger, 0, 0}, // start
{AUDIO_SYMBOL, NO_AUDIO, NULL, 0, 0}, // exit
{AUDIO_SYMBOL, NO_AUDIO, NULL, 0, 0}, // popup
{AUDIO_SYMBOL, NO_AUDIO, NULL, 0, 0}, // error
{AUDIO_SYMBOL, NO_AUDIO, NULL, 0, 0}, // warning
{AUDIO_SYMBOL, NO_AUDIO, NULL, 0, 0}, // prompt
{AUDIO_SYMBOL, NO_AUDIO, NULL, 0, 0}, // touch
{AUDIO_SYMBOL, NO_AUDIO, NULL, 0, 0}, // select
{AUDIO_SYMBOL, NO_AUDIO, NULL, 0, 0}, // pagedown
{AUDIO_SYMBOL, NO_AUDIO, NULL, 0, 0}, // pageup
};
void audio_svr( void );
//**********************SERVICE TASK DESCRIPTION*********************
DWORD audio_stk[STACK_SIZE];
TASKDESCRIPTION AudioSvrTskDesp =
{"audio_service", ASIX_SERVICE, NOT_IN_ANYGROUP, NULL, STACK_SIZE, 1, PHY_LCD_W, PHY_LCD_H, audio_svr, SERVICE_PRI};
TASKDESCRIPTIONLIST AudioSvrTskDespLst =
{NULL, NULL, &AudioSvrTskDesp, AUDIOSERVER_ID, audio_stk};
//**********************END SERVICE TASK DESCRIPTION****************
//the audio service rutine
void audio_svr( void )
{
MSG msg;
// MSG note_end_msg;
U32 play_time;
U32 note_timer = 0;
AUDIO_INFO *audio_info;
ID note_on_tid;
ID sound_on_tid;
ID MIDI_on_tid;
MIDI_EVENT_STRU *MidiEeventHead;
MIDI_EVENT_STRU *pt_midi_event;
config_PWM( PWM16KHZ ); //config the PWM Playback rate: default is 16khz
while(1)
{
ASIXGetMessage( &msg, NULL, 0, 0 );
switch( msg.message )
{
case ASIX_AUDIO:
audio_info = msg.data; //read the note infomation
switch( msg.wparam )
{
//**********************PLAY SOUND****************************
case SOUND_START:
sound_on_tid = msg.lparam; //read the task id of running audio application
set_play_mode( ISSOUND ); //set the sound type :sound
AUDIO = audio_info->audio_head;
AUDIO_SIZE = audio_info->audio_size;
AUDIO_PT = 0;
open_PWM( );
//open the sound device
Lfree( audio_info );
break;
case SOUND_STOP: //reserved
Lfree( audio_info );
break;
//**********************************************************
//**********************PLAY NOTE****************************
case NOTE_START: //BY_TIME
note_on_tid = msg.lparam; //read the task id of running audio application
if( audio_info->audio_head != NULL )//******is a note infomation
{
set_play_mode( ISNOTE ); //set the sound type :note
set_loop_addr( 4800 ); //set loop address
AUDIO = audio_info->audio_head; //the note information
AUDIO_SIZE = audio_info->audio_size;
play_time = audio_info->play_time;
AUDIO_PT = 0; //clear the note pointer
CreateTimer( ¬e_timer, play_time, NoteEnd, ¬e_on_tid, ALARM_MODE|AUTO_CLEAR_MODE );
StartTimer( note_timer );
open_PWM( ); //open the sound device
}
else //******is a silence infomation
{
CreateTimer( ¬e_timer, play_time, NoteEnd, ¬e_on_tid, ALARM_MODE|AUTO_CLEAR_MODE );
StartTimer( note_timer );
}
Lfree( audio_info );
break;
case NOTE_STOP:
close_PWM();
FreeTimer( note_timer );
Lfree( audio_info );
break;
//*********************************************************
//***********************PLAY MIDI*************************
case MIDI_START:
MIDI_on_tid = msg.lparam; //read the task id of running audio application
MidiEeventHead = midi_decode( (U32)audio_info->audio_head ); //decode the midi file
pt_midi_event = MidiEeventHead;
if( MidiEeventHead != NULL ) //******play the first note
{
NoteSelect( pt_midi_event->note_symbol, 8000 ,pt_midi_event->delta_time );
pt_midi_event = pt_midi_event->next;
}
Lfree( audio_info );
break;
case NOTE_END:
if( pt_midi_event != NULL ) //******play the next note
{
NoteSelect( pt_midi_event->note_symbol, 8000 ,pt_midi_event->delta_time );
pt_midi_event = pt_midi_event->next;
}
else //******midi end
{
MIDIEnd( &MIDI_on_tid ); //sent msg to MIDI player
pt_midi_event = MidiEeventHead; //release the old midi event list
while( pt_midi_event != NULL )
{
MidiEeventHead = MidiEeventHead->next;
Lfree( pt_midi_event );
pt_midi_event = NULL;
pt_midi_event = MidiEeventHead;
}
MidiEeventHead = NULL;
pt_midi_event = NULL;
MIDI_PLAY_STATUS = MIDI_STOP_STATUS;
}
break;
case MIDI_STOP:
FreeTimer( note_timer );
close_PWM();
pt_midi_event = MidiEeventHead; //release the old midi event list
while( pt_midi_event != NULL )
{
MidiEeventHead = MidiEeventHead->next;
Lfree( pt_midi_event );
pt_midi_event = NULL;
pt_midi_event = MidiEeventHead;
}
MidiEeventHead = NULL;
pt_midi_event = NULL;
MIDI_PLAY_STATUS = MIDI_STOP_STATUS;
break;
//********************************************************
default:
break;
}
break;
default:
break;
}
}
}
/**********************the interface of the sound service rutine*********************************/
//play one sound---------determined by sample numbers( SIZE );
//APP sent to SERVICE
void SoundStart( P_U8 sound_head, U32 sound_size )
{
ID sound_on_tid;
MSG msg;
AUDIO_INFO *sound_info;
if( AUDIO_STATUS == STOP_STATUS )
{
AUDIO_STATUS = RUNNING_STATUS; //set the running status
sound_info = (AUDIO_INFO *)Lmalloc( sizeof(AUDIO_INFO) );
if( sound_info == NULL )
return;
sound_info->audio_head = sound_head;
sound_info->audio_size = sound_size;
get_tid( &sound_on_tid );
msg.messageType = ASIX_MESSAGE;
msg.message = ASIX_AUDIO;
msg.lparam = sound_on_tid;
msg.wparam = SOUND_START;
msg.data = sound_info;
SysSendMessage( AUDIOSERVER_ID, &msg );
}
}
//APP sent to SERVICE
void SoundStop( void )
{
AUDIO_STATUS = STOP_STATUS;
close_PWM();
}
//reserved
//SERVICE sent to APP
void SoundEnd( ID sound_on_tid )
{
AUDIO_STATUS = STOP_STATUS;
close_PWM();
}
//play one note---------determined by TIMER;
//APP sent to SERVICE
void NoteStart( P_U8 note_head, U32 note_size, U32 play_time )
{
ID note_on_tid;
MSG msg;
AUDIO_INFO *note_info;
if( AUDIO_STATUS == STOP_STATUS )
{
AUDIO_STATUS = RUNNING_STATUS; //set the running status
note_info = (AUDIO_INFO *)Lmalloc( sizeof(AUDIO_INFO) );
if( note_info == NULL )
return;
note_info->audio_head = note_head;
note_info->audio_size = note_size;
note_info->play_time = play_time;
get_tid( ¬e_on_tid );
msg.messageType = ASIX_MESSAGE;
msg.message = ASIX_AUDIO;
msg.lparam = note_on_tid;
msg.wparam = NOTE_START;
msg.data = note_info;
SysSendMessage( AUDIOSERVER_ID, &msg );
}
}
//APP sent to SERVICE
void NoteStop( void )
{
ID note_off_tid;
MSG msg;
AUDIO_INFO *note_info;
// if( AUDIO_STATUS == RUNNING_STATUS )
// {
AUDIO_STATUS = STOP_STATUS; //set the running status
note_info = (AUDIO_INFO *)Lmalloc( sizeof(AUDIO_INFO) );
if( note_info == NULL )
return;
get_tid( ¬e_off_tid );
msg.messageType = ASIX_MESSAGE;
msg.message = ASIX_AUDIO;
msg.lparam = note_off_tid;
msg.wparam = NOTE_STOP;
msg.data = note_info;
SysSendMessage( AUDIOSERVER_ID, &msg );
// }
}
//SERVICE sent to APP
void NoteEnd( void *p )
{
MSG msg;
ID *note_on_tid = (ID *)p;
close_PWM();
AUDIO_STATUS = STOP_STATUS; //set the stop status
if( note_on_tid == NULL )
return;
msg.messageType = ASIX_MESSAGE;
msg.message = ASIX_AUDIO;
msg.wparam = NOTE_END;
SysSendMessage( *note_on_tid, &msg );
}
void MIDIStart( U32 MIDI_pointer )
{
ID MIDI_on_tid;
MSG msg;
AUDIO_INFO *MIDI_info;
if( AUDIO_STATUS == STOP_STATUS ) // audio status
{
if( MIDI_PLAY_STATUS == MIDI_STOP_STATUS ) // MIDI play status
{
MIDI_PLAY_STATUS = MIDI_RUNNING_STATUS;
MIDI_info = (AUDIO_INFO *)Lmalloc( sizeof(AUDIO_INFO) );
if( MIDI_info == NULL )
return;
MIDI_info->audio_head = (P_U8)MIDI_pointer;
get_tid( &MIDI_on_tid );
msg.messageType = ASIX_MESSAGE;
msg.message = ASIX_AUDIO;
msg.lparam = MIDI_on_tid;
msg.wparam = MIDI_START;
msg.data = MIDI_info;
SysSendMessage( AUDIOSERVER_ID, &msg );
}
}
}
//APP sent to SERVICE
void MIDIStop( void )
{
ID MIDI_off_tid;
MSG msg;
AUDIO_INFO *MIDI_info;
// if( AUDIO_STATUS == RUNNING_STATUS )
// {
AUDIO_STATUS = STOP_STATUS; //set the running status
if( MIDI_PLAY_STATUS == MIDI_RUNNING_STATUS )
{
MIDI_PLAY_STATUS = MIDI_STOP_STATUS;
MIDI_info = (AUDIO_INFO *)Lmalloc( sizeof(AUDIO_INFO) );
if( MIDI_info == NULL )
return;
get_tid( &MIDI_off_tid );
msg.messageType = ASIX_MESSAGE;
msg.message = ASIX_AUDIO;
msg.lparam = MIDI_off_tid;
msg.wparam = MIDI_STOP;
msg.data = MIDI_info;
SysSendMessage( AUDIOSERVER_ID, &msg );
}
// }
}
//SERVICE sent to APP
void MIDIEnd( void *p )
{
MSG msg;
ID *MIDI_on_tid = (ID *)p;
close_PWM();
AUDIO_STATUS = STOP_STATUS; //set the stop status
if( MIDI_on_tid == NULL )
return;
msg.messageType = ASIX_MESSAGE;
msg.message = ASIX_AUDIO;
msg.wparam = MIDI_END;
SysSendMessage( *MIDI_on_tid, &msg );
}
void SetRepeatPoint( U32 loop_addr )
{
if( AUDIO_STATUS == STOP_STATUS )
{
LOOP_PT = loop_addr;
}
}
void NoteSelect( U8 note_symbol, U32 note_size, U32 play_time )
{
/* switch( note_symbol )
{
case 48: NoteStart( c4, 8000 ,play_time ); break;
case 49: NoteStart( c_4, 8000 ,play_time ); break;
case 50: NoteStart( d4, 8000 ,play_time ); break;
case 51: NoteStart( d_4, 8000 ,play_time ); break;
case 52: NoteStart( e4, 8000 ,play_time ); break;
case 53: NoteStart( f4, 8000 ,play_time ); break;
case 54: NoteStart( f_4, 8000 ,play_time ); break;
case 55: NoteStart( g4, 8000 ,play_time ); break;
case 56: NoteStart( g_4, 8000 ,play_time ); break;
case 57: NoteStart( a4, 8000 ,play_time ); break;
case 58: NoteStart( a_4, 8000 ,play_time ); break;
case 59: NoteStart( b4, 8000 ,play_time ); break;
//5 octave
case 60: NoteStart( c5, 8000 ,play_time ); break;
case 61: NoteStart( c_5, 8000 ,play_time ); break;
case 62: NoteStart( d5, 8000 ,play_time ); break;
case 63: NoteStart( d_5, 8000 ,play_time ); break;
case 64: NoteStart( e5, 8000 ,play_time ); break;
case 65: NoteStart( f5, 8000 ,play_time ); break;
case 66: NoteStart( f_5, 8000 ,play_time ); break;
case 67: NoteStart( g5, 8000 ,play_time ); break;
case 68: NoteStart( g_5, 8000 ,play_time ); break;
case 69: NoteStart( a5, 8000 ,play_time ); break;
case 70: NoteStart( a_5, 8000 ,play_time ); break;
case 71: NoteStart( b5, 8000 ,play_time ); break;
//6 octave
case 72: NoteStart( c6, 8000 ,play_time ); break;
case 73: NoteStart( c_6, 8000 ,play_time ); break;
case 74: NoteStart( d6, 8000 ,play_time ); break;
case 75: NoteStart( d_6, 8000 ,play_time ); break;
case 76: NoteStart( e6, 8000 ,play_time ); break;
case 77: NoteStart( f6, 8000 ,play_time ); break;
case 78: NoteStart( f_6, 8000 ,play_time ); break;
case 79: NoteStart( g6, 8000 ,play_time ); break;
case 80: NoteStart( g_6, 8000 ,play_time ); break;
case 81: NoteStart( a6, 8000 ,play_time ); break;
case 82: NoteStart( a_6, 8000 ,play_time ); break;
//case 83: NoteStart( b6, 8000 ,play_time ); break;
default:
{
ID id = GetCurTaskID();
NoteEnd( &id );
}
break; //Assume that there are not other notes
}
*/
if( note_symbol >= 48 || note_symbol <= 83 )
{
NoteStart( WAVE_TABLE(note_symbol-48), 8000 ,play_time );
}
else
{
ID id = GetCurTaskID();
NoteEnd( &id );
}
}
void PlayAudio( AUDIO_INFO *info )
{
if( info == NULL || info->symbol != AUDIO_SYMBOL )
return;
switch( info->type )
{
case WAVE:
SoundStart( info->audio_head, info->audio_size );
break;
case MIDI:
MIDIStart( (U32)info->audio_head );
break;
default:
return;
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -