I made threading model to play sound for dingoo os native.
Don't use pcm_xxx functions, they are bad for dingoo os native.
Just take a look at that, it's a320_sound.c file :
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "../../lib/ucos2.h"
#include "a320_snd.h"
typedef void waveout_inst;
typedef struct {
unsigned int sample_rate;
unsigned short format;
unsigned char channel;
unsigned char volume;
} waveout_args;
waveout_inst *_audio_music = NULL;
waveout_args tempArgs = { A320SAMPLE_RATE, 16, 1, 20 };
#define TASK_START_PRIO 5
#define TASK_STK_SIZE 1024 // Size of each task's stacks (# of WORDs)
#define NO_TASKS 1 // Number of identical tasks
OS_STK TaskStk[NO_TASKS][TASK_STK_SIZE]; // Tasks stacks
OS_STK TaskStartStk[TASK_STK_SIZE];
char TaskData[NO_TASKS]; // Parameters to pass to each task
static volatile int a320_sound_pausei;
static volatile int a320_audio_done;
static volatile int a320_sound_thread_exit; // Flag to end wiz_sound_thread_play() thread
volatile int a320_curSound;
inline int a320_getCurSound()
{
int curSnd=a320_curSound;
curSnd--; if (curSnd<0) curSnd+=16;
return curSnd;
}
static signed short a320_sound_buf[SNDLENGTH]; // Sound buffer
signed short a320_sound_emu[SNDLENGTH]; // Sound buffer
void a320_sound_pause(int yes) { a320_sound_pausei=yes; }
void a320_new_sound_play(void *none) {
signed short w;
do {
if (a320_sound_pausei == 0) {
int k;
unsigned char * bleh = (unsigned char *) a320_sound_buf;
for (k = 0; k < SNDLENGTH; k++) {
a320_sound_buf[k] = a320_sound_emu[k];
}
waveout_write(_audio_music, a320_sound_buf, SNDLENGTH);
a320_audio_done=1;
}
else
OSTimeDly(1);
} while(a320_sound_thread_exit == 0); // Until the end of the sound thread
a320_sound_thread_exit=0;
OSTaskDel(TASK_START_PRIO);
}
void a320_sound_thread_mute(void)
{
//memset(a320_sound_buf_tot,0,(SNDLENGTH*16*2));
}
void a320_sound_thread_stop(void)
{
a320_sound_pausei = 1;
a320_sound_thread_exit=1;
a320_sound_thread_mute();
}
void a320_sound_wait(void)
{
while (!a320_audio_done)
OSTimeDly(4);
a320_audio_done = 0;
}
void a320_sound_init(void) {
a320_sound_thread_exit=0;
a320_sound_pausei = 1;
a320_curSound = 0;
a320_audio_done=1;
_audio_music = waveout_open(&tempArgs);
a320_sound_thread_mute();
OSTaskCreate(a320_new_sound_play, (void *) 0, (void *)&TaskStartStk[TASK_STK_SIZE - 1], TASK_START_PRIO);
}
void a320_sound_stop(void) {
a320_sound_thread_stop();
while (a320_sound_thread_exit); // to avoid pb with waveout_close -> can hang ??
waveout_close(_audio_music);
}
and that is a320sound.h file :
#ifndef __a320_sound_h__
#define __a320_sound_h__
#define A320SAMPLE_RATE 48000
#define SNDLENGTH 4096
extern signed short a320_sound_emu[SNDLENGTH]; // Sound buffer
extern inline int a320_getCurSound();
extern volatile int curSound;
extern void *a320_sound_bufs[16];
#endif
You put these files in your src/lib directory (or where you want), you start threading model in your main file with
a320_sound_init(); and you stop sound with
a320_sound_stop(); just before exiting your main function.
You fill your buffer in
a320_sound_emu buffer , this is the buffer that is copied in threading function to another buffer.
Also , i modified ucos2.h to have correct threading function prototype :
typedef unsigned int OS_STK;
extern uint8_t OSTaskCreate (void (*task)(void *pd), void *pdata, OS_STK *ptos, uint8_t priority);
Hope it will help you.
edit:
Please use the code tags when pasting chunks of code, not quote tags, thanks
mog