You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2020/10/26 12:57:00 UTC

[GitHub] [incubator-nuttx] GUIDINGLI opened a new pull request #2124: arch/sim: add sim audio support

GUIDINGLI opened a new pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124


   ## Summary
   
   arch/sim: add sim audio support
   
   ## Impact
   
   sim audio will call alsa to playback/capture data
   
   ## Testing
   
   nxplayer
   nxrecoder
   
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] acassis commented on pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
acassis commented on pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#issuecomment-718961981


   > @GUIDINGLI
   > Could you please add a defconfig file so that we can try audio on sim?
   
   In fact, this is a good idea to have a sim:audio example in place.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] btashton commented on a change in pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
btashton commented on a change in pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#discussion_r517333650



##########
File path: arch/sim/src/sim/up_alsa.c
##########
@@ -0,0 +1,731 @@
+/****************************************************************************
+ * arch/sim/src/sim/up_alsa.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/audio/audio.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/nuttx.h>
+
+#include <queue.h>
+
+#include <alsa/asoundlib.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define AUDMIN(a,b)     ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct sim_audio_s
+{
+  struct audio_lowerhalf_s dev;
+  struct dq_queue_s pendq;
+
+  sq_entry_t link;
+
+  bool playback;
+  uint32_t frame_size;
+  uint32_t nbuffers;
+  uint32_t buffer_size;
+
+  uint32_t sample_rate;
+  uint32_t channels;
+  uint32_t bps;
+
+  snd_pcm_t *pcm;
+  snd_mixer_t *mixer;
+  snd_mixer_elem_t *volume;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int sim_audio_getcaps(struct audio_lowerhalf_s *dev, int type,
+                             struct audio_caps_s *caps);
+static int sim_audio_configure(struct audio_lowerhalf_s *dev,
+                               const struct audio_caps_s *caps);
+static int sim_audio_shutdown(struct audio_lowerhalf_s *dev);
+static int sim_audio_start(struct audio_lowerhalf_s *dev);
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+static int sim_audio_stop(struct audio_lowerhalf_s *dev);
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+static int sim_audio_pause(struct audio_lowerhalf_s *dev);
+static int sim_audio_resume(struct audio_lowerhalf_s *dev);
+#endif
+static int sim_audio_enqueuebuffer(struct audio_lowerhalf_s *dev,
+                                   struct ap_buffer_s *apb);
+static int sim_audio_ioctl(struct audio_lowerhalf_s *dev, int cmd,
+                           unsigned long arg);
+static int sim_audio_reserve(struct audio_lowerhalf_s *dev);
+static int sim_audio_release(struct audio_lowerhalf_s *dev);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct audio_ops_s g_sim_audio_ops =
+{
+  .getcaps       = sim_audio_getcaps,
+  .configure     = sim_audio_configure,
+  .shutdown      = sim_audio_shutdown,
+  .start         = sim_audio_start,
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+  .stop          = sim_audio_stop,
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+  .pause         = sim_audio_pause,
+  .resume        = sim_audio_resume,
+#endif
+  .enqueuebuffer = sim_audio_enqueuebuffer,
+  .ioctl         = sim_audio_ioctl,
+  .reserve       = sim_audio_reserve,
+  .release       = sim_audio_release,
+};
+
+static sq_queue_t g_sim_audio;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int sim_audio_config_format(struct sim_audio_s *priv, snd_pcm_t *pcm)
+{
+  snd_pcm_hw_params_t *hw_params;
+  snd_pcm_uframes_t pframes;
+  snd_pcm_format_t format;
+  uint32_t total_size;
+  int ret;
+
+  switch (priv->bps)
+    {
+      case 8:
+        format = SND_PCM_FORMAT_S8;
+        break;
+      case 24:
+        format = SND_PCM_FORMAT_S24;
+        break;
+      case 32:
+        format = SND_PCM_FORMAT_S32;
+        break;
+      default:
+        format = SND_PCM_FORMAT_S16;
+        break;
+    }
+
+  ret = snd_pcm_hw_params_malloc(&hw_params);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = snd_pcm_hw_params_any(pcm, hw_params);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_access(pcm, hw_params,
+                                     SND_PCM_ACCESS_RW_INTERLEAVED);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_format(pcm, hw_params, format);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_rate(pcm, hw_params,
+                                   priv->sample_rate, 0);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_channels(pcm, hw_params,
+                                       priv->channels);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  total_size = priv->nbuffers * priv->buffer_size;
+
+  pframes = priv->buffer_size / priv->frame_size;
+  ret = snd_pcm_hw_params_set_period_size_near(pcm, hw_params,
+                                               &pframes, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  priv->buffer_size = pframes * priv->frame_size;
+  priv->nbuffers    = total_size / priv->buffer_size;
+  ret = snd_pcm_hw_params_set_periods_near(pcm, hw_params,
+                                           &priv->nbuffers, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params(pcm, hw_params);
+
+fail:
+  snd_pcm_hw_params_free(hw_params);
+  return ret;
+}
+
+static int sim_audio_open(struct sim_audio_s *priv)
+{
+  snd_pcm_t *pcm;
+  int direction;
+  int ret;
+
+  if (priv->pcm)
+    {
+      return 0;
+    }
+
+  direction = priv->playback ? SND_PCM_STREAM_PLAYBACK
+                             : SND_PCM_STREAM_CAPTURE;
+
+  ret = snd_pcm_open(&pcm, "default", direction, 0);

Review comment:
       I guess I don't care if we limit to one device, but I want to be able to at least specify it via a config. "default" is certainly the correct default choice. 
   
   My default microphone is a poor choice in this case (I'm doing a loop back)




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] GUIDINGLI commented on pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
GUIDINGLI commented on pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#issuecomment-721485562


   > @GUIDINGLI @xiaoxiang781216
   > I tried this PR with ubuntu18.04 x86_64, but nuttx crashed when playback an WAV file via hostfs.
   > Could you tell me what environment are you using?
   > 
   > ```
   > Thread 1 "nuttx" received signal SIGSEGV, Segmentation fault.                                                                                                                   
   > 0x00005555555714a2 in file_vioctl (filep=0x10000000d8, req=4100, ap=0x7ffff2c8b520) at vfs/fs_ioctl.c:87                                                                        
   > 87        inode = filep->f_inode;                                                                                                                                                                                                                                                                                                                     
   > p filep                                                                                                                                                                         
   > $1 = (struct file *) 0x10000000d8                                                                                                                                               
   > (gdb) p *filep                                                                                                                                                                  
   > Cannot access memory at address 0x10000000d8                                                                                                                                    
   > (gdb) up                                                                                                                                                                        
   > #1  0x0000555555571682 in nx_vioctl (fd=4, req=4100, ap=0x7ffff2c8b520) at vfs/fs_ioctl.c:173                                                                                   
   > 173           ret = file_vioctl(filep, req, ap);                                                                                                                                
   > (gdb) where                                                                                                                                                                     
   > #0  0x00005555555714a2 in file_vioctl (filep=0x10000000d8, req=4100, ap=0x7ffff2c8b520) at vfs/fs_ioctl.c:87                                                                    
   > #1  0x0000555555571682 in nx_vioctl (fd=4, req=4100, ap=0x7ffff2c8b520) at vfs/fs_ioctl.c:173                                                                                   
   > #2  0x000055555557197a in ioctl (fd=4, req=4100) at vfs/fs_ioctl.c:259                                                                                                          
   > #3  0x000055555558f487 in nxplayer_setvolume (pplayer=0x7ffff2bfa0f0, volume=400) at nxplayer.c:1226                                                                            
   > #4  0x000055555558f164 in nxplayer_playthread (pvarg=0x7ffff2bfa0f0) at nxplayer.c:957                                                                                          
   > #5  0x0000555555583970 in pthread_start () at pthread/pthread_create.c:192                                                                                                      
   > #6  0x0000000000000000 in ?? ()     
   > ```
   
   Could you show me your nxplayer cmd line ?
   
   mine :
   nsh> mount -t hostfs -o fs=/home/ligd/platform/FFmpeg/st /data
   nsh> nxplayer
   nxplayer> device /dev/audio/pcm0p
   nxplayer> playraw /data/test2.wav 1 16 48000
   
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] GUIDINGLI edited a comment on pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
GUIDINGLI edited a comment on pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#issuecomment-721485562


   
   > I tried this PR with ubuntu18.04 x86_64, but nuttx crashed when playback an WAV file via hostfs.
   > Could you tell me what environment are you using?
   > 
   > ```
   > Thread 1 "nuttx" received signal SIGSEGV, Segmentation fault.                                                                                                                   
   > 0x00005555555714a2 in file_vioctl (filep=0x10000000d8, req=4100, ap=0x7ffff2c8b520) at vfs/fs_ioctl.c:87                                                                        
   > 87        inode = filep->f_inode;                                                                                                                                                                                                                                                                                                                     
   > p filep                                                                                                                                                                         
   > $1 = (struct file *) 0x10000000d8                                                                                                                                               
   > (gdb) p *filep                                                                                                                                                                  
   > Cannot access memory at address 0x10000000d8                                                                                                                                    
   > (gdb) up                                                                                                                                                                        
   > #1  0x0000555555571682 in nx_vioctl (fd=4, req=4100, ap=0x7ffff2c8b520) at vfs/fs_ioctl.c:173                                                                                   
   > 173           ret = file_vioctl(filep, req, ap);                                                                                                                                
   > (gdb) where                                                                                                                                                                     
   > #0  0x00005555555714a2 in file_vioctl (filep=0x10000000d8, req=4100, ap=0x7ffff2c8b520) at vfs/fs_ioctl.c:87                                                                    
   > #1  0x0000555555571682 in nx_vioctl (fd=4, req=4100, ap=0x7ffff2c8b520) at vfs/fs_ioctl.c:173                                                                                   
   > #2  0x000055555557197a in ioctl (fd=4, req=4100) at vfs/fs_ioctl.c:259                                                                                                          
   > #3  0x000055555558f487 in nxplayer_setvolume (pplayer=0x7ffff2bfa0f0, volume=400) at nxplayer.c:1226                                                                            
   > #4  0x000055555558f164 in nxplayer_playthread (pvarg=0x7ffff2bfa0f0) at nxplayer.c:957                                                                                          
   > #5  0x0000555555583970 in pthread_start () at pthread/pthread_create.c:192                                                                                                      
   > #6  0x0000000000000000 in ?? ()     
   > ```
   
   @masayuki2009 
   
   Could you show me your nxplayer cmd line ?
   
   mine :
   nsh> mount -t hostfs -o fs=/home/ligd/platform/FFmpeg/st /data
   nsh> nxplayer
   nxplayer> device /dev/audio/pcm0p
   nxplayer> playraw /data/test2.wav 1 16 48000
   
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] GUIDINGLI edited a comment on pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
GUIDINGLI edited a comment on pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#issuecomment-721485562


   > @masayuki2009 
   > I tried this PR with ubuntu18.04 x86_64, but nuttx crashed when playback an WAV file via hostfs.
   > Could you tell me what environment are you using?
   > 
   > ```
   > Thread 1 "nuttx" received signal SIGSEGV, Segmentation fault.                                                                                                                   
   > 0x00005555555714a2 in file_vioctl (filep=0x10000000d8, req=4100, ap=0x7ffff2c8b520) at vfs/fs_ioctl.c:87                                                                        
   > 87        inode = filep->f_inode;                                                                                                                                                                                                                                                                                                                     
   > p filep                                                                                                                                                                         
   > $1 = (struct file *) 0x10000000d8                                                                                                                                               
   > (gdb) p *filep                                                                                                                                                                  
   > Cannot access memory at address 0x10000000d8                                                                                                                                    
   > (gdb) up                                                                                                                                                                        
   > #1  0x0000555555571682 in nx_vioctl (fd=4, req=4100, ap=0x7ffff2c8b520) at vfs/fs_ioctl.c:173                                                                                   
   > 173           ret = file_vioctl(filep, req, ap);                                                                                                                                
   > (gdb) where                                                                                                                                                                     
   > #0  0x00005555555714a2 in file_vioctl (filep=0x10000000d8, req=4100, ap=0x7ffff2c8b520) at vfs/fs_ioctl.c:87                                                                    
   > #1  0x0000555555571682 in nx_vioctl (fd=4, req=4100, ap=0x7ffff2c8b520) at vfs/fs_ioctl.c:173                                                                                   
   > #2  0x000055555557197a in ioctl (fd=4, req=4100) at vfs/fs_ioctl.c:259                                                                                                          
   > #3  0x000055555558f487 in nxplayer_setvolume (pplayer=0x7ffff2bfa0f0, volume=400) at nxplayer.c:1226                                                                            
   > #4  0x000055555558f164 in nxplayer_playthread (pvarg=0x7ffff2bfa0f0) at nxplayer.c:957                                                                                          
   > #5  0x0000555555583970 in pthread_start () at pthread/pthread_create.c:192                                                                                                      
   > #6  0x0000000000000000 in ?? ()     
   > ```
   
   Could you show me your nxplayer cmd line ?
   
   mine :
   nsh> mount -t hostfs -o fs=/home/ligd/platform/FFmpeg/st /data
   nsh> nxplayer
   nxplayer> device /dev/audio/pcm0p
   nxplayer> playraw /data/test2.wav 1 16 48000
   
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] acassis commented on pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
acassis commented on pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#issuecomment-721703931


   Hi @masayuki2009 could you please give an Approved from your side? If nobody is against it we can merge it now!


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] btashton merged pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
btashton merged pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] btashton commented on pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
btashton commented on pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#issuecomment-716980852


   I probably won't be able to really look at this until the weekend, but if someone wants to review and merge ahead of that I can look at it in master then.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] acassis commented on pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
acassis commented on pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#issuecomment-721076981


   @btashton could you please verify if everything were fixed?


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] btashton commented on a change in pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
btashton commented on a change in pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#discussion_r517347612



##########
File path: arch/sim/src/sim/up_alsa.c
##########
@@ -0,0 +1,731 @@
+/****************************************************************************
+ * arch/sim/src/sim/up_alsa.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/audio/audio.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/nuttx.h>
+
+#include <queue.h>
+
+#include <alsa/asoundlib.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define AUDMIN(a,b)     ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct sim_audio_s
+{
+  struct audio_lowerhalf_s dev;
+  struct dq_queue_s pendq;
+
+  sq_entry_t link;
+
+  bool playback;
+  uint32_t frame_size;
+  uint32_t nbuffers;
+  uint32_t buffer_size;
+
+  uint32_t sample_rate;
+  uint32_t channels;
+  uint32_t bps;
+
+  snd_pcm_t *pcm;
+  snd_mixer_t *mixer;
+  snd_mixer_elem_t *volume;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int sim_audio_getcaps(struct audio_lowerhalf_s *dev, int type,
+                             struct audio_caps_s *caps);
+static int sim_audio_configure(struct audio_lowerhalf_s *dev,
+                               const struct audio_caps_s *caps);
+static int sim_audio_shutdown(struct audio_lowerhalf_s *dev);
+static int sim_audio_start(struct audio_lowerhalf_s *dev);
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+static int sim_audio_stop(struct audio_lowerhalf_s *dev);
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+static int sim_audio_pause(struct audio_lowerhalf_s *dev);
+static int sim_audio_resume(struct audio_lowerhalf_s *dev);
+#endif
+static int sim_audio_enqueuebuffer(struct audio_lowerhalf_s *dev,
+                                   struct ap_buffer_s *apb);
+static int sim_audio_ioctl(struct audio_lowerhalf_s *dev, int cmd,
+                           unsigned long arg);
+static int sim_audio_reserve(struct audio_lowerhalf_s *dev);
+static int sim_audio_release(struct audio_lowerhalf_s *dev);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct audio_ops_s g_sim_audio_ops =
+{
+  .getcaps       = sim_audio_getcaps,
+  .configure     = sim_audio_configure,
+  .shutdown      = sim_audio_shutdown,
+  .start         = sim_audio_start,
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+  .stop          = sim_audio_stop,
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+  .pause         = sim_audio_pause,
+  .resume        = sim_audio_resume,
+#endif
+  .enqueuebuffer = sim_audio_enqueuebuffer,
+  .ioctl         = sim_audio_ioctl,
+  .reserve       = sim_audio_reserve,
+  .release       = sim_audio_release,
+};
+
+static sq_queue_t g_sim_audio;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int sim_audio_config_format(struct sim_audio_s *priv, snd_pcm_t *pcm)
+{
+  snd_pcm_hw_params_t *hw_params;
+  snd_pcm_uframes_t pframes;
+  snd_pcm_format_t format;
+  uint32_t total_size;
+  int ret;
+
+  switch (priv->bps)
+    {
+      case 8:
+        format = SND_PCM_FORMAT_S8;
+        break;
+      case 24:
+        format = SND_PCM_FORMAT_S24;
+        break;
+      case 32:
+        format = SND_PCM_FORMAT_S32;
+        break;
+      default:
+        format = SND_PCM_FORMAT_S16;
+        break;
+    }
+
+  ret = snd_pcm_hw_params_malloc(&hw_params);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = snd_pcm_hw_params_any(pcm, hw_params);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_access(pcm, hw_params,
+                                     SND_PCM_ACCESS_RW_INTERLEAVED);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_format(pcm, hw_params, format);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_rate(pcm, hw_params,
+                                   priv->sample_rate, 0);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_channels(pcm, hw_params,
+                                       priv->channels);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  total_size = priv->nbuffers * priv->buffer_size;
+
+  pframes = priv->buffer_size / priv->frame_size;
+  ret = snd_pcm_hw_params_set_period_size_near(pcm, hw_params,
+                                               &pframes, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  priv->buffer_size = pframes * priv->frame_size;
+  priv->nbuffers    = total_size / priv->buffer_size;
+  ret = snd_pcm_hw_params_set_periods_near(pcm, hw_params,
+                                           &priv->nbuffers, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params(pcm, hw_params);
+
+fail:
+  snd_pcm_hw_params_free(hw_params);
+  return ret;
+}
+
+static int sim_audio_open(struct sim_audio_s *priv)
+{
+  snd_pcm_t *pcm;
+  int direction;
+  int ret;
+
+  if (priv->pcm)
+    {
+      return 0;
+    }
+
+  direction = priv->playback ? SND_PCM_STREAM_PLAYBACK
+                             : SND_PCM_STREAM_CAPTURE;
+
+  ret = snd_pcm_open(&pcm, "default", direction, 0);

Review comment:
       Just seems trivial to add the two kconfig values here and then not require people to modify their system for testing.  This makes it a lot easier to use an alsa loopback device.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] masayuki2009 commented on pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
masayuki2009 commented on pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#issuecomment-721503976


   >Have you pick another patch for increase stack size ?
   
   @GUIDINGLI 
   Yes, because I checked out your repository and branch.
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] btashton commented on a change in pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
btashton commented on a change in pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#discussion_r517323775



##########
File path: arch/sim/src/sim/up_alsa.c
##########
@@ -0,0 +1,731 @@
+/****************************************************************************
+ * arch/sim/src/sim/up_alsa.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/audio/audio.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/nuttx.h>
+
+#include <queue.h>
+
+#include <alsa/asoundlib.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define AUDMIN(a,b)     ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct sim_audio_s
+{
+  struct audio_lowerhalf_s dev;
+  struct dq_queue_s pendq;
+
+  sq_entry_t link;
+
+  bool playback;
+  uint32_t frame_size;
+  uint32_t nbuffers;
+  uint32_t buffer_size;
+
+  uint32_t sample_rate;
+  uint32_t channels;
+  uint32_t bps;
+
+  snd_pcm_t *pcm;
+  snd_mixer_t *mixer;
+  snd_mixer_elem_t *volume;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int sim_audio_getcaps(struct audio_lowerhalf_s *dev, int type,
+                             struct audio_caps_s *caps);
+static int sim_audio_configure(struct audio_lowerhalf_s *dev,
+                               const struct audio_caps_s *caps);
+static int sim_audio_shutdown(struct audio_lowerhalf_s *dev);
+static int sim_audio_start(struct audio_lowerhalf_s *dev);
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+static int sim_audio_stop(struct audio_lowerhalf_s *dev);
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+static int sim_audio_pause(struct audio_lowerhalf_s *dev);
+static int sim_audio_resume(struct audio_lowerhalf_s *dev);
+#endif
+static int sim_audio_enqueuebuffer(struct audio_lowerhalf_s *dev,
+                                   struct ap_buffer_s *apb);
+static int sim_audio_ioctl(struct audio_lowerhalf_s *dev, int cmd,
+                           unsigned long arg);
+static int sim_audio_reserve(struct audio_lowerhalf_s *dev);
+static int sim_audio_release(struct audio_lowerhalf_s *dev);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct audio_ops_s g_sim_audio_ops =
+{
+  .getcaps       = sim_audio_getcaps,
+  .configure     = sim_audio_configure,
+  .shutdown      = sim_audio_shutdown,
+  .start         = sim_audio_start,
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+  .stop          = sim_audio_stop,
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+  .pause         = sim_audio_pause,
+  .resume        = sim_audio_resume,
+#endif
+  .enqueuebuffer = sim_audio_enqueuebuffer,
+  .ioctl         = sim_audio_ioctl,
+  .reserve       = sim_audio_reserve,
+  .release       = sim_audio_release,
+};
+
+static sq_queue_t g_sim_audio;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int sim_audio_config_format(struct sim_audio_s *priv, snd_pcm_t *pcm)
+{
+  snd_pcm_hw_params_t *hw_params;
+  snd_pcm_uframes_t pframes;
+  snd_pcm_format_t format;
+  uint32_t total_size;
+  int ret;
+
+  switch (priv->bps)
+    {
+      case 8:
+        format = SND_PCM_FORMAT_S8;
+        break;
+      case 24:
+        format = SND_PCM_FORMAT_S24;
+        break;
+      case 32:
+        format = SND_PCM_FORMAT_S32;
+        break;
+      default:
+        format = SND_PCM_FORMAT_S16;
+        break;
+    }
+
+  ret = snd_pcm_hw_params_malloc(&hw_params);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = snd_pcm_hw_params_any(pcm, hw_params);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_access(pcm, hw_params,
+                                     SND_PCM_ACCESS_RW_INTERLEAVED);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_format(pcm, hw_params, format);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_rate(pcm, hw_params,
+                                   priv->sample_rate, 0);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_channels(pcm, hw_params,
+                                       priv->channels);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  total_size = priv->nbuffers * priv->buffer_size;
+
+  pframes = priv->buffer_size / priv->frame_size;
+  ret = snd_pcm_hw_params_set_period_size_near(pcm, hw_params,
+                                               &pframes, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  priv->buffer_size = pframes * priv->frame_size;
+  priv->nbuffers    = total_size / priv->buffer_size;
+  ret = snd_pcm_hw_params_set_periods_near(pcm, hw_params,
+                                           &priv->nbuffers, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params(pcm, hw_params);
+
+fail:
+  snd_pcm_hw_params_free(hw_params);
+  return ret;
+}
+
+static int sim_audio_open(struct sim_audio_s *priv)
+{
+  snd_pcm_t *pcm;
+  int direction;
+  int ret;
+
+  if (priv->pcm)
+    {
+      return 0;
+    }
+
+  direction = priv->playback ? SND_PCM_STREAM_PLAYBACK
+                             : SND_PCM_STREAM_CAPTURE;
+
+  ret = snd_pcm_open(&pcm, "default", direction, 0);

Review comment:
       Ah I guess this is actually where my comment should be. Don't we want to specify this?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] masayuki2009 commented on pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
masayuki2009 commented on pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#issuecomment-716873193


   @GUIDINGLI 
   Could you please add a defconfig file so that we can try audio on sim?
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] masayuki2009 commented on pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
masayuki2009 commented on pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#issuecomment-721597269


   > @xiaoxiang781216 I went ahead and just added it. Note I only included `libasound2-dev libasound2-dev:i386` as it is all that should be required for building. Adding pulseaudio pulls in a lot of deps so I hope that we can avoid that so we only add less than 10MB instead of 200MB+
   > 
   > @GUIDINGLI The config name I reserved is `alsa` so if you use that the CI should work for you.
   
   @btashton @xiaoxiang781216 @GUIDINGLI 
   I only installed libasound2-dev onto my ubuntu18.04 x86_64 machine.
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#discussion_r517329885



##########
File path: arch/sim/src/sim/up_alsa.c
##########
@@ -0,0 +1,731 @@
+/****************************************************************************
+ * arch/sim/src/sim/up_alsa.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/audio/audio.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/nuttx.h>
+
+#include <queue.h>
+
+#include <alsa/asoundlib.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define AUDMIN(a,b)     ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct sim_audio_s
+{
+  struct audio_lowerhalf_s dev;
+  struct dq_queue_s pendq;
+
+  sq_entry_t link;
+
+  bool playback;
+  uint32_t frame_size;
+  uint32_t nbuffers;
+  uint32_t buffer_size;
+
+  uint32_t sample_rate;
+  uint32_t channels;
+  uint32_t bps;
+
+  snd_pcm_t *pcm;
+  snd_mixer_t *mixer;
+  snd_mixer_elem_t *volume;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int sim_audio_getcaps(struct audio_lowerhalf_s *dev, int type,
+                             struct audio_caps_s *caps);
+static int sim_audio_configure(struct audio_lowerhalf_s *dev,
+                               const struct audio_caps_s *caps);
+static int sim_audio_shutdown(struct audio_lowerhalf_s *dev);
+static int sim_audio_start(struct audio_lowerhalf_s *dev);
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+static int sim_audio_stop(struct audio_lowerhalf_s *dev);
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+static int sim_audio_pause(struct audio_lowerhalf_s *dev);
+static int sim_audio_resume(struct audio_lowerhalf_s *dev);
+#endif
+static int sim_audio_enqueuebuffer(struct audio_lowerhalf_s *dev,
+                                   struct ap_buffer_s *apb);
+static int sim_audio_ioctl(struct audio_lowerhalf_s *dev, int cmd,
+                           unsigned long arg);
+static int sim_audio_reserve(struct audio_lowerhalf_s *dev);
+static int sim_audio_release(struct audio_lowerhalf_s *dev);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct audio_ops_s g_sim_audio_ops =
+{
+  .getcaps       = sim_audio_getcaps,
+  .configure     = sim_audio_configure,
+  .shutdown      = sim_audio_shutdown,
+  .start         = sim_audio_start,
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+  .stop          = sim_audio_stop,
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+  .pause         = sim_audio_pause,
+  .resume        = sim_audio_resume,
+#endif
+  .enqueuebuffer = sim_audio_enqueuebuffer,
+  .ioctl         = sim_audio_ioctl,
+  .reserve       = sim_audio_reserve,
+  .release       = sim_audio_release,
+};
+
+static sq_queue_t g_sim_audio;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int sim_audio_config_format(struct sim_audio_s *priv, snd_pcm_t *pcm)
+{
+  snd_pcm_hw_params_t *hw_params;
+  snd_pcm_uframes_t pframes;
+  snd_pcm_format_t format;
+  uint32_t total_size;
+  int ret;
+
+  switch (priv->bps)
+    {
+      case 8:
+        format = SND_PCM_FORMAT_S8;
+        break;
+      case 24:
+        format = SND_PCM_FORMAT_S24;
+        break;
+      case 32:
+        format = SND_PCM_FORMAT_S32;
+        break;
+      default:
+        format = SND_PCM_FORMAT_S16;
+        break;
+    }
+
+  ret = snd_pcm_hw_params_malloc(&hw_params);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = snd_pcm_hw_params_any(pcm, hw_params);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_access(pcm, hw_params,
+                                     SND_PCM_ACCESS_RW_INTERLEAVED);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_format(pcm, hw_params, format);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_rate(pcm, hw_params,
+                                   priv->sample_rate, 0);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_channels(pcm, hw_params,
+                                       priv->channels);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  total_size = priv->nbuffers * priv->buffer_size;
+
+  pframes = priv->buffer_size / priv->frame_size;
+  ret = snd_pcm_hw_params_set_period_size_near(pcm, hw_params,
+                                               &pframes, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  priv->buffer_size = pframes * priv->frame_size;
+  priv->nbuffers    = total_size / priv->buffer_size;
+  ret = snd_pcm_hw_params_set_periods_near(pcm, hw_params,
+                                           &priv->nbuffers, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params(pcm, hw_params);
+
+fail:
+  snd_pcm_hw_params_free(hw_params);
+  return ret;
+}
+
+static int sim_audio_open(struct sim_audio_s *priv)
+{
+  snd_pcm_t *pcm;
+  int direction;
+  int ret;
+
+  if (priv->pcm)
+    {
+      return 0;
+    }
+
+  direction = priv->playback ? SND_PCM_STREAM_PLAYBACK
+                             : SND_PCM_STREAM_CAPTURE;
+
+  ret = snd_pcm_open(&pcm, "default", direction, 0);

Review comment:
       Do you want to change the device name? Ok, it depends on how many pcm instances we want to create. only one playback/capture is created in the current design, "default" should be the best choice in this case. On the other hand, sim_audio_initialize need accept name argument and open the different sound device if we want to simulate multiple sound devices. But I think it's better to create a new patch to change both the alsa driver and board initialization for the mulitple devices. Do you think so? 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] btashton commented on a change in pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
btashton commented on a change in pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#discussion_r512417078



##########
File path: arch/sim/src/Makefile
##########
@@ -202,6 +202,11 @@ ifeq ($(CONFIG_RPTUN),y)
   CSRCS += up_rptun.c
 endif
 
+ifeq ($(CONFIG_AUDIO),y)

Review comment:
       I think this should be replaced with something like `CONFIG_SIM_ALSA` that way we can support other methods of supporting sim audio in the future.

##########
File path: arch/sim/src/sim/up_audio.c
##########
@@ -0,0 +1,590 @@
+/****************************************************************************
+ * arch/sim/src/sim/up_audio.c

Review comment:
       For the same reason lets make this ALSA specific filename along with the function/struct names.  It would be nice to support macOS in the future for audio.

##########
File path: arch/sim/src/sim/up_initialize.c
##########
@@ -281,4 +282,9 @@ void up_initialize(void)
 #if defined(CONFIG_FS_SMARTFS) && (defined(CONFIG_SIM_SPIFLASH) || defined(CONFIG_SIM_QSPIFLASH))
   up_init_smartfs();
 #endif
+
+#ifdef CONFIG_AUDIO
+  audio_register("pcm0p", sim_audio_initialize(true));

Review comment:
       Lets set these via `CONFIG_SIM_ALSA_DEVICE`




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] btashton commented on a change in pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
btashton commented on a change in pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#discussion_r517349934



##########
File path: arch/sim/src/sim/up_alsa.c
##########
@@ -0,0 +1,731 @@
+/****************************************************************************
+ * arch/sim/src/sim/up_alsa.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/audio/audio.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/nuttx.h>
+
+#include <queue.h>
+
+#include <alsa/asoundlib.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define AUDMIN(a,b)     ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct sim_audio_s
+{
+  struct audio_lowerhalf_s dev;
+  struct dq_queue_s pendq;
+
+  sq_entry_t link;
+
+  bool playback;
+  uint32_t frame_size;
+  uint32_t nbuffers;
+  uint32_t buffer_size;
+
+  uint32_t sample_rate;
+  uint32_t channels;
+  uint32_t bps;
+
+  snd_pcm_t *pcm;
+  snd_mixer_t *mixer;
+  snd_mixer_elem_t *volume;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int sim_audio_getcaps(struct audio_lowerhalf_s *dev, int type,
+                             struct audio_caps_s *caps);
+static int sim_audio_configure(struct audio_lowerhalf_s *dev,
+                               const struct audio_caps_s *caps);
+static int sim_audio_shutdown(struct audio_lowerhalf_s *dev);
+static int sim_audio_start(struct audio_lowerhalf_s *dev);
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+static int sim_audio_stop(struct audio_lowerhalf_s *dev);
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+static int sim_audio_pause(struct audio_lowerhalf_s *dev);
+static int sim_audio_resume(struct audio_lowerhalf_s *dev);
+#endif
+static int sim_audio_enqueuebuffer(struct audio_lowerhalf_s *dev,
+                                   struct ap_buffer_s *apb);
+static int sim_audio_ioctl(struct audio_lowerhalf_s *dev, int cmd,
+                           unsigned long arg);
+static int sim_audio_reserve(struct audio_lowerhalf_s *dev);
+static int sim_audio_release(struct audio_lowerhalf_s *dev);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct audio_ops_s g_sim_audio_ops =
+{
+  .getcaps       = sim_audio_getcaps,
+  .configure     = sim_audio_configure,
+  .shutdown      = sim_audio_shutdown,
+  .start         = sim_audio_start,
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+  .stop          = sim_audio_stop,
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+  .pause         = sim_audio_pause,
+  .resume        = sim_audio_resume,
+#endif
+  .enqueuebuffer = sim_audio_enqueuebuffer,
+  .ioctl         = sim_audio_ioctl,
+  .reserve       = sim_audio_reserve,
+  .release       = sim_audio_release,
+};
+
+static sq_queue_t g_sim_audio;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int sim_audio_config_format(struct sim_audio_s *priv, snd_pcm_t *pcm)
+{
+  snd_pcm_hw_params_t *hw_params;
+  snd_pcm_uframes_t pframes;
+  snd_pcm_format_t format;
+  uint32_t total_size;
+  int ret;
+
+  switch (priv->bps)
+    {
+      case 8:
+        format = SND_PCM_FORMAT_S8;
+        break;
+      case 24:
+        format = SND_PCM_FORMAT_S24;
+        break;
+      case 32:
+        format = SND_PCM_FORMAT_S32;
+        break;
+      default:
+        format = SND_PCM_FORMAT_S16;
+        break;
+    }
+
+  ret = snd_pcm_hw_params_malloc(&hw_params);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = snd_pcm_hw_params_any(pcm, hw_params);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_access(pcm, hw_params,
+                                     SND_PCM_ACCESS_RW_INTERLEAVED);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_format(pcm, hw_params, format);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_rate(pcm, hw_params,
+                                   priv->sample_rate, 0);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_channels(pcm, hw_params,
+                                       priv->channels);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  total_size = priv->nbuffers * priv->buffer_size;
+
+  pframes = priv->buffer_size / priv->frame_size;
+  ret = snd_pcm_hw_params_set_period_size_near(pcm, hw_params,
+                                               &pframes, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  priv->buffer_size = pframes * priv->frame_size;
+  priv->nbuffers    = total_size / priv->buffer_size;
+  ret = snd_pcm_hw_params_set_periods_near(pcm, hw_params,
+                                           &priv->nbuffers, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params(pcm, hw_params);
+
+fail:
+  snd_pcm_hw_params_free(hw_params);
+  return ret;
+}
+
+static int sim_audio_open(struct sim_audio_s *priv)
+{
+  snd_pcm_t *pcm;
+  int direction;
+  int ret;
+
+  if (priv->pcm)
+    {
+      return 0;
+    }
+
+  direction = priv->playback ? SND_PCM_STREAM_PLAYBACK
+                             : SND_PCM_STREAM_CAPTURE;
+
+  ret = snd_pcm_open(&pcm, "default", direction, 0);

Review comment:
       If you want to still merge fine, it just does not align with other sim hardware where the user can specify how it connects to to the host hardware. 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] btashton commented on a change in pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
btashton commented on a change in pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#discussion_r517333650



##########
File path: arch/sim/src/sim/up_alsa.c
##########
@@ -0,0 +1,731 @@
+/****************************************************************************
+ * arch/sim/src/sim/up_alsa.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/audio/audio.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/nuttx.h>
+
+#include <queue.h>
+
+#include <alsa/asoundlib.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define AUDMIN(a,b)     ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct sim_audio_s
+{
+  struct audio_lowerhalf_s dev;
+  struct dq_queue_s pendq;
+
+  sq_entry_t link;
+
+  bool playback;
+  uint32_t frame_size;
+  uint32_t nbuffers;
+  uint32_t buffer_size;
+
+  uint32_t sample_rate;
+  uint32_t channels;
+  uint32_t bps;
+
+  snd_pcm_t *pcm;
+  snd_mixer_t *mixer;
+  snd_mixer_elem_t *volume;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int sim_audio_getcaps(struct audio_lowerhalf_s *dev, int type,
+                             struct audio_caps_s *caps);
+static int sim_audio_configure(struct audio_lowerhalf_s *dev,
+                               const struct audio_caps_s *caps);
+static int sim_audio_shutdown(struct audio_lowerhalf_s *dev);
+static int sim_audio_start(struct audio_lowerhalf_s *dev);
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+static int sim_audio_stop(struct audio_lowerhalf_s *dev);
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+static int sim_audio_pause(struct audio_lowerhalf_s *dev);
+static int sim_audio_resume(struct audio_lowerhalf_s *dev);
+#endif
+static int sim_audio_enqueuebuffer(struct audio_lowerhalf_s *dev,
+                                   struct ap_buffer_s *apb);
+static int sim_audio_ioctl(struct audio_lowerhalf_s *dev, int cmd,
+                           unsigned long arg);
+static int sim_audio_reserve(struct audio_lowerhalf_s *dev);
+static int sim_audio_release(struct audio_lowerhalf_s *dev);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct audio_ops_s g_sim_audio_ops =
+{
+  .getcaps       = sim_audio_getcaps,
+  .configure     = sim_audio_configure,
+  .shutdown      = sim_audio_shutdown,
+  .start         = sim_audio_start,
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+  .stop          = sim_audio_stop,
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+  .pause         = sim_audio_pause,
+  .resume        = sim_audio_resume,
+#endif
+  .enqueuebuffer = sim_audio_enqueuebuffer,
+  .ioctl         = sim_audio_ioctl,
+  .reserve       = sim_audio_reserve,
+  .release       = sim_audio_release,
+};
+
+static sq_queue_t g_sim_audio;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int sim_audio_config_format(struct sim_audio_s *priv, snd_pcm_t *pcm)
+{
+  snd_pcm_hw_params_t *hw_params;
+  snd_pcm_uframes_t pframes;
+  snd_pcm_format_t format;
+  uint32_t total_size;
+  int ret;
+
+  switch (priv->bps)
+    {
+      case 8:
+        format = SND_PCM_FORMAT_S8;
+        break;
+      case 24:
+        format = SND_PCM_FORMAT_S24;
+        break;
+      case 32:
+        format = SND_PCM_FORMAT_S32;
+        break;
+      default:
+        format = SND_PCM_FORMAT_S16;
+        break;
+    }
+
+  ret = snd_pcm_hw_params_malloc(&hw_params);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = snd_pcm_hw_params_any(pcm, hw_params);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_access(pcm, hw_params,
+                                     SND_PCM_ACCESS_RW_INTERLEAVED);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_format(pcm, hw_params, format);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_rate(pcm, hw_params,
+                                   priv->sample_rate, 0);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_channels(pcm, hw_params,
+                                       priv->channels);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  total_size = priv->nbuffers * priv->buffer_size;
+
+  pframes = priv->buffer_size / priv->frame_size;
+  ret = snd_pcm_hw_params_set_period_size_near(pcm, hw_params,
+                                               &pframes, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  priv->buffer_size = pframes * priv->frame_size;
+  priv->nbuffers    = total_size / priv->buffer_size;
+  ret = snd_pcm_hw_params_set_periods_near(pcm, hw_params,
+                                           &priv->nbuffers, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params(pcm, hw_params);
+
+fail:
+  snd_pcm_hw_params_free(hw_params);
+  return ret;
+}
+
+static int sim_audio_open(struct sim_audio_s *priv)
+{
+  snd_pcm_t *pcm;
+  int direction;
+  int ret;
+
+  if (priv->pcm)
+    {
+      return 0;
+    }
+
+  direction = priv->playback ? SND_PCM_STREAM_PLAYBACK
+                             : SND_PCM_STREAM_CAPTURE;
+
+  ret = snd_pcm_open(&pcm, "default", direction, 0);

Review comment:
       I guess I don't care if we limit to one device, but I want to be able to at least specify it via a config. "default" is certainly the correct default choice. 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#discussion_r516796776



##########
File path: arch/sim/src/sim/up_internal.h
##########
@@ -397,6 +397,13 @@ int bthcisock_register(int dev_id);
 int bthcisock_loop(void);
 #endif
 
+/* up_audio.c ***************************************************************/
+
+#ifdef CONFIG_AUDIO

