This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 2b4a25c8d55a15d7363649a5e7d9c8795cf7a18b
Author: SPRESENSE <41312067+sprese...@users.noreply.github.com>
AuthorDate: Tue Feb 27 14:58:55 2024 +0900

    examples/fmsynth: Fix freaze when execute it in 2nd time
    
    Fix a freaze BUG when the example is executed again after 1st execution.
---
 audioutils/nxaudio/nxaudio.c      | 49 ++++++++++++++++++++++++++++++++-------
 examples/fmsynth/keyboard_main.c  | 15 ++++++------
 examples/fmsynth/mmlplayer_main.c | 13 +++++++----
 include/audioutils/nxaudio.h      |  3 +++
 4 files changed, 60 insertions(+), 20 deletions(-)

diff --git a/audioutils/nxaudio/nxaudio.c b/audioutils/nxaudio/nxaudio.c
index 68d709a0b..0964febf8 100644
--- a/audioutils/nxaudio/nxaudio.c
+++ b/audioutils/nxaudio/nxaudio.c
@@ -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);
diff --git a/examples/fmsynth/keyboard_main.c b/examples/fmsynth/keyboard_main.c
index 9c197eb70..1cbaba2a5 100644
--- a/examples/fmsynth/keyboard_main.c
+++ b/examples/fmsynth/keyboard_main.c
@@ -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);
 
diff --git a/examples/fmsynth/mmlplayer_main.c 
b/examples/fmsynth/mmlplayer_main.c
index e24a0021e..88c849c02 100644
--- a/examples/fmsynth/mmlplayer_main.c
+++ b/examples/fmsynth/mmlplayer_main.c
@@ -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;
             }
         }
diff --git a/include/audioutils/nxaudio.h b/include/audioutils/nxaudio.h
index 0d0acb133..519fb2cf4 100644
--- a/include/audioutils/nxaudio.h
+++ b/include/audioutils/nxaudio.h
@@ -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);

Reply via email to