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:
us...@infra.apache.org


Reply via email to