Review comment:
       should change to CONFIG_SIM_SOUND




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] masayuki2009 commented on pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
masayuki2009 commented on pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#issuecomment-721493333


   > > I tried this PR with ubuntu18.04 x86_64, but nuttx crashed when playback an WAV file via hostfs.
   > > Could you tell me what environment are you using?
   > > ```
   > > Thread 1 "nuttx" received signal SIGSEGV, Segmentation fault.                                                                                                                   
   > > 0x00005555555714a2 in file_vioctl (filep=0x10000000d8, req=4100, ap=0x7ffff2c8b520) at vfs/fs_ioctl.c:87                                                                        
   > > 87        inode = filep->f_inode;                                                                                                                                                                                                                                                                                                                     
   > > p filep                                                                                                                                                                         
   > > $1 = (struct file *) 0x10000000d8                                                                                                                                               
   > > (gdb) p *filep                                                                                                                                                                  
   > > Cannot access memory at address 0x10000000d8                                                                                                                                    
   > > (gdb) up                                                                                                                                                                        
   > > #1  0x0000555555571682 in nx_vioctl (fd=4, req=4100, ap=0x7ffff2c8b520) at vfs/fs_ioctl.c:173                                                                                   
   > > 173           ret = file_vioctl(filep, req, ap);                                                                                                                                
   > > (gdb) where                                                                                                                                                                     
   > > #0  0x00005555555714a2 in file_vioctl (filep=0x10000000d8, req=4100, ap=0x7ffff2c8b520) at vfs/fs_ioctl.c:87                                                                    
   > > #1  0x0000555555571682 in nx_vioctl (fd=4, req=4100, ap=0x7ffff2c8b520) at vfs/fs_ioctl.c:173                                                                                   
   > > #2  0x000055555557197a in ioctl (fd=4, req=4100) at vfs/fs_ioctl.c:259                                                                                                          
   > > #3  0x000055555558f487 in nxplayer_setvolume (pplayer=0x7ffff2bfa0f0, volume=400) at nxplayer.c:1226                                                                            
   > > #4  0x000055555558f164 in nxplayer_playthread (pvarg=0x7ffff2bfa0f0) at nxplayer.c:957                                                                                          
   > > #5  0x0000555555583970 in pthread_start () at pthread/pthread_create.c:192                                                                                                      
   > > #6  0x0000000000000000 in ?? ()     
   > > ```
   > 
   > @masayuki2009
   > 
   > Could you show me your nxplayer cmd line ?
   > 
   > mine :
   > nsh> mount -t hostfs -o fs=/home/ligd/platform/FFmpeg/st /data
   > nsh> nxplayer
   > nxplayer> device /dev/audio/pcm0p
   > nxplayer> playraw /data/test2.wav 1 16 48000
   
   @GUIDINGLI 
   The followings are actual sequences.
   
   ```
   NuttShell (NSH) NuttX-8.2.0                                                                                                                               
   nsh> mount -t hostfs -o fs=/home/ishikawa/nuttx_root /host                                                                                                
   mount -t hostfs -o fs=/home/ishikawa/nuttx_root /host                                                                                                     
   nsh> ls -l /host                                                                                                                                          
   ls -l /host                                                                                                                                               
   /host:                                                                                                                                                    
    drwxrwxr-x    4096 ..                                                                                                                                    
    -rw-rw-r--34108636 sample1.wav                                                                                                                           
    drwxrwxr-x    4096 .                                                                                                                                     
   nsh> ls -l /dev/audio                                                                                                                                     
   ls -l /dev/audio                                                                                                                                          
   /dev/audio:                                                                                                                                               
    crw-rw-rw-       0 pcm0c                                                                                                                                 
    crw-rw-rw-       0 pcm0p                                                                                                                                 
   nsh> nxplayer                                                                                                                                             
   nxplayer                                                                                                                                                  
   NxPlayer version 1.05                                                                                                                                     
   h for commands, q to exit                                                                                                                                 
                                                                                                                                                             
   nxplayer> device /dev/audio/pcm0p                                                                                                                         
   device /dev/audio/pcm0p                                                                                                                                   
   nxplayer> playraw /host/sample1.wav 2 16 44100  
   ```
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#discussion_r517342055



##########
File path: arch/sim/src/sim/up_alsa.c
##########
@@ -0,0 +1,731 @@
+/****************************************************************************
+ * arch/sim/src/sim/up_alsa.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/audio/audio.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/nuttx.h>
+
+#include <queue.h>
+
+#include <alsa/asoundlib.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define AUDMIN(a,b)     ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct sim_audio_s
+{
+  struct audio_lowerhalf_s dev;
+  struct dq_queue_s pendq;
+
+  sq_entry_t link;
+
+  bool playback;
+  uint32_t frame_size;
+  uint32_t nbuffers;
+  uint32_t buffer_size;
+
+  uint32_t sample_rate;
+  uint32_t channels;
+  uint32_t bps;
+
+  snd_pcm_t *pcm;
+  snd_mixer_t *mixer;
+  snd_mixer_elem_t *volume;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int sim_audio_getcaps(struct audio_lowerhalf_s *dev, int type,
+                             struct audio_caps_s *caps);
+static int sim_audio_configure(struct audio_lowerhalf_s *dev,
+                               const struct audio_caps_s *caps);
+static int sim_audio_shutdown(struct audio_lowerhalf_s *dev);
+static int sim_audio_start(struct audio_lowerhalf_s *dev);
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+static int sim_audio_stop(struct audio_lowerhalf_s *dev);
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+static int sim_audio_pause(struct audio_lowerhalf_s *dev);
+static int sim_audio_resume(struct audio_lowerhalf_s *dev);
+#endif
+static int sim_audio_enqueuebuffer(struct audio_lowerhalf_s *dev,
+                                   struct ap_buffer_s *apb);
+static int sim_audio_ioctl(struct audio_lowerhalf_s *dev, int cmd,
+                           unsigned long arg);
+static int sim_audio_reserve(struct audio_lowerhalf_s *dev);
+static int sim_audio_release(struct audio_lowerhalf_s *dev);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct audio_ops_s g_sim_audio_ops =
+{
+  .getcaps       = sim_audio_getcaps,
+  .configure     = sim_audio_configure,
+  .shutdown      = sim_audio_shutdown,
+  .start         = sim_audio_start,
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+  .stop          = sim_audio_stop,
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+  .pause         = sim_audio_pause,
+  .resume        = sim_audio_resume,
+#endif
+  .enqueuebuffer = sim_audio_enqueuebuffer,
+  .ioctl         = sim_audio_ioctl,
+  .reserve       = sim_audio_reserve,
+  .release       = sim_audio_release,
+};
+
+static sq_queue_t g_sim_audio;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int sim_audio_config_format(struct sim_audio_s *priv, snd_pcm_t *pcm)
+{
+  snd_pcm_hw_params_t *hw_params;
+  snd_pcm_uframes_t pframes;
+  snd_pcm_format_t format;
+  uint32_t total_size;
+  int ret;
+
+  switch (priv->bps)
+    {
+      case 8:
+        format = SND_PCM_FORMAT_S8;
+        break;
+      case 24:
+        format = SND_PCM_FORMAT_S24;
+        break;
+      case 32:
+        format = SND_PCM_FORMAT_S32;
+        break;
+      default:
+        format = SND_PCM_FORMAT_S16;
+        break;
+    }
+
+  ret = snd_pcm_hw_params_malloc(&hw_params);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = snd_pcm_hw_params_any(pcm, hw_params);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_access(pcm, hw_params,
+                                     SND_PCM_ACCESS_RW_INTERLEAVED);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_format(pcm, hw_params, format);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_rate(pcm, hw_params,
+                                   priv->sample_rate, 0);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_channels(pcm, hw_params,
+                                       priv->channels);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  total_size = priv->nbuffers * priv->buffer_size;
+
+  pframes = priv->buffer_size / priv->frame_size;
+  ret = snd_pcm_hw_params_set_period_size_near(pcm, hw_params,
+                                               &pframes, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  priv->buffer_size = pframes * priv->frame_size;
+  priv->nbuffers    = total_size / priv->buffer_size;
+  ret = snd_pcm_hw_params_set_periods_near(pcm, hw_params,
+                                           &priv->nbuffers, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params(pcm, hw_params);
+
+fail:
+  snd_pcm_hw_params_free(hw_params);
+  return ret;
+}
+
+static int sim_audio_open(struct sim_audio_s *priv)
+{
+  snd_pcm_t *pcm;
+  int direction;
+  int ret;
+
+  if (priv->pcm)
+    {
+      return 0;
+    }
+
+  direction = priv->playback ? SND_PCM_STREAM_PLAYBACK
+                             : SND_PCM_STREAM_CAPTURE;
+
+  ret = snd_pcm_open(&pcm, "default", direction, 0);

Review comment:
       In ALSA design, user can modify alsa.conf to map "default" to the different sound hardware. That's why we don't make the device name configurable initially. If you really need this feature, we can add it.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#issuecomment-721712346


   @btashton could you review again? I think your comment is already addressed in the latest revision.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#discussion_r517329885



##########
File path: arch/sim/src/sim/up_alsa.c
##########
@@ -0,0 +1,731 @@
+/****************************************************************************
+ * arch/sim/src/sim/up_alsa.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/audio/audio.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/nuttx.h>
+
+#include <queue.h>
+
+#include <alsa/asoundlib.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define AUDMIN(a,b)     ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct sim_audio_s
+{
+  struct audio_lowerhalf_s dev;
+  struct dq_queue_s pendq;
+
+  sq_entry_t link;
+
+  bool playback;
+  uint32_t frame_size;
+  uint32_t nbuffers;
+  uint32_t buffer_size;
+
+  uint32_t sample_rate;
+  uint32_t channels;
+  uint32_t bps;
+
+  snd_pcm_t *pcm;
+  snd_mixer_t *mixer;
+  snd_mixer_elem_t *volume;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int sim_audio_getcaps(struct audio_lowerhalf_s *dev, int type,
+                             struct audio_caps_s *caps);
+static int sim_audio_configure(struct audio_lowerhalf_s *dev,
+                               const struct audio_caps_s *caps);
+static int sim_audio_shutdown(struct audio_lowerhalf_s *dev);
+static int sim_audio_start(struct audio_lowerhalf_s *dev);
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+static int sim_audio_stop(struct audio_lowerhalf_s *dev);
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+static int sim_audio_pause(struct audio_lowerhalf_s *dev);
+static int sim_audio_resume(struct audio_lowerhalf_s *dev);
+#endif
+static int sim_audio_enqueuebuffer(struct audio_lowerhalf_s *dev,
+                                   struct ap_buffer_s *apb);
+static int sim_audio_ioctl(struct audio_lowerhalf_s *dev, int cmd,
+                           unsigned long arg);
+static int sim_audio_reserve(struct audio_lowerhalf_s *dev);
+static int sim_audio_release(struct audio_lowerhalf_s *dev);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct audio_ops_s g_sim_audio_ops =
+{
+  .getcaps       = sim_audio_getcaps,
+  .configure     = sim_audio_configure,
+  .shutdown      = sim_audio_shutdown,
+  .start         = sim_audio_start,
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+  .stop          = sim_audio_stop,
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+  .pause         = sim_audio_pause,
+  .resume        = sim_audio_resume,
+#endif
+  .enqueuebuffer = sim_audio_enqueuebuffer,
+  .ioctl         = sim_audio_ioctl,
+  .reserve       = sim_audio_reserve,
+  .release       = sim_audio_release,
+};
+
+static sq_queue_t g_sim_audio;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int sim_audio_config_format(struct sim_audio_s *priv, snd_pcm_t *pcm)
+{
+  snd_pcm_hw_params_t *hw_params;
+  snd_pcm_uframes_t pframes;
+  snd_pcm_format_t format;
+  uint32_t total_size;
+  int ret;
+
+  switch (priv->bps)
+    {
+      case 8:
+        format = SND_PCM_FORMAT_S8;
+        break;
+      case 24:
+        format = SND_PCM_FORMAT_S24;
+        break;
+      case 32:
+        format = SND_PCM_FORMAT_S32;
+        break;
+      default:
+        format = SND_PCM_FORMAT_S16;
+        break;
+    }
+
+  ret = snd_pcm_hw_params_malloc(&hw_params);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = snd_pcm_hw_params_any(pcm, hw_params);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_access(pcm, hw_params,
+                                     SND_PCM_ACCESS_RW_INTERLEAVED);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_format(pcm, hw_params, format);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_rate(pcm, hw_params,
+                                   priv->sample_rate, 0);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_channels(pcm, hw_params,
+                                       priv->channels);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  total_size = priv->nbuffers * priv->buffer_size;
+
+  pframes = priv->buffer_size / priv->frame_size;
+  ret = snd_pcm_hw_params_set_period_size_near(pcm, hw_params,
+                                               &pframes, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  priv->buffer_size = pframes * priv->frame_size;
+  priv->nbuffers    = total_size / priv->buffer_size;
+  ret = snd_pcm_hw_params_set_periods_near(pcm, hw_params,
+                                           &priv->nbuffers, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params(pcm, hw_params);
+
+fail:
+  snd_pcm_hw_params_free(hw_params);
+  return ret;
+}
+
+static int sim_audio_open(struct sim_audio_s *priv)
+{
+  snd_pcm_t *pcm;
+  int direction;
+  int ret;
+
+  if (priv->pcm)
+    {
+      return 0;
+    }
+
+  direction = priv->playback ? SND_PCM_STREAM_PLAYBACK
+                             : SND_PCM_STREAM_CAPTURE;
+
+  ret = snd_pcm_open(&pcm, "default", direction, 0);

Review comment:
       Do you want to change the device name? Ok, it depend on how many pcm instances we want to create. only one playback/capture is created in the current design, "default" should be the best choice in this case. On the other hand, we sim_audio_initialize need accept name argument and open the different sound device.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] GUIDINGLI commented on a change in pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
GUIDINGLI commented on a change in pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#discussion_r516452647



##########
File path: arch/sim/src/Makefile
##########
@@ -202,6 +202,11 @@ ifeq ($(CONFIG_RPTUN),y)
   CSRCS += up_rptun.c
 endif
 
+ifeq ($(CONFIG_AUDIO),y)

Review comment:
       done

##########
File path: arch/sim/src/sim/up_audio.c
##########
@@ -0,0 +1,590 @@
+/****************************************************************************
+ * arch/sim/src/sim/up_audio.c

Review comment:
       done

##########
File path: arch/sim/src/sim/up_initialize.c
##########
@@ -281,4 +282,9 @@ void up_initialize(void)
 #if defined(CONFIG_FS_SMARTFS) && (defined(CONFIG_SIM_SPIFLASH) || defined(CONFIG_SIM_QSPIFLASH))
   up_init_smartfs();
 #endif
+
+#ifdef CONFIG_AUDIO
+  audio_register("pcm0p", sim_audio_initialize(true));

Review comment:
       done




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] btashton commented on a change in pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
btashton commented on a change in pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#discussion_r516793069



##########
File path: arch/sim/src/sim/up_initialize.c
##########
@@ -281,4 +282,9 @@ void up_initialize(void)
 #if defined(CONFIG_FS_SMARTFS) && (defined(CONFIG_SIM_SPIFLASH) || defined(CONFIG_SIM_QSPIFLASH))
   up_init_smartfs();
 #endif
+
+#ifdef CONFIG_AUDIO
+  audio_register("pcm0p", sim_audio_initialize(true));

Review comment:
       @GUIDINGLI I don't see this in the recent commits




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#discussion_r517353293



##########
File path: arch/sim/src/sim/up_alsa.c
##########
@@ -0,0 +1,731 @@
+/****************************************************************************
+ * arch/sim/src/sim/up_alsa.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/audio/audio.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/nuttx.h>
+
+#include <queue.h>
+
+#include <alsa/asoundlib.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define AUDMIN(a,b)     ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct sim_audio_s
+{
+  struct audio_lowerhalf_s dev;
+  struct dq_queue_s pendq;
+
+  sq_entry_t link;
+
+  bool playback;
+  uint32_t frame_size;
+  uint32_t nbuffers;
+  uint32_t buffer_size;
+
+  uint32_t sample_rate;
+  uint32_t channels;
+  uint32_t bps;
+
+  snd_pcm_t *pcm;
+  snd_mixer_t *mixer;
+  snd_mixer_elem_t *volume;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int sim_audio_getcaps(struct audio_lowerhalf_s *dev, int type,
+                             struct audio_caps_s *caps);
+static int sim_audio_configure(struct audio_lowerhalf_s *dev,
+                               const struct audio_caps_s *caps);
+static int sim_audio_shutdown(struct audio_lowerhalf_s *dev);
+static int sim_audio_start(struct audio_lowerhalf_s *dev);
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+static int sim_audio_stop(struct audio_lowerhalf_s *dev);
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+static int sim_audio_pause(struct audio_lowerhalf_s *dev);
+static int sim_audio_resume(struct audio_lowerhalf_s *dev);
+#endif
+static int sim_audio_enqueuebuffer(struct audio_lowerhalf_s *dev,
+                                   struct ap_buffer_s *apb);
+static int sim_audio_ioctl(struct audio_lowerhalf_s *dev, int cmd,
+                           unsigned long arg);
+static int sim_audio_reserve(struct audio_lowerhalf_s *dev);
+static int sim_audio_release(struct audio_lowerhalf_s *dev);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct audio_ops_s g_sim_audio_ops =
+{
+  .getcaps       = sim_audio_getcaps,
+  .configure     = sim_audio_configure,
+  .shutdown      = sim_audio_shutdown,
+  .start         = sim_audio_start,
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+  .stop          = sim_audio_stop,
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+  .pause         = sim_audio_pause,
+  .resume        = sim_audio_resume,
+#endif
+  .enqueuebuffer = sim_audio_enqueuebuffer,
+  .ioctl         = sim_audio_ioctl,
+  .reserve       = sim_audio_reserve,
+  .release       = sim_audio_release,
+};
+
+static sq_queue_t g_sim_audio;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int sim_audio_config_format(struct sim_audio_s *priv, snd_pcm_t *pcm)
+{
+  snd_pcm_hw_params_t *hw_params;
+  snd_pcm_uframes_t pframes;
+  snd_pcm_format_t format;
+  uint32_t total_size;
+  int ret;
+
+  switch (priv->bps)
+    {
+      case 8:
+        format = SND_PCM_FORMAT_S8;
+        break;
+      case 24:
+        format = SND_PCM_FORMAT_S24;
+        break;
+      case 32:
+        format = SND_PCM_FORMAT_S32;
+        break;
+      default:
+        format = SND_PCM_FORMAT_S16;
+        break;
+    }
+
+  ret = snd_pcm_hw_params_malloc(&hw_params);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = snd_pcm_hw_params_any(pcm, hw_params);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_access(pcm, hw_params,
+                                     SND_PCM_ACCESS_RW_INTERLEAVED);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_format(pcm, hw_params, format);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_rate(pcm, hw_params,
+                                   priv->sample_rate, 0);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_channels(pcm, hw_params,
+                                       priv->channels);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  total_size = priv->nbuffers * priv->buffer_size;
+
+  pframes = priv->buffer_size / priv->frame_size;
+  ret = snd_pcm_hw_params_set_period_size_near(pcm, hw_params,
+                                               &pframes, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  priv->buffer_size = pframes * priv->frame_size;
+  priv->nbuffers    = total_size / priv->buffer_size;
+  ret = snd_pcm_hw_params_set_periods_near(pcm, hw_params,
+                                           &priv->nbuffers, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params(pcm, hw_params);
+
+fail:
+  snd_pcm_hw_params_free(hw_params);
+  return ret;
+}
+
+static int sim_audio_open(struct sim_audio_s *priv)
+{
+  snd_pcm_t *pcm;
+  int direction;
+  int ret;
+
+  if (priv->pcm)
+    {
+      return 0;
+    }
+
+  direction = priv->playback ? SND_PCM_STREAM_PLAYBACK
+                             : SND_PCM_STREAM_CAPTURE;
+
+  ret = snd_pcm_open(&pcm, "default", direction, 0);

Review comment:
       How about we merge this patch first? and  @GUIDINGLI provide a new patch to support configuration in tomorrow.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] btashton commented on pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
btashton commented on pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#issuecomment-721705722


   No. My comment has not been addressed. 


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] GUIDINGLI commented on pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
GUIDINGLI commented on pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#issuecomment-721506417


   @masayuki2009 
   
   You should also update apps,:
   
   https://github.com/apache/incubator-nuttx-apps/pull/461
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#discussion_r517329885



##########
File path: arch/sim/src/sim/up_alsa.c
##########
@@ -0,0 +1,731 @@
+/****************************************************************************
+ * arch/sim/src/sim/up_alsa.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/audio/audio.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/nuttx.h>
+
+#include <queue.h>
+
+#include <alsa/asoundlib.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define AUDMIN(a,b)     ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct sim_audio_s
+{
+  struct audio_lowerhalf_s dev;
+  struct dq_queue_s pendq;
+
+  sq_entry_t link;
+
+  bool playback;
+  uint32_t frame_size;
+  uint32_t nbuffers;
+  uint32_t buffer_size;
+
+  uint32_t sample_rate;
+  uint32_t channels;
+  uint32_t bps;
+
+  snd_pcm_t *pcm;
+  snd_mixer_t *mixer;
+  snd_mixer_elem_t *volume;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int sim_audio_getcaps(struct audio_lowerhalf_s *dev, int type,
+                             struct audio_caps_s *caps);
+static int sim_audio_configure(struct audio_lowerhalf_s *dev,
+                               const struct audio_caps_s *caps);
+static int sim_audio_shutdown(struct audio_lowerhalf_s *dev);
+static int sim_audio_start(struct audio_lowerhalf_s *dev);
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+static int sim_audio_stop(struct audio_lowerhalf_s *dev);
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+static int sim_audio_pause(struct audio_lowerhalf_s *dev);
+static int sim_audio_resume(struct audio_lowerhalf_s *dev);
+#endif
+static int sim_audio_enqueuebuffer(struct audio_lowerhalf_s *dev,
+                                   struct ap_buffer_s *apb);
+static int sim_audio_ioctl(struct audio_lowerhalf_s *dev, int cmd,
+                           unsigned long arg);
+static int sim_audio_reserve(struct audio_lowerhalf_s *dev);
+static int sim_audio_release(struct audio_lowerhalf_s *dev);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct audio_ops_s g_sim_audio_ops =
+{
+  .getcaps       = sim_audio_getcaps,
+  .configure     = sim_audio_configure,
+  .shutdown      = sim_audio_shutdown,
+  .start         = sim_audio_start,
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+  .stop          = sim_audio_stop,
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+  .pause         = sim_audio_pause,
+  .resume        = sim_audio_resume,
+#endif
+  .enqueuebuffer = sim_audio_enqueuebuffer,
+  .ioctl         = sim_audio_ioctl,
+  .reserve       = sim_audio_reserve,
+  .release       = sim_audio_release,
+};
+
+static sq_queue_t g_sim_audio;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int sim_audio_config_format(struct sim_audio_s *priv, snd_pcm_t *pcm)
+{
+  snd_pcm_hw_params_t *hw_params;
+  snd_pcm_uframes_t pframes;
+  snd_pcm_format_t format;
+  uint32_t total_size;
+  int ret;
+
+  switch (priv->bps)
+    {
+      case 8:
+        format = SND_PCM_FORMAT_S8;
+        break;
+      case 24:
+        format = SND_PCM_FORMAT_S24;
+        break;
+      case 32:
+        format = SND_PCM_FORMAT_S32;
+        break;
+      default:
+        format = SND_PCM_FORMAT_S16;
+        break;
+    }
+
+  ret = snd_pcm_hw_params_malloc(&hw_params);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = snd_pcm_hw_params_any(pcm, hw_params);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_access(pcm, hw_params,
+                                     SND_PCM_ACCESS_RW_INTERLEAVED);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_format(pcm, hw_params, format);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_rate(pcm, hw_params,
+                                   priv->sample_rate, 0);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_channels(pcm, hw_params,
+                                       priv->channels);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  total_size = priv->nbuffers * priv->buffer_size;
+
+  pframes = priv->buffer_size / priv->frame_size;
+  ret = snd_pcm_hw_params_set_period_size_near(pcm, hw_params,
+                                               &pframes, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  priv->buffer_size = pframes * priv->frame_size;
+  priv->nbuffers    = total_size / priv->buffer_size;
+  ret = snd_pcm_hw_params_set_periods_near(pcm, hw_params,
+                                           &priv->nbuffers, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params(pcm, hw_params);
+
+fail:
+  snd_pcm_hw_params_free(hw_params);
+  return ret;
+}
+
+static int sim_audio_open(struct sim_audio_s *priv)
+{
+  snd_pcm_t *pcm;
+  int direction;
+  int ret;
+
+  if (priv->pcm)
+    {
+      return 0;
+    }
+
+  direction = priv->playback ? SND_PCM_STREAM_PLAYBACK
+                             : SND_PCM_STREAM_CAPTURE;
+
+  ret = snd_pcm_open(&pcm, "default", direction, 0);

Review comment:
       Do you want to change the device name? Ok, it depend on how many pcm instances we want to create. only one playback/capture is created in the current design, "default" should be the best choice in this case. On the other hand, sim_audio_initialize need accept name argument and open the different sound device if we want to simulate multiple sound devices. But I think it's better to create a new patch to change both the alsa driver and board initialization. Do you think so? 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] btashton commented on pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
btashton commented on pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#issuecomment-718986008


   I'll update the test repo to be able to support this when I do my testing. We will need to have the alsa libraries available. 


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] masayuki2009 commented on pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
masayuki2009 commented on pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#issuecomment-721511490


   >You should also update apps,:
   >
   >apache/incubator-nuttx-apps#461
   
   @GUIDINGLI 
   Thanks, it works now!
   
   
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] acassis commented on pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
acassis commented on pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#issuecomment-716701307


   Very nice!


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#discussion_r517329885



##########
File path: arch/sim/src/sim/up_alsa.c
##########
@@ -0,0 +1,731 @@
+/****************************************************************************
+ * arch/sim/src/sim/up_alsa.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/audio/audio.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/nuttx.h>
+
+#include <queue.h>
+
+#include <alsa/asoundlib.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define AUDMIN(a,b)     ((a) > (b) ? (b) : (a))
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct sim_audio_s
+{
+  struct audio_lowerhalf_s dev;
+  struct dq_queue_s pendq;
+
+  sq_entry_t link;
+
+  bool playback;
+  uint32_t frame_size;
+  uint32_t nbuffers;
+  uint32_t buffer_size;
+
+  uint32_t sample_rate;
+  uint32_t channels;
+  uint32_t bps;
+
+  snd_pcm_t *pcm;
+  snd_mixer_t *mixer;
+  snd_mixer_elem_t *volume;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int sim_audio_getcaps(struct audio_lowerhalf_s *dev, int type,
+                             struct audio_caps_s *caps);
+static int sim_audio_configure(struct audio_lowerhalf_s *dev,
+                               const struct audio_caps_s *caps);
+static int sim_audio_shutdown(struct audio_lowerhalf_s *dev);
+static int sim_audio_start(struct audio_lowerhalf_s *dev);
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+static int sim_audio_stop(struct audio_lowerhalf_s *dev);
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+static int sim_audio_pause(struct audio_lowerhalf_s *dev);
+static int sim_audio_resume(struct audio_lowerhalf_s *dev);
+#endif
+static int sim_audio_enqueuebuffer(struct audio_lowerhalf_s *dev,
+                                   struct ap_buffer_s *apb);
+static int sim_audio_ioctl(struct audio_lowerhalf_s *dev, int cmd,
+                           unsigned long arg);
+static int sim_audio_reserve(struct audio_lowerhalf_s *dev);
+static int sim_audio_release(struct audio_lowerhalf_s *dev);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct audio_ops_s g_sim_audio_ops =
+{
+  .getcaps       = sim_audio_getcaps,
+  .configure     = sim_audio_configure,
+  .shutdown      = sim_audio_shutdown,
+  .start         = sim_audio_start,
+#ifndef CONFIG_AUDIO_EXCLUDE_STOP
+  .stop          = sim_audio_stop,
+#endif
+#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
+  .pause         = sim_audio_pause,
+  .resume        = sim_audio_resume,
+#endif
+  .enqueuebuffer = sim_audio_enqueuebuffer,
+  .ioctl         = sim_audio_ioctl,
+  .reserve       = sim_audio_reserve,
+  .release       = sim_audio_release,
+};
+
+static sq_queue_t g_sim_audio;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int sim_audio_config_format(struct sim_audio_s *priv, snd_pcm_t *pcm)
+{
+  snd_pcm_hw_params_t *hw_params;
+  snd_pcm_uframes_t pframes;
+  snd_pcm_format_t format;
+  uint32_t total_size;
+  int ret;
+
+  switch (priv->bps)
+    {
+      case 8:
+        format = SND_PCM_FORMAT_S8;
+        break;
+      case 24:
+        format = SND_PCM_FORMAT_S24;
+        break;
+      case 32:
+        format = SND_PCM_FORMAT_S32;
+        break;
+      default:
+        format = SND_PCM_FORMAT_S16;
+        break;
+    }
+
+  ret = snd_pcm_hw_params_malloc(&hw_params);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  ret = snd_pcm_hw_params_any(pcm, hw_params);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_access(pcm, hw_params,
+                                     SND_PCM_ACCESS_RW_INTERLEAVED);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_format(pcm, hw_params, format);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_rate(pcm, hw_params,
+                                   priv->sample_rate, 0);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params_set_channels(pcm, hw_params,
+                                       priv->channels);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  total_size = priv->nbuffers * priv->buffer_size;
+
+  pframes = priv->buffer_size / priv->frame_size;
+  ret = snd_pcm_hw_params_set_period_size_near(pcm, hw_params,
+                                               &pframes, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  priv->buffer_size = pframes * priv->frame_size;
+  priv->nbuffers    = total_size / priv->buffer_size;
+  ret = snd_pcm_hw_params_set_periods_near(pcm, hw_params,
+                                           &priv->nbuffers, NULL);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  ret = snd_pcm_hw_params(pcm, hw_params);
+
+fail:
+  snd_pcm_hw_params_free(hw_params);
+  return ret;
+}
+
+static int sim_audio_open(struct sim_audio_s *priv)
+{
+  snd_pcm_t *pcm;
+  int direction;
+  int ret;
+
+  if (priv->pcm)
+    {
+      return 0;
+    }
+
+  direction = priv->playback ? SND_PCM_STREAM_PLAYBACK
+                             : SND_PCM_STREAM_CAPTURE;
+
+  ret = snd_pcm_open(&pcm, "default", direction, 0);

Review comment:
       Do you want to change the device name? Ok, it depends on how many pcm instances we want to create. only one playback/capture is created in the current design, "default" should be the best choice in this case. On the other hand, sim_audio_initialize need accept name argument and open the different sound device if we want to simulate multiple sound devices. But I think it's better to create a new patch to change both the alsa driver and board initialization. Do you think so? 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] masayuki2009 commented on pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
masayuki2009 commented on pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#issuecomment-721475927


   @GUIDINGLI @xiaoxiang781216 
   I tried this PR with ubuntu18.04 x86_64, but nuttx crashed when playback an WAV file via hostfs.
   Could you tell me what environment are you using?
   
   ```
   Thread 1 "nuttx" received signal SIGSEGV, Segmentation fault.                                                                                                                   
   0x00005555555714a2 in file_vioctl (filep=0x10000000d8, req=4100, ap=0x7ffff2c8b520) at vfs/fs_ioctl.c:87                                                                        
   87        inode = filep->f_inode;                                                                                                                                                                                                                                                                                                                     
   p filep                                                                                                                                                                         
   $1 = (struct file *) 0x10000000d8                                                                                                                                               
   (gdb) p *filep                                                                                                                                                                  
   Cannot access memory at address 0x10000000d8                                                                                                                                    
   (gdb) up                                                                                                                                                                        
   #1  0x0000555555571682 in nx_vioctl (fd=4, req=4100, ap=0x7ffff2c8b520) at vfs/fs_ioctl.c:173                                                                                   
   173           ret = file_vioctl(filep, req, ap);                                                                                                                                
   (gdb) where                                                                                                                                                                     
   #0  0x00005555555714a2 in file_vioctl (filep=0x10000000d8, req=4100, ap=0x7ffff2c8b520) at vfs/fs_ioctl.c:87                                                                    
   #1  0x0000555555571682 in nx_vioctl (fd=4, req=4100, ap=0x7ffff2c8b520) at vfs/fs_ioctl.c:173                                                                                   
   #2  0x000055555557197a in ioctl (fd=4, req=4100) at vfs/fs_ioctl.c:259                                                                                                          
   #3  0x000055555558f487 in nxplayer_setvolume (pplayer=0x7ffff2bfa0f0, volume=400) at nxplayer.c:1226                                                                            
   #4  0x000055555558f164 in nxplayer_playthread (pvarg=0x7ffff2bfa0f0) at nxplayer.c:957                                                                                          
   #5  0x0000555555583970 in pthread_start () at pthread/pthread_create.c:192                                                                                                      
   #6  0x0000000000000000 in ?? ()     
   ```
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] GUIDINGLI commented on pull request #2124: arch/sim: add sim audio support

Posted by GitBox <gi...@apache.org>.
GUIDINGLI commented on pull request #2124:
URL: https://github.com/apache/incubator-nuttx/pull/2124#issuecomment-721501909


   @masayuki2009
   
   Have you pick another patch for increase stack size ?
   
   https://github.com/apache/incubator-nuttx/pull/2124/commits/d86d7bc177d9180022549d4d4476b88127510f79
   
   From your log:
   #0  0x00005555555714a2 in file_vioctl (filep=0x10000000d8, req=4100, ap=0x7ffff2c8b520) at vfs/fs_ioctl.c:87
   
   filep address is invaild
   
   in my gdb:
   
   filep is the same area with ap, both 0x7ffff2c...
   
   #0  file_vioctl (filep=0x7ffff2c7a8c8, req=4100, ap=0x7ffff2c9af40) at vfs/fs_ioctl.c:87
   #1  0x000055555556fb4c in nx_vioctl (fd=4, req=4100, ap=0x7ffff2c9af40) at vfs/fs_ioctl.c:173
   #2  0x000055555556fe44 in ioctl (fd=4, req=4100) at vfs/fs_ioctl.c:259
   #3  0x000055555558cc96 in nxplayer_setvolume (pplayer=0x7ffff2bfa0f0, volume=400) at nxplayer.c:1226
   #4  0x000055555558c973 in nxplayer_playthread (pvarg=0x7ffff2bfa0f0) at nxplayer.c:957
   #5  0x0000555555581e3a in pthread_start () at pthread/pthread_create.c:192
   #6  0x0000000000000000 in ?? ()
   
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org