examples/fmsynth: Fix freaze when execute it in 2nd time

Fix a freaze BUG when the example is executed again after 1st execution.
This commit is contained in:
SPRESENSE 2024-02-27 14:58:55 +09:00 committed by Xiang Xiao
parent 2d6f5f230e
commit 2b4a25c8d5
4 changed files with 60 additions and 20 deletions

View File

@ -68,7 +68,7 @@ static int configure_audio(int fd, int ch, int fs, int bps, int chmap)
* name: create_audiomq
****************************************************************************/
static mqd_t create_audiomq(int fd, int buf_num)
static mqd_t create_audiomq(const char *mqname, int fd, int buf_num)
{
mqd_t ret;
struct mq_attr attr;
@ -78,8 +78,7 @@ static mqd_t create_audiomq(int fd, int buf_num)
attr.mq_curmsgs = 0;
attr.mq_flags = 0;
ret = mq_open(CONFIG_AUDIOUTILS_NXAUDIO_MSGQNAME,
O_RDWR | O_CREAT, 0644, &attr);
ret = mq_open(mqname, O_RDWR | O_CREAT, 0644, &attr);
if (ret >= (mqd_t)0)
{
int rr;
@ -147,14 +146,13 @@ static void free_audio_buffers(FAR struct nxaudio_s *nxaudio)
void fin_nxaudio(FAR struct nxaudio_s *nxaudio)
{
free_audio_buffers(nxaudio);
ioctl(nxaudio->fd, AUDIOIOC_STOP, 0);
ioctl(nxaudio->fd, AUDIOIOC_SHUTDOWN, 0);
ioctl(nxaudio->fd, AUDIOIOC_UNREGISTERMQ, (unsigned long)nxaudio->mq);
ioctl(nxaudio->fd, AUDIOIOC_RELEASE, 0);
ioctl(nxaudio->fd, AUDIOIOC_SHUTDOWN, 0);
free_audio_buffers(nxaudio);
close(nxaudio->fd);
mq_close(nxaudio->mq);
mq_unlink(CONFIG_AUDIOUTILS_NXAUDIO_MSGQNAME);
close(nxaudio->fd);
}
/****************************************************************************
@ -163,10 +161,23 @@ void fin_nxaudio(FAR struct nxaudio_s *nxaudio)
int init_nxaudio(FAR struct nxaudio_s *nxaudio,
int fs, int bps, int chnum)
{
return init_nxaudio_devname(nxaudio, fs, bps, chnum,
CONFIG_AUDIOUTILS_NXAUDIO_DEVPATH,
CONFIG_AUDIOUTILS_NXAUDIO_MSGQNAME);
}
/****************************************************************************
* name: init_nxaudio_devname
****************************************************************************/
int init_nxaudio_devname(FAR struct nxaudio_s *nxaudio,
int fs, int bps, int chnum,
const char *devname, const char *mqname)
{
struct ap_buffer_info_s buf_info;
nxaudio->fd = open(CONFIG_AUDIOUTILS_NXAUDIO_DEVPATH, O_RDWR | O_CLOEXEC);
nxaudio->fd = open(devname, O_RDWR | O_CLOEXEC);
if (nxaudio->fd >= 0)
{
if (ioctl(nxaudio->fd, AUDIOIOC_RESERVE, 0) < 0)
@ -186,7 +197,7 @@ int init_nxaudio(FAR struct nxaudio_s *nxaudio,
/* Create message queue to communicate with audio driver */
nxaudio->mq = create_audiomq(nxaudio->fd, buf_info.nbuffers + 8);
nxaudio->mq = create_audiomq(mqname, nxaudio->fd, buf_info.nbuffers + 8);
/* Create audio buffers to inject audio sample */
@ -218,6 +229,24 @@ int nxaudio_enqbuffer(FAR struct nxaudio_s *nxaudio,
(unsigned long)(uintptr_t)&desc);
}
/****************************************************************************
* name: nxaudio_pause
****************************************************************************/
int nxaudio_pause(FAR struct nxaudio_s *nxaudio)
{
return ioctl(nxaudio->fd, AUDIOIOC_PAUSE, 0);
}
/****************************************************************************
* name: nxaudio_resume
****************************************************************************/
int nxaudio_resume(FAR struct nxaudio_s *nxaudio)
{
return ioctl(nxaudio->fd, AUDIOIOC_RESUME, 0);
}
/****************************************************************************
* name: nxaudio_setvolume
****************************************************************************/
@ -252,6 +281,8 @@ int nxaudio_stop(FAR struct nxaudio_s *nxaudio)
{
struct audio_msg_s term_msg;
ioctl(nxaudio->fd, AUDIOIOC_STOP, 0);
term_msg.msg_id = AUDIO_MSG_STOP;
term_msg.u.data = 0;
mq_send(nxaudio->mq, (FAR const char *)&term_msg, sizeof(term_msg), 0);

View File

@ -79,7 +79,6 @@ struct key_convert_s
* Private Function Prototypes
****************************************************************************/
extern int board_external_amp_mute_control(bool en);
static void app_dequeue_cb(unsigned long arg,
FAR struct ap_buffer_s *apb);
static void app_complete_cb(unsigned long arg);
@ -91,6 +90,7 @@ static void app_user_cb(unsigned long arg,
****************************************************************************/
static struct kbd_s g_kbd;
static bool g_running = true;
static struct nxaudio_callbacks_s cbs =
{
@ -191,7 +191,10 @@ static void app_dequeue_cb(unsigned long arg,
NULL, 0);
}
nxaudio_enqbuffer(&kbd->nxaudio, apb);
if (g_running)
{
nxaudio_enqbuffer(&kbd->nxaudio, apb);
}
}
/****************************************************************************
@ -373,11 +376,11 @@ int main(int argc, FAR char *argv[])
int i;
int ret;
int key;
bool running = true;
pthread_t pid;
struct app_options appopt;
int key_idx;
g_running = true;
if (configure_option(&appopt, argc, argv) != OK)
{
print_help(argv[0]);
@ -414,7 +417,7 @@ int main(int argc, FAR char *argv[])
printf("Start %s\n", argv[0]);
print_keyusage();
while (running)
while (g_running)
{
key = getchar();
if (key != EOF)
@ -422,7 +425,7 @@ int main(int argc, FAR char *argv[])
switch (key)
{
case 'q':
running = false;
g_running = false;
break;
default:
@ -438,8 +441,6 @@ int main(int argc, FAR char *argv[])
}
}
board_external_amp_mute_control(true);
nxaudio_stop(&g_kbd.nxaudio);
pthread_join(pid, NULL);

View File

@ -98,6 +98,7 @@ static void app_user_cb(unsigned long arg,
****************************************************************************/
static struct mmlplayer_s g_mmlplayer;
static bool g_running = true;
static struct nxaudio_callbacks_s cbs =
{
@ -263,7 +264,11 @@ static void app_dequeue_cb(unsigned long arg,
mmlplayer->nxaudio.chnum,
tick_callback,
(unsigned long)(uintptr_t)mmlplayer);
nxaudio_enqbuffer(&mmlplayer->nxaudio, apb);
if (g_running)
{
nxaudio_enqbuffer(&mmlplayer->nxaudio, apb);
}
}
/****************************************************************************
@ -503,12 +508,12 @@ int main(int argc, FAR char *argv[])
int i;
int ret;
int key;
bool running = true;
pthread_t pid;
struct app_options appopt;
printf("Start %s\n", argv[0]);
g_running = true;
if (configure_option(&appopt, argc, argv) != OK)
{
print_help(argv[0]);
@ -540,7 +545,7 @@ int main(int argc, FAR char *argv[])
pid = create_audio_thread(&g_mmlplayer);
while (running)
while (g_running)
{
key = getchar();
if (key != EOF)
@ -548,7 +553,7 @@ int main(int argc, FAR char *argv[])
switch (key)
{
case 'q':
running = false;
g_running = false;
break;
}
}

View File

@ -63,6 +63,9 @@ extern "C"
int init_nxaudio(FAR struct nxaudio_s *nxaudio,
int fs, int bps, int chnum);
int init_nxaudio_devname(FAR struct nxaudio_s *nxaudio,
int fs, int bps, int chnum,
const char *devname, const char *mqname);
void fin_nxaudio(FAR struct nxaudio_s *nxaudio);
int nxaudio_enqbuffer(FAR struct nxaudio_s *nxaudio,
FAR struct ap_buffer_s *apb);