2014-07-22 19:54:13 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* audio/pcm_decode.c
|
|
|
|
*
|
2021-04-10 08:50:57 +02:00
|
|
|
* 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
|
2014-07-22 19:54:13 +02:00
|
|
|
*
|
2021-04-10 08:50:57 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-07-22 19:54:13 +02:00
|
|
|
*
|
2021-04-10 08:50:57 +02:00
|
|
|
* 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.
|
2014-07-22 19:54:13 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2020-11-27 06:23:23 +01:00
|
|
|
#include <inttypes.h>
|
2014-07-22 19:54:13 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/audio/audio.h>
|
2014-07-22 23:54:56 +02:00
|
|
|
#include <nuttx/audio/pcm.h>
|
2014-07-22 19:54:13 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_AUDIO) && defined(CONFIG_AUDIO_FORMAT_PCM)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2018-08-24 13:51:44 +02:00
|
|
|
|
2014-07-22 19:54:13 +02:00
|
|
|
/* Configuration ************************************************************/
|
2014-07-28 02:33:17 +02:00
|
|
|
|
2014-07-23 03:23:05 +02:00
|
|
|
#define CONFIG_PCM_DEBUG 1 /* For now */
|
2014-07-22 19:54:13 +02:00
|
|
|
|
2014-07-28 02:33:17 +02:00
|
|
|
/* Often defined and re-defined macros */
|
|
|
|
|
|
|
|
#ifndef MIN
|
|
|
|
# define MIN(a,b) (((a) < (b)) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MAX
|
|
|
|
# define MAX(a,b) (((a) > (b)) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
2014-07-22 19:54:13 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
2018-08-24 13:51:44 +02:00
|
|
|
|
2014-07-22 19:54:13 +02:00
|
|
|
/* This structure describes the internal state of the PCM decoder */
|
|
|
|
|
|
|
|
struct pcm_decode_s
|
|
|
|
{
|
|
|
|
/* This is is our our appearance to the outside world. This *MUST* be the
|
|
|
|
* first element of the structure so that we can freely cast between types
|
|
|
|
* struct audio_lowerhalf and struct pcm_decode_s.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct audio_lowerhalf_s export;
|
|
|
|
|
|
|
|
/* These are our operations that intervene between the player application
|
|
|
|
* and the lower level driver. Unlike the ops in the struct
|
|
|
|
* audio_lowerhalf_s, these are writeable because we need to customize a
|
2017-05-11 21:35:56 +02:00
|
|
|
* few of the methods based upon what is supported by the lower level
|
2014-07-22 19:54:13 +02:00
|
|
|
* driver.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct audio_ops_s ops;
|
|
|
|
|
|
|
|
/* This is the contained, low-level DAC-type device and will receive the
|
|
|
|
* decoded PCM audio data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
FAR struct audio_lowerhalf_s *lower;
|
2014-07-23 03:23:05 +02:00
|
|
|
|
2014-07-24 17:56:39 +02:00
|
|
|
/* Session returned from the lower level driver */
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
FAR void *session;
|
|
|
|
#endif
|
|
|
|
|
2014-07-23 19:58:43 +02:00
|
|
|
/* These are values extracted from WAV file header */
|
2014-07-23 03:23:05 +02:00
|
|
|
|
2014-07-28 02:33:17 +02:00
|
|
|
uint32_t samprate; /* 8000, 44100, ... */
|
|
|
|
uint32_t byterate; /* samprate * nchannels * bpsamp / 8 */
|
|
|
|
uint8_t align; /* nchannels * bpsamp / 8 */
|
|
|
|
uint8_t bpsamp; /* Bits per sample: 8 bits = 8, 16 bits = 16 */
|
|
|
|
uint8_t nchannels; /* Mono=1, Stereo=2 */
|
|
|
|
bool streaming; /* Streaming PCM data chunk */
|
|
|
|
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_FFORWARD
|
|
|
|
/* Fast forward support */
|
|
|
|
|
|
|
|
uint8_t subsample; /* Fast forward rate: See AUDIO_SUBSAMPLE_* defns */
|
2014-07-28 05:42:10 +02:00
|
|
|
uint8_t skip; /* Number of sample bytes to be skipped */
|
2014-07-28 05:00:19 +02:00
|
|
|
uint8_t npartial; /* Size of the partially copied sample */
|
2014-07-28 02:33:17 +02:00
|
|
|
#endif
|
2014-07-22 19:54:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-07-23 03:23:05 +02:00
|
|
|
/* Helper functions *********************************************************/
|
|
|
|
|
2014-07-22 23:54:56 +02:00
|
|
|
#ifdef CONFIG_PCM_DEBUG
|
2014-07-23 03:23:05 +02:00
|
|
|
static void pcm_dump(FAR const struct wav_header_s *wav);
|
|
|
|
#else
|
|
|
|
# define pcm_dump(w)
|
2014-07-22 23:54:56 +02:00
|
|
|
#endif
|
|
|
|
|
2020-10-07 10:12:55 +02:00
|
|
|
#ifndef CONFIG_AUDIO_FORMAT_RAW
|
2014-07-23 03:23:05 +02:00
|
|
|
static inline bool pcm_validwav(FAR const struct wav_header_s *wav);
|
2021-04-18 12:05:12 +02:00
|
|
|
static ssize_t pcm_parsewav(FAR struct pcm_decode_s *priv, uint8_t *data,
|
|
|
|
apb_samp_t len);
|
2020-10-07 10:12:55 +02:00
|
|
|
#endif
|
2014-07-28 02:33:17 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_FFORWARD
|
2014-07-28 05:42:10 +02:00
|
|
|
static void pcm_subsample_configure(FAR struct pcm_decode_s *priv,
|
2014-07-28 02:33:17 +02:00
|
|
|
uint8_t subsample);
|
2014-07-28 05:42:10 +02:00
|
|
|
static void pcm_subsample(FAR struct pcm_decode_s *priv,
|
2014-07-28 02:33:17 +02:00
|
|
|
FAR struct ap_buffer_s *apb);
|
|
|
|
#endif
|
2014-07-23 03:23:05 +02:00
|
|
|
|
2014-07-22 19:54:13 +02:00
|
|
|
/* struct audio_lowerhalf_s methods *****************************************/
|
|
|
|
|
|
|
|
static int pcm_getcaps(FAR struct audio_lowerhalf_s *dev, int type,
|
|
|
|
FAR struct audio_caps_s *caps);
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
static int pcm_configure(FAR struct audio_lowerhalf_s *dev,
|
|
|
|
FAR void *session, FAR const struct audio_caps_s *caps);
|
|
|
|
#else
|
|
|
|
static int pcm_configure(FAR struct audio_lowerhalf_s *dev,
|
|
|
|
FAR const struct audio_caps_s *caps);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int pcm_shutdown(FAR struct audio_lowerhalf_s *dev);
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
static int pcm_start(FAR struct audio_lowerhalf_s *dev, FAR void *session);
|
|
|
|
#else
|
|
|
|
static int pcm_start(FAR struct audio_lowerhalf_s *dev);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_STOP
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
static int pcm_stop(FAR struct audio_lowerhalf_s *dev, FAR void *session);
|
|
|
|
#else
|
|
|
|
static int pcm_stop(FAR struct audio_lowerhalf_s *dev);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
static int pcm_pause(FAR struct audio_lowerhalf_s *dev, FAR void *session);
|
|
|
|
#else
|
|
|
|
static int pcm_pause(FAR struct audio_lowerhalf_s *dev);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
static int pcm_resume(FAR struct audio_lowerhalf_s *dev, FAR void *session);
|
|
|
|
#else
|
|
|
|
static int pcm_resume(FAR struct audio_lowerhalf_s *dev);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int pcm_allocbuffer(FAR struct audio_lowerhalf_s *dev,
|
|
|
|
FAR struct audio_buf_desc_s *apb);
|
|
|
|
static int pcm_freebuffer(FAR struct audio_lowerhalf_s *dev,
|
|
|
|
FAR struct audio_buf_desc_s *apb);
|
|
|
|
static int pcm_enqueuebuffer(FAR struct audio_lowerhalf_s *dev,
|
|
|
|
FAR struct ap_buffer_s *apb);
|
|
|
|
static int pcm_cancelbuffer(FAR struct audio_lowerhalf_s *dev,
|
|
|
|
FAR struct ap_buffer_s *apb);
|
|
|
|
static int pcm_ioctl(FAR struct audio_lowerhalf_s *dev,
|
|
|
|
int cmd, unsigned long arg);
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
2020-04-21 09:45:24 +02:00
|
|
|
static int pcm_reserve(FAR struct audio_lowerhalf_s *dev,
|
|
|
|
FAR void **session);
|
2014-07-22 19:54:13 +02:00
|
|
|
#else
|
|
|
|
static int pcm_reserve(FAR struct audio_lowerhalf_s *dev);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
2020-04-21 09:45:24 +02:00
|
|
|
static int pcm_release(FAR struct audio_lowerhalf_s *dev,
|
|
|
|
FAR void *session);
|
2014-07-22 19:54:13 +02:00
|
|
|
#else
|
|
|
|
static int pcm_release(FAR struct audio_lowerhalf_s *dev);
|
|
|
|
#endif
|
|
|
|
|
2014-07-25 18:07:32 +02:00
|
|
|
/* Audio callback */
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
static void pcm_callback(FAR void *arg, uint16_t reason,
|
|
|
|
FAR struct ap_buffer_s *apb, uint16_t status,
|
|
|
|
FAR void *session);
|
|
|
|
#else
|
|
|
|
static void pcm_callback(FAR void *arg, uint16_t reason,
|
|
|
|
FAR struct ap_buffer_s *apb, uint16_t status);
|
|
|
|
#endif
|
|
|
|
|
2014-07-22 19:54:13 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-07-22 23:54:56 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_dump
|
|
|
|
*
|
|
|
|
* Description:
|
2014-07-23 03:23:05 +02:00
|
|
|
* Dump a WAV file header.
|
2014-07-22 23:54:56 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_PCM_DEBUG
|
|
|
|
static void pcm_dump(FAR const struct wav_header_s *wav)
|
|
|
|
{
|
2016-06-16 20:33:32 +02:00
|
|
|
_info("Wave file header\n");
|
|
|
|
_info(" Header Chunk:\n");
|
2020-11-27 06:23:23 +01:00
|
|
|
_info(" Chunk ID: 0x%08" PRIx32 "\n", wav->hdr.chunkid);
|
|
|
|
_info(" Chunk Size: %" PRIu32 "\n", wav->hdr.chunklen);
|
|
|
|
_info(" Format: 0x%08" PRIx32 "\n", wav->hdr.format);
|
2016-06-16 20:33:32 +02:00
|
|
|
_info(" Format Chunk:\n");
|
2020-11-27 06:23:23 +01:00
|
|
|
_info(" Chunk ID: 0x%08" PRIx32 "\n", wav->fmt.chunkid);
|
|
|
|
_info(" Chunk Size: %" PRIu32 "\n", wav->fmt.chunklen);
|
|
|
|
_info(" Audio Format: 0x%04x\n", wav->fmt.format);
|
|
|
|
_info(" Num. Channels: %d\n", wav->fmt.nchannels);
|
|
|
|
_info(" Sample Rate: %" PRIu32 "\n", wav->fmt.samprate);
|
|
|
|
_info(" Byte Rate: %" PRIu32 "\n", wav->fmt.byterate);
|
|
|
|
_info(" Block Align: %d\n", wav->fmt.align);
|
|
|
|
_info(" Bits Per Sample: %d\n", wav->fmt.bpsamp);
|
2016-06-16 20:33:32 +02:00
|
|
|
_info(" Data Chunk:\n");
|
2020-11-27 06:23:23 +01:00
|
|
|
_info(" Chunk ID: 0x%08" PRIx32 "\n", wav->data.chunkid);
|
|
|
|
_info(" Chunk Size: %" PRIu32 "\n", wav->data.chunklen);
|
2014-07-23 03:23:05 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_leuint16
|
|
|
|
*
|
|
|
|
* Description:
|
2021-04-18 12:05:12 +02:00
|
|
|
* Get a 16-bit value stored in little endian order. Unaligned address is
|
|
|
|
* acceptable.
|
2014-07-23 03:23:05 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-18 12:05:12 +02:00
|
|
|
static uint16_t pcm_leuint16(FAR const uint16_t *ptr)
|
2014-07-23 03:23:05 +02:00
|
|
|
{
|
2021-04-18 12:05:12 +02:00
|
|
|
FAR const uint8_t *p = (FAR const uint8_t *)ptr;
|
|
|
|
|
|
|
|
return ((p[0] << 0) |
|
|
|
|
(p[1] << 8));
|
2014-07-23 03:23:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2021-04-18 12:05:12 +02:00
|
|
|
* Name: pcm_leuint32
|
2014-07-23 03:23:05 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2021-04-18 12:05:12 +02:00
|
|
|
* Get a 32-bit value stored in little endian order. Unaligned address is
|
|
|
|
* acceptable.
|
2014-07-23 03:23:05 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-18 12:05:12 +02:00
|
|
|
static uint32_t pcm_leuint32(FAR const uint32_t *ptr)
|
2014-07-23 03:23:05 +02:00
|
|
|
{
|
2021-04-18 12:05:12 +02:00
|
|
|
FAR const uint8_t *p = (FAR const uint8_t *)ptr;
|
|
|
|
|
|
|
|
return ((p[0] << 0) |
|
|
|
|
(p[1] << 8) |
|
|
|
|
(p[2] << 16) |
|
|
|
|
(p[3] << 24));
|
2014-07-22 23:54:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_validwav
|
|
|
|
*
|
|
|
|
* Description:
|
2014-07-23 03:23:05 +02:00
|
|
|
* Return true if this is a valid WAV file header
|
2014-07-22 23:54:56 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
2020-10-07 10:12:55 +02:00
|
|
|
#ifndef CONFIG_AUDIO_FORMAT_RAW
|
2014-07-22 23:54:56 +02:00
|
|
|
static inline bool pcm_validwav(FAR const struct wav_header_s *wav)
|
|
|
|
{
|
2014-07-23 19:58:43 +02:00
|
|
|
return (wav->hdr.chunkid == WAV_HDR_CHUNKID &&
|
|
|
|
wav->hdr.format == WAV_HDR_FORMAT &&
|
|
|
|
wav->fmt.chunkid == WAV_FMT_CHUNKID &&
|
2014-07-23 03:23:05 +02:00
|
|
|
wav->fmt.chunklen == WAV_FMT_CHUNKLEN &&
|
2014-07-23 19:58:43 +02:00
|
|
|
wav->fmt.format == WAV_FMT_FORMAT &&
|
|
|
|
wav->fmt.nchannels < 256 &&
|
|
|
|
wav->fmt.align < 256 &&
|
2021-04-18 12:05:12 +02:00
|
|
|
wav->fmt.bpsamp < 256);
|
2014-07-23 03:23:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_parsewav
|
|
|
|
*
|
|
|
|
* Description:
|
2014-08-05 18:06:05 +02:00
|
|
|
* Parse and verify the WAV file header. A WAV file is a particular
|
|
|
|
* packaging of an audio file; on PCM encoded WAV files are accepted by
|
|
|
|
* this driver.
|
2014-07-23 03:23:05 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-18 12:05:12 +02:00
|
|
|
static ssize_t pcm_parsewav(FAR struct pcm_decode_s *priv, uint8_t *data,
|
|
|
|
apb_samp_t len)
|
2014-07-23 03:23:05 +02:00
|
|
|
{
|
|
|
|
FAR const struct wav_header_s *wav = (FAR const struct wav_header_s *)data;
|
2021-04-18 12:05:12 +02:00
|
|
|
FAR const struct wav_datachunk_s *dchunk;
|
2014-07-23 19:58:43 +02:00
|
|
|
struct wav_header_s localwav;
|
2021-04-18 12:05:12 +02:00
|
|
|
size_t ret = sizeof(struct wav_header_s);
|
|
|
|
|
|
|
|
if (len < sizeof(struct wav_header_s))
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2014-07-23 03:23:05 +02:00
|
|
|
|
2014-07-23 19:58:43 +02:00
|
|
|
/* Transfer the purported WAV file header into our stack storage,
|
2014-07-23 03:23:05 +02:00
|
|
|
* correcting for endian issues as needed.
|
|
|
|
*/
|
|
|
|
|
2021-04-18 12:05:12 +02:00
|
|
|
localwav.hdr.chunkid = pcm_leuint32(&wav->hdr.chunkid);
|
|
|
|
localwav.hdr.chunklen = pcm_leuint32(&wav->hdr.chunklen);
|
|
|
|
localwav.hdr.format = pcm_leuint32(&wav->hdr.format);
|
|
|
|
|
|
|
|
localwav.fmt.chunkid = pcm_leuint32(&wav->fmt.chunkid);
|
|
|
|
localwav.fmt.chunklen = pcm_leuint32(&wav->fmt.chunklen);
|
|
|
|
localwav.fmt.format = pcm_leuint16(&wav->fmt.format);
|
|
|
|
localwav.fmt.nchannels = pcm_leuint16(&wav->fmt.nchannels);
|
|
|
|
localwav.fmt.samprate = pcm_leuint32(&wav->fmt.samprate);
|
|
|
|
localwav.fmt.byterate = pcm_leuint32(&wav->fmt.byterate);
|
|
|
|
localwav.fmt.align = pcm_leuint16(&wav->fmt.align);
|
|
|
|
localwav.fmt.bpsamp = pcm_leuint16(&wav->fmt.bpsamp);
|
2014-07-23 03:23:05 +02:00
|
|
|
|
2021-04-18 12:05:12 +02:00
|
|
|
/* Find the data chunk */
|
2014-07-23 03:23:05 +02:00
|
|
|
|
2021-04-18 12:05:12 +02:00
|
|
|
dchunk = &wav->data;
|
|
|
|
|
|
|
|
for (; ; )
|
|
|
|
{
|
|
|
|
/* NOTE: The data chunk is possible to be not word-aligned if extra
|
|
|
|
* chunks exist before it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
localwav.data.chunkid = pcm_leuint32(&dchunk->chunkid);
|
|
|
|
localwav.data.chunklen = pcm_leuint32(&dchunk->chunklen);
|
|
|
|
|
|
|
|
if (localwav.data.chunkid == WAV_DATA_CHUNKID)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not data chunk. Skip it. */
|
|
|
|
|
|
|
|
ret += localwav.data.chunklen + 8;
|
|
|
|
if (ret >= len)
|
|
|
|
{
|
|
|
|
/* Data chunk not found */
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dchunk = (FAR const struct wav_datachunk_s *)
|
|
|
|
((uintptr_t)dchunk + localwav.data.chunklen + 8);
|
|
|
|
}
|
2014-07-23 03:23:05 +02:00
|
|
|
|
|
|
|
/* Dump the converted wave header information */
|
|
|
|
|
2014-07-23 19:58:43 +02:00
|
|
|
pcm_dump(&localwav);
|
|
|
|
|
2014-08-05 18:06:05 +02:00
|
|
|
/* Check if the file is a valid PCM WAV header */
|
2014-07-23 19:58:43 +02:00
|
|
|
|
2021-04-18 12:05:12 +02:00
|
|
|
if (!pcm_validwav(&localwav))
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
2014-07-23 19:58:43 +02:00
|
|
|
{
|
|
|
|
/* Yes... pick off the relevant format values and save then in the
|
|
|
|
* device structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->samprate = localwav.fmt.samprate; /* 8000, 44100, ... */
|
|
|
|
priv->byterate = localwav.fmt.byterate; /* samprate * nchannels * bpsamp / 8 */
|
|
|
|
priv->align = localwav.fmt.align; /* nchannels * bpsamp / 8 */
|
|
|
|
priv->bpsamp = localwav.fmt.bpsamp; /* Bits per sample: 8 bits = 8, 16 bits = 16 */
|
|
|
|
priv->nchannels = localwav.fmt.nchannels; /* Mono=1, Stereo=2 */
|
2014-07-28 02:33:17 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_FFORWARD
|
|
|
|
/* We are going to subsample, there then are some restrictions on the
|
|
|
|
* number of channels and sample sizes that we can handle.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->bpsamp != 8 && priv->bpsamp != 16)
|
|
|
|
{
|
2021-02-25 13:48:46 +01:00
|
|
|
auderr("ERROR: %d bits per sample are not supported in this "
|
|
|
|
"mode\n",
|
2014-07-28 02:33:17 +02:00
|
|
|
priv->bpsamp);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->nchannels != 1 && priv->nchannels != 2)
|
|
|
|
{
|
2020-04-21 09:45:24 +02:00
|
|
|
auderr("ERROR: %d channels are not supported in this mode\n",
|
2014-07-28 02:33:17 +02:00
|
|
|
priv->nchannels);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(priv->align == priv->nchannels * priv->bpsamp / 8);
|
|
|
|
#endif
|
2014-07-23 19:58:43 +02:00
|
|
|
}
|
2014-07-23 03:23:05 +02:00
|
|
|
|
2017-05-11 21:35:56 +02:00
|
|
|
/* And return true if the file is a valid WAV header file */
|
2014-07-23 03:23:05 +02:00
|
|
|
|
2014-07-23 19:58:43 +02:00
|
|
|
return ret;
|
2014-07-22 23:54:56 +02:00
|
|
|
}
|
2020-10-07 10:12:55 +02:00
|
|
|
#endif
|
2014-07-22 23:54:56 +02:00
|
|
|
|
2014-07-28 02:33:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_subsample_configure
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Configure to perform sub-sampling (or not) on the following audio
|
|
|
|
* buffers.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_FFORWARD
|
2014-07-28 05:42:10 +02:00
|
|
|
static void pcm_subsample_configure(FAR struct pcm_decode_s *priv,
|
|
|
|
uint8_t subsample)
|
2014-07-28 02:33:17 +02:00
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("subsample: %d\n", subsample);
|
2014-07-28 02:33:17 +02:00
|
|
|
|
|
|
|
/* Three possibilities:
|
|
|
|
*
|
|
|
|
* 1. We were playing normally and we have been requested to begin fast
|
|
|
|
* forwarding.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->subsample == AUDIO_SUBSAMPLE_NONE)
|
|
|
|
{
|
2014-11-25 20:15:09 +01:00
|
|
|
/* Ignore request to stop fast forwarding if we are already playing
|
|
|
|
* normally.
|
2014-07-28 02:33:17 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (subsample != AUDIO_SUBSAMPLE_NONE)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Start sub-sampling\n");
|
2014-07-28 02:33:17 +02:00
|
|
|
|
|
|
|
/* Save the current subsample setting. Subsampling will begin on
|
|
|
|
* then next audio buffer that we receive.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->npartial = 0;
|
|
|
|
priv->skip = 0;
|
|
|
|
priv->subsample = subsample;
|
|
|
|
}
|
|
|
|
}
|
2014-07-28 03:32:07 +02:00
|
|
|
|
2014-11-25 20:15:09 +01:00
|
|
|
/* 2. We were already fast forwarding and we have been asked to return to
|
2014-07-28 02:33:17 +02:00
|
|
|
* normal play.
|
|
|
|
*/
|
|
|
|
|
2014-11-25 20:15:09 +01:00
|
|
|
else if (subsample == AUDIO_SUBSAMPLE_NONE)
|
2014-07-28 02:33:17 +02:00
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Stop sub-sampling\n");
|
2014-07-28 02:33:17 +02:00
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
/* Indicate that we are in normal play mode. This will take effect
|
|
|
|
* when the next audio buffer is received.
|
2014-07-28 02:33:17 +02:00
|
|
|
*/
|
2014-07-28 03:32:07 +02:00
|
|
|
|
2014-07-28 02:33:17 +02:00
|
|
|
priv->npartial = 0;
|
|
|
|
priv->skip = 0;
|
|
|
|
priv->subsample = AUDIO_SUBSAMPLE_NONE;
|
|
|
|
}
|
2014-11-25 20:15:09 +01:00
|
|
|
|
|
|
|
/* 3. Were already fast forwarding and we have been asked to change the
|
|
|
|
* sub-sampling rate.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (priv->subsample != subsample)
|
|
|
|
{
|
|
|
|
/* Just save the new subsample setting. It will take effect on the
|
|
|
|
* next audio buffer that we receive.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->subsample = subsample;
|
|
|
|
}
|
2014-07-28 02:33:17 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_subsample
|
|
|
|
*
|
|
|
|
* Description:
|
2014-07-28 05:42:10 +02:00
|
|
|
* Given a newly received audio buffer, perform sub-sampling in-place in
|
|
|
|
* the audio buffer. Since the sub-sampled data will always be smaller
|
|
|
|
* than the original buffer, no additional buffering should be necessary.
|
2014-07-28 02:33:17 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_FFORWARD
|
2014-07-28 05:42:10 +02:00
|
|
|
static void pcm_subsample(FAR struct pcm_decode_s *priv,
|
|
|
|
FAR struct ap_buffer_s *apb)
|
2014-07-28 02:33:17 +02:00
|
|
|
{
|
|
|
|
FAR const uint8_t *src;
|
|
|
|
FAR uint8_t *dest;
|
|
|
|
unsigned int destsize;
|
|
|
|
unsigned int srcsize;
|
|
|
|
unsigned int skipsize;
|
2014-07-28 05:42:10 +02:00
|
|
|
unsigned int copysize;
|
2014-07-28 02:33:17 +02:00
|
|
|
unsigned int i;
|
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
/* Are we sub-sampling? */
|
2014-07-28 02:33:17 +02:00
|
|
|
|
|
|
|
if (priv->subsample == AUDIO_SUBSAMPLE_NONE)
|
|
|
|
{
|
|
|
|
/* No.. do nothing to the buffer */
|
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
return;
|
2014-07-28 02:33:17 +02:00
|
|
|
}
|
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
/* Yes.. we will need to subsample the newly received buffer in-place by
|
|
|
|
* copying from the upper end of the buffer to the lower end.
|
|
|
|
*/
|
2014-07-28 02:33:17 +02:00
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
src = &apb->samp[apb->curbyte];
|
|
|
|
dest = apb->samp;
|
2014-07-28 02:33:17 +02:00
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
srcsize = apb->nbytes - apb->curbyte;
|
|
|
|
destsize = apb->nmaxbytes;
|
2014-07-28 02:33:17 +02:00
|
|
|
|
|
|
|
/* This is the number of bytes that we need to skip between samples */
|
|
|
|
|
|
|
|
skipsize = priv->align * (priv->subsample - 1);
|
2014-07-28 03:32:07 +02:00
|
|
|
|
2014-07-28 05:00:19 +02:00
|
|
|
/* Let's deal with any partial samples from the last buffer */
|
2014-07-28 02:33:17 +02:00
|
|
|
|
|
|
|
if (priv->npartial > 0)
|
|
|
|
{
|
|
|
|
/* Let's get an impossible corner case out of the way. What if we
|
|
|
|
* received a tiny audio buffer. So small, that it (plus any previous
|
|
|
|
* sample) is smaller than one sample.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->npartial + srcsize < priv->align)
|
|
|
|
{
|
2014-07-28 05:42:10 +02:00
|
|
|
/* Update the partial sample size and return the unmodified
|
|
|
|
* buffer.
|
|
|
|
*/
|
2014-07-28 02:33:17 +02:00
|
|
|
|
|
|
|
priv->npartial += srcsize;
|
2014-07-28 05:42:10 +02:00
|
|
|
return;
|
2014-07-28 02:33:17 +02:00
|
|
|
}
|
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
/* We do at least have enough to complete the sample. If this data
|
|
|
|
* does not resides at the correct position at the from of the audio
|
|
|
|
* buffer, then we will need to copy it.
|
2014-07-28 05:00:19 +02:00
|
|
|
*/
|
2014-07-28 02:33:17 +02:00
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
copysize = priv->align - priv->npartial;
|
|
|
|
if (apb->curbyte > 0)
|
2014-07-28 02:33:17 +02:00
|
|
|
{
|
2014-07-28 05:42:10 +02:00
|
|
|
/* We have to copy down */
|
2014-07-28 02:33:17 +02:00
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
for (i = 0; i < copysize; i++)
|
|
|
|
{
|
|
|
|
*dest++ = *src++;
|
|
|
|
}
|
|
|
|
}
|
2014-07-28 05:47:25 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If the data is already position at the beginning of the audio
|
|
|
|
* buffer, then just increment the buffer pointers around the
|
|
|
|
* data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
src += copysize;
|
|
|
|
dest += copysize;
|
|
|
|
}
|
2014-07-28 05:00:19 +02:00
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
/* Update the number of bytes in the working buffer and reset the
|
|
|
|
* skip value
|
2014-07-28 02:33:17 +02:00
|
|
|
*/
|
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
priv->npartial = 0;
|
|
|
|
srcsize -= copysize;
|
|
|
|
destsize -= copysize;
|
|
|
|
priv->skip = skipsize;
|
2014-07-28 02:33:17 +02:00
|
|
|
}
|
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
/* Now loop until either the entire audio buffer has been sub-sampling.
|
|
|
|
* This copy in place works because we know that the sub-sampled data
|
|
|
|
* will always be smaller than the original data.
|
2014-07-28 02:33:17 +02:00
|
|
|
*/
|
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
while (srcsize > 0)
|
2014-07-28 02:33:17 +02:00
|
|
|
{
|
|
|
|
/* Do we need to skip ahead in the buffer? */
|
|
|
|
|
|
|
|
if (priv->skip > 0)
|
|
|
|
{
|
|
|
|
/* How much can we skip in this buffer? Depends on the smaller
|
|
|
|
* of (1) the number of bytes that we need to skip and (2) the
|
|
|
|
* number of bytes available in the newly received audio buffer.
|
|
|
|
*/
|
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
copysize = MIN(priv->skip, srcsize);
|
2014-07-28 02:33:17 +02:00
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
priv->skip -= copysize;
|
|
|
|
src += copysize;
|
|
|
|
srcsize -= copysize;
|
2014-07-28 02:33:17 +02:00
|
|
|
}
|
|
|
|
|
2014-07-28 05:00:19 +02:00
|
|
|
/* Copy the sample from the audio buffer into the working buffer. */
|
2014-07-28 02:33:17 +02:00
|
|
|
|
2014-07-28 05:00:19 +02:00
|
|
|
else
|
2014-07-28 02:33:17 +02:00
|
|
|
{
|
2014-07-28 05:00:19 +02:00
|
|
|
/* Do we have space for the whole sample? */
|
2014-07-28 02:33:17 +02:00
|
|
|
|
2014-07-28 05:00:19 +02:00
|
|
|
if (srcsize < priv->align)
|
|
|
|
{
|
|
|
|
/* No.. this is a partial copy */
|
2014-07-28 02:33:17 +02:00
|
|
|
|
2014-07-28 05:00:19 +02:00
|
|
|
copysize = srcsize;
|
|
|
|
priv->npartial = srcsize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-28 05:42:10 +02:00
|
|
|
/* Copy the whole sample and re-arm the skip size */
|
|
|
|
|
2014-07-28 05:00:19 +02:00
|
|
|
copysize = priv->align;
|
|
|
|
priv->skip = skipsize;
|
|
|
|
}
|
2014-07-28 02:33:17 +02:00
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
/* Now copy the sample from the end of audio buffer
|
|
|
|
* to the beginning.
|
|
|
|
*/
|
2014-07-28 02:33:17 +02:00
|
|
|
|
2014-07-28 05:00:19 +02:00
|
|
|
for (i = 0; i < copysize; i++)
|
2014-07-28 02:33:17 +02:00
|
|
|
{
|
|
|
|
*dest++ = *src++;
|
|
|
|
}
|
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
/* Updates bytes available in the source buffer and bytes
|
|
|
|
* remaining in the destination buffer.
|
|
|
|
*/
|
2014-07-28 02:33:17 +02:00
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
srcsize -= copysize;
|
|
|
|
destsize -= copysize;
|
2014-07-28 02:33:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
/* Update the size and offset data in the audio buffer */
|
2014-07-28 02:33:17 +02:00
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
apb->curbyte = 0;
|
|
|
|
apb->nbytes = apb->nmaxbytes - destsize;
|
2014-07-28 02:33:17 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-07-22 19:54:13 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_getcaps
|
|
|
|
*
|
|
|
|
* Description:
|
2014-07-23 03:23:05 +02:00
|
|
|
* This method is called to retrieve the lower-half device capabilities.
|
|
|
|
* It will be called with device type AUDIO_TYPE_QUERY to request the
|
2020-04-21 09:45:24 +02:00
|
|
|
* overall capabilities, such as to determine the types of devices
|
|
|
|
* supported audio formats supported, etc.
|
|
|
|
* Then it may be called once or more with reported supported device types
|
|
|
|
* to determine the specific capabilities of that device type
|
|
|
|
* (such as MP3 encoder, WMA encoder, PCM output, etc.).
|
2014-07-22 19:54:13 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pcm_getcaps(FAR struct audio_lowerhalf_s *dev, int type,
|
|
|
|
FAR struct audio_caps_s *caps)
|
|
|
|
{
|
|
|
|
FAR struct pcm_decode_s *priv = (FAR struct pcm_decode_s *)dev;
|
2014-07-23 03:23:05 +02:00
|
|
|
FAR struct audio_lowerhalf_s *lower;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Defer the operation to the lower device driver */
|
|
|
|
|
|
|
|
lower = priv->lower;
|
|
|
|
DEBUGASSERT(lower && lower->ops->getcaps);
|
|
|
|
|
|
|
|
/* Get the capabilities of the lower-level driver */
|
|
|
|
|
|
|
|
ret = lower->ops->getcaps(lower, type, caps);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 17:46:23 +02:00
|
|
|
auderr("ERROR: Lower getcaps() failed: %d\n", ret);
|
2014-07-23 03:23:05 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
/* Modify the capabilities reported by the lower driver:
|
|
|
|
* PCM is the only supported format that we will report,
|
|
|
|
* regardless of what the lower driver reported.
|
2014-07-23 03:23:05 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (caps->ac_subtype == AUDIO_TYPE_QUERY)
|
|
|
|
{
|
2014-07-24 18:21:04 +02:00
|
|
|
caps->ac_format.hw = (1 << (AUDIO_FMT_PCM - 1));
|
2014-07-23 03:23:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return caps->ac_len;
|
2014-07-22 19:54:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_configure
|
|
|
|
*
|
|
|
|
* Description:
|
2014-07-23 03:23:05 +02:00
|
|
|
* This method is called to bind the lower-level driver to the upper-level
|
|
|
|
* driver and to configure the driver for a specific mode of
|
|
|
|
* operation defined by the parameters selected in supplied device caps
|
|
|
|
* structure. The lower-level device should perform any initialization
|
|
|
|
* needed to prepare for operations in the specified mode. It should not,
|
|
|
|
* however, process any audio data until the start method is called.
|
2014-07-22 19:54:13 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
static int pcm_configure(FAR struct audio_lowerhalf_s *dev,
|
2020-04-21 09:45:24 +02:00
|
|
|
FAR void *session,
|
|
|
|
FAR const struct audio_caps_s *caps)
|
2014-07-22 19:54:13 +02:00
|
|
|
#else
|
|
|
|
static int pcm_configure(FAR struct audio_lowerhalf_s *dev,
|
|
|
|
FAR const struct audio_caps_s *caps)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
FAR struct pcm_decode_s *priv = (FAR struct pcm_decode_s *)dev;
|
2014-07-23 19:58:43 +02:00
|
|
|
FAR struct audio_lowerhalf_s *lower;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
2014-07-28 02:33:17 +02:00
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_FFORWARD
|
|
|
|
/* Pick off commands to perform sub-sampling. Those are done by this
|
|
|
|
* decoder. All of configuration settings are handled by the lower level
|
|
|
|
* audio driver.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (caps->ac_type == AUDIO_TYPE_PROCESSING &&
|
|
|
|
caps->ac_format.hw == AUDIO_PU_SUBSAMPLE_FORWARD)
|
|
|
|
{
|
2014-07-28 05:42:10 +02:00
|
|
|
/* Configure sub-sampling and return to avoid forwarding the
|
|
|
|
* configuration to the lower level
|
2014-07-28 02:33:17 +02:00
|
|
|
* driver.
|
|
|
|
*/
|
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
pcm_subsample_configure(priv, caps->ac_controls.b[0]);
|
|
|
|
return OK;
|
2014-07-28 02:33:17 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Defer all other operations to the lower device driver */
|
2014-07-23 19:58:43 +02:00
|
|
|
|
|
|
|
lower = priv->lower;
|
|
|
|
DEBUGASSERT(lower && lower->ops->configure);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Defer to lower configure\n");
|
2014-07-23 19:58:43 +02:00
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
return lower->ops->configure(lower, session, caps);
|
|
|
|
#else
|
|
|
|
return lower->ops->configure(lower, caps);
|
|
|
|
#endif
|
2014-07-22 19:54:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_shutdown
|
|
|
|
*
|
|
|
|
* Description:
|
2014-07-23 03:23:05 +02:00
|
|
|
* This method is called when the driver is closed. The lower half driver
|
|
|
|
* should stop processing audio data, including terminating any active
|
|
|
|
* output generation. It should also disable the audio hardware and put
|
|
|
|
* it into the lowest possible power usage state.
|
2014-07-22 19:54:13 +02:00
|
|
|
*
|
2020-04-21 09:45:24 +02:00
|
|
|
* Any enqueued Audio Pipeline Buffers that have not been
|
|
|
|
* processed / dequeued should be dequeued by this function.
|
2014-07-22 19:54:13 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pcm_shutdown(FAR struct audio_lowerhalf_s *dev)
|
|
|
|
{
|
|
|
|
FAR struct pcm_decode_s *priv = (FAR struct pcm_decode_s *)dev;
|
2014-07-23 19:58:43 +02:00
|
|
|
FAR struct audio_lowerhalf_s *lower;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
2014-08-02 22:27:12 +02:00
|
|
|
/* We are no longer streaming audio */
|
|
|
|
|
|
|
|
priv->streaming = false;
|
|
|
|
|
2014-07-23 19:58:43 +02:00
|
|
|
/* Defer the operation to the lower device driver */
|
|
|
|
|
|
|
|
lower = priv->lower;
|
|
|
|
DEBUGASSERT(lower && lower->ops->start);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Defer to lower shutdown\n");
|
2014-07-23 19:58:43 +02:00
|
|
|
return lower->ops->shutdown(lower);
|
2014-07-22 19:54:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_start
|
|
|
|
*
|
|
|
|
* Description:
|
2020-04-21 09:45:24 +02:00
|
|
|
* Start audio streaming in the configured mode.
|
|
|
|
* For input and synthesis devices, this means it should begin sending
|
|
|
|
* streaming audio data. For output or processing type device, it means
|
|
|
|
* it should begin processing of any enqueued Audio Pipeline Buffers.
|
2014-07-22 19:54:13 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
static int pcm_start(FAR struct audio_lowerhalf_s *dev, FAR void *session)
|
|
|
|
#else
|
|
|
|
static int pcm_start(FAR struct audio_lowerhalf_s *dev)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
FAR struct pcm_decode_s *priv = (FAR struct pcm_decode_s *)dev;
|
2014-07-23 19:58:43 +02:00
|
|
|
FAR struct audio_lowerhalf_s *lower;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Defer the operation to the lower device driver */
|
|
|
|
|
|
|
|
lower = priv->lower;
|
|
|
|
DEBUGASSERT(lower && lower->ops->start);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Defer to lower start\n");
|
2014-07-23 19:58:43 +02:00
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
return lower->ops->start(lower, session);
|
|
|
|
#else
|
|
|
|
return lower->ops->start(lower);
|
|
|
|
#endif
|
2014-07-22 19:54:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_stop
|
|
|
|
*
|
|
|
|
* Description:
|
2014-07-23 03:23:05 +02:00
|
|
|
* Stop audio streaming and/or processing of enqueued Audio Pipeline
|
|
|
|
* Buffers
|
2014-07-22 19:54:13 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_STOP
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
static int pcm_stop(FAR struct audio_lowerhalf_s *dev, FAR void *session)
|
|
|
|
#else
|
|
|
|
static int pcm_stop(FAR struct audio_lowerhalf_s *dev)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
FAR struct pcm_decode_s *priv = (FAR struct pcm_decode_s *)dev;
|
2014-07-23 19:58:43 +02:00
|
|
|
FAR struct audio_lowerhalf_s *lower;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
2014-08-02 22:27:12 +02:00
|
|
|
/* We are no longer streaming */
|
|
|
|
|
|
|
|
priv->streaming = false;
|
|
|
|
|
2014-07-23 19:58:43 +02:00
|
|
|
/* Defer the operation to the lower device driver */
|
|
|
|
|
|
|
|
lower = priv->lower;
|
|
|
|
DEBUGASSERT(lower && lower->ops->stop);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Defer to lower stop\n");
|
2014-07-23 19:58:43 +02:00
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
return lower->ops->stop(lower, session);
|
|
|
|
#else
|
|
|
|
return lower->ops->stop(lower);
|
|
|
|
#endif
|
2014-07-22 19:54:13 +02:00
|
|
|
}
|
2020-06-05 17:15:24 +02:00
|
|
|
#endif /* CONFIG_AUDIO_EXCLUDE_STOP */
|
2014-07-22 19:54:13 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_pause
|
|
|
|
*
|
|
|
|
* Description:
|
2020-04-21 09:45:24 +02:00
|
|
|
* Pause the audio stream.
|
|
|
|
* Should keep current playback context active in case a resume is issued.
|
|
|
|
* Could be called and then followed by a stop.
|
2014-07-22 19:54:13 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
static int pcm_pause(FAR struct audio_lowerhalf_s *dev, FAR void *session)
|
|
|
|
#else
|
|
|
|
static int pcm_pause(FAR struct audio_lowerhalf_s *dev)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
FAR struct pcm_decode_s *priv = (FAR struct pcm_decode_s *)dev;
|
2014-07-23 19:58:43 +02:00
|
|
|
FAR struct audio_lowerhalf_s *lower;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Defer the operation to the lower device driver */
|
|
|
|
|
|
|
|
lower = priv->lower;
|
|
|
|
DEBUGASSERT(lower && lower->ops->pause);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Defer to lower pause\n");
|
2014-07-23 19:58:43 +02:00
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
return lower->ops->pause(lower, session);
|
|
|
|
#else
|
|
|
|
return lower->ops->pause(lower);
|
|
|
|
#endif
|
2014-07-22 19:54:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_resume
|
|
|
|
*
|
|
|
|
* Description:
|
2014-07-23 03:23:05 +02:00
|
|
|
* Resumes audio streaming after a pause.
|
2014-07-22 19:54:13 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
static int pcm_resume(FAR struct audio_lowerhalf_s *dev, FAR void *session)
|
|
|
|
#else
|
|
|
|
static int pcm_resume(FAR struct audio_lowerhalf_s *dev)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
FAR struct pcm_decode_s *priv = (FAR struct pcm_decode_s *)dev;
|
2014-07-23 19:58:43 +02:00
|
|
|
FAR struct audio_lowerhalf_s *lower;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Defer the operation to the lower device driver */
|
|
|
|
|
|
|
|
lower = priv->lower;
|
|
|
|
DEBUGASSERT(lower && lower->ops->resume);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Defer to lower resume\n");
|
2014-07-23 19:58:43 +02:00
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
return lower->ops->resume(lower, session);
|
|
|
|
#else
|
|
|
|
return lower->ops->resume(lower);
|
|
|
|
#endif
|
2014-07-22 19:54:13 +02:00
|
|
|
}
|
2020-06-05 17:15:24 +02:00
|
|
|
#endif /* CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME */
|
2014-07-22 19:54:13 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_allocbuffer
|
|
|
|
*
|
|
|
|
* Description:
|
2014-07-23 03:23:05 +02:00
|
|
|
* Allocate an audio pipeline buffer. This routine provides the
|
|
|
|
* lower-half driver with the opportunity to perform special buffer
|
|
|
|
* allocation if needed, such as allocating from a specific memory
|
|
|
|
* region (DMA-able, etc.). If not supplied, then the top-half
|
2014-09-01 00:24:24 +02:00
|
|
|
* driver will perform a standard kumm_malloc using normal user-space
|
2014-07-23 03:23:05 +02:00
|
|
|
* memory region.
|
2014-07-22 19:54:13 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pcm_allocbuffer(FAR struct audio_lowerhalf_s *dev,
|
|
|
|
FAR struct audio_buf_desc_s *apb)
|
|
|
|
{
|
|
|
|
FAR struct pcm_decode_s *priv = (FAR struct pcm_decode_s *)dev;
|
|
|
|
FAR struct audio_lowerhalf_s *lower;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Defer the operation to the lower device driver */
|
|
|
|
|
|
|
|
lower = priv->lower;
|
|
|
|
DEBUGASSERT(lower && lower->ops->allocbuffer);
|
2014-07-23 03:23:05 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Defer to lower allocbuffer\n");
|
2014-07-22 19:54:13 +02:00
|
|
|
return lower->ops->allocbuffer(lower, apb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_freebuffer
|
|
|
|
*
|
|
|
|
* Description:
|
2014-07-23 03:23:05 +02:00
|
|
|
* Free an audio pipeline buffer. If the lower-level driver provides an
|
|
|
|
* allocbuffer routine, it should also provide the freebuffer routine to
|
|
|
|
* perform the free operation.
|
2014-07-22 19:54:13 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pcm_freebuffer(FAR struct audio_lowerhalf_s *dev,
|
|
|
|
FAR struct audio_buf_desc_s *apb)
|
|
|
|
{
|
|
|
|
FAR struct pcm_decode_s *priv = (FAR struct pcm_decode_s *)dev;
|
|
|
|
FAR struct audio_lowerhalf_s *lower;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Defer the operation to the lower device driver */
|
|
|
|
|
|
|
|
lower = priv->lower;
|
|
|
|
DEBUGASSERT(lower && lower->ops->freebuffer);
|
2014-07-23 03:23:05 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Defer to lower freebuffer, apb=%p\n", apb);
|
2014-07-22 19:54:13 +02:00
|
|
|
return lower->ops->freebuffer(lower, apb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_enqueuebuffer
|
|
|
|
*
|
|
|
|
* Description:
|
2014-07-23 03:23:05 +02:00
|
|
|
* Enqueue a buffer for processing. This is a non-blocking enqueue
|
|
|
|
* operation. If the lower-half driver's buffer queue is full, then it
|
|
|
|
* should return an error code of -ENOMEM, and the upper-half driver can
|
|
|
|
* decide to either block the calling thread or deal with it in a non-
|
|
|
|
* blocking manner.
|
|
|
|
*
|
|
|
|
* For each call to enqueuebuffer, the lower-half driver must call
|
|
|
|
* audio_dequeuebuffer when it is finished processing the bufferr, passing
|
|
|
|
* the previously enqueued apb and a dequeue status so that the upper-half
|
|
|
|
* driver can decide if a waiting thread needs to be release, if the
|
|
|
|
* dequeued buffer should be passed to the next block in the Audio
|
|
|
|
* Pipeline, etc.
|
2014-07-22 19:54:13 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pcm_enqueuebuffer(FAR struct audio_lowerhalf_s *dev,
|
|
|
|
FAR struct ap_buffer_s *apb)
|
|
|
|
{
|
|
|
|
FAR struct pcm_decode_s *priv = (FAR struct pcm_decode_s *)dev;
|
2014-07-23 03:23:05 +02:00
|
|
|
FAR struct audio_lowerhalf_s *lower;
|
|
|
|
apb_samp_t bytesleft;
|
2021-04-18 12:05:12 +02:00
|
|
|
ssize_t headersize;
|
2014-07-23 20:21:04 +02:00
|
|
|
int ret;
|
2014-07-23 03:23:05 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Received buffer %p, streaming=%d\n", apb, priv->streaming);
|
2014-07-23 03:23:05 +02:00
|
|
|
|
|
|
|
lower = priv->lower;
|
2014-07-24 17:56:39 +02:00
|
|
|
DEBUGASSERT(lower && lower->ops->enqueuebuffer && lower->ops->configure);
|
2014-07-23 03:23:05 +02:00
|
|
|
|
|
|
|
/* Are we streaming yet? */
|
|
|
|
|
|
|
|
if (priv->streaming)
|
|
|
|
{
|
2014-07-28 02:33:17 +02:00
|
|
|
/* Yes, we are streaming */
|
2020-04-21 09:45:24 +02:00
|
|
|
|
2014-08-02 22:27:12 +02:00
|
|
|
/* Check for the last audio buffer in the stream */
|
|
|
|
|
|
|
|
if ((apb->flags & AUDIO_APB_FINAL) != 0)
|
|
|
|
{
|
|
|
|
/* Yes.. then we are no longer streaming */
|
|
|
|
|
|
|
|
priv->streaming = false;
|
|
|
|
}
|
2014-07-23 03:23:05 +02:00
|
|
|
|
2014-07-28 02:33:17 +02:00
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_FFORWARD
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Received: apb=%p curbyte=%d nbytes=%d flags=%04x\n",
|
2014-08-05 18:06:05 +02:00
|
|
|
apb, apb->curbyte, apb->nbytes, apb->flags);
|
2014-07-23 19:58:43 +02:00
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
/* Perform any necessary sub-sampling operations */
|
2014-07-28 02:33:17 +02:00
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
pcm_subsample(priv, apb);
|
2014-07-28 02:33:17 +02:00
|
|
|
#endif
|
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
/* Then give the audio buffer to the lower driver */
|
2014-07-28 03:32:07 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Pass to lower enqueuebuffer: apb=%p curbyte=%d nbytes=%d\n",
|
2014-07-28 05:42:10 +02:00
|
|
|
apb, apb->curbyte, apb->nbytes);
|
2014-07-28 03:32:07 +02:00
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
return lower->ops->enqueuebuffer(lower, apb);
|
2014-07-23 03:23:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No.. then this must be the first buffer that we have seen (since we
|
|
|
|
* will error out out if the first buffer is smaller than the WAV file
|
|
|
|
* header. There is no attempt to reconstruct the full header from
|
|
|
|
* fragments in multiple, tiny audio buffers).
|
|
|
|
*/
|
|
|
|
|
|
|
|
bytesleft = apb->nbytes - apb->curbyte;
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("curbyte=%d nbytes=%d nmaxbytes=%d bytesleft=%d\n",
|
2014-07-23 03:23:05 +02:00
|
|
|
apb->curbyte, apb->nbytes, apb->nmaxbytes, bytesleft);
|
|
|
|
|
2020-10-07 10:12:55 +02:00
|
|
|
/* Parse and verify the candidate PCM WAV file header */
|
2014-07-23 03:23:05 +02:00
|
|
|
|
2020-10-07 10:12:55 +02:00
|
|
|
#ifndef CONFIG_AUDIO_FORMAT_RAW
|
2021-04-18 12:05:12 +02:00
|
|
|
headersize = pcm_parsewav(priv, &apb->samp[apb->curbyte], bytesleft);
|
|
|
|
if (headersize > 0)
|
2020-10-07 10:12:55 +02:00
|
|
|
{
|
|
|
|
struct audio_caps_s caps;
|
2014-07-24 17:56:39 +02:00
|
|
|
|
2020-10-07 10:12:55 +02:00
|
|
|
/* Configure the lower level for the number of channels, bitrate,
|
|
|
|
* and sample bitwidth.
|
|
|
|
*/
|
2014-07-23 20:21:04 +02:00
|
|
|
|
2020-10-07 10:12:55 +02:00
|
|
|
DEBUGASSERT(priv->samprate < 65535);
|
2014-07-23 20:21:04 +02:00
|
|
|
|
2020-10-07 10:12:55 +02:00
|
|
|
caps.ac_len = sizeof(struct audio_caps_s);
|
|
|
|
caps.ac_type = AUDIO_TYPE_OUTPUT;
|
|
|
|
caps.ac_channels = priv->nchannels;
|
2014-07-23 20:21:04 +02:00
|
|
|
|
2020-10-07 10:12:55 +02:00
|
|
|
caps.ac_controls.hw[0] = (uint16_t)priv->samprate;
|
|
|
|
caps.ac_controls.b[2] = priv->bpsamp;
|
2014-07-24 17:56:39 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
2020-10-07 10:12:55 +02:00
|
|
|
ret = lower->ops->configure(lower, priv->session, &caps);
|
2014-07-24 17:56:39 +02:00
|
|
|
#else
|
2020-10-07 10:12:55 +02:00
|
|
|
ret = lower->ops->configure(lower, &caps);
|
2014-07-24 17:56:39 +02:00
|
|
|
#endif
|
2020-10-07 10:12:55 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
auderr("ERROR: Failed to set PCM configuration: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2014-07-23 03:23:05 +02:00
|
|
|
|
2020-10-07 10:12:55 +02:00
|
|
|
/* Bump up the data offset */
|
2014-07-28 02:33:17 +02:00
|
|
|
|
2021-04-18 12:05:12 +02:00
|
|
|
apb->curbyte += headersize;
|
2020-10-07 10:12:55 +02:00
|
|
|
#endif
|
2014-07-28 02:33:17 +02:00
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_FFORWARD
|
2020-10-07 10:12:55 +02:00
|
|
|
audinfo("Begin streaming: apb=%p curbyte=%d nbytes=%d\n",
|
|
|
|
apb, apb->curbyte, apb->nbytes);
|
2014-07-23 19:58:43 +02:00
|
|
|
|
2020-10-07 10:12:55 +02:00
|
|
|
/* Perform any necessary sub-sampling operations */
|
2014-07-28 02:33:17 +02:00
|
|
|
|
2020-10-07 10:12:55 +02:00
|
|
|
pcm_subsample(priv, apb);
|
2014-07-28 02:33:17 +02:00
|
|
|
#endif
|
|
|
|
|
2020-10-07 10:12:55 +02:00
|
|
|
/* Then give the audio buffer to the lower driver */
|
2014-07-28 02:33:17 +02:00
|
|
|
|
2020-10-07 10:12:55 +02:00
|
|
|
audinfo(
|
|
|
|
"Pass to lower enqueuebuffer: apb=%p curbyte=%d nbytes=%d\n",
|
|
|
|
apb, apb->curbyte, apb->nbytes);
|
2014-07-28 03:32:07 +02:00
|
|
|
|
2020-10-07 10:12:55 +02:00
|
|
|
ret = lower->ops->enqueuebuffer(lower, apb);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Now we are streaming. Unless for some reason there is only
|
|
|
|
* one audio buffer in the audio stream. In that case, this
|
|
|
|
* will be marked as the final buffer
|
|
|
|
*/
|
2014-08-05 18:06:05 +02:00
|
|
|
|
2020-10-07 10:12:55 +02:00
|
|
|
priv->streaming = ((apb->flags & AUDIO_APB_FINAL) == 0);
|
|
|
|
return OK;
|
2014-07-23 03:23:05 +02:00
|
|
|
}
|
2014-08-02 22:27:12 +02:00
|
|
|
|
2014-08-05 18:06:05 +02:00
|
|
|
/* The normal protocol for streaming errors is as follows:
|
|
|
|
*
|
|
|
|
* (1) Fail the enqueueing by returned a negated error value. The
|
|
|
|
* upper level then knows that this buffer was not queue.
|
|
|
|
* (2) Return all queued buffers to the caller using the
|
|
|
|
* AUDIO_CALLBACK_DEQUEUE callback
|
|
|
|
* (3) Terminate playing using the AUDIO_CALLBACK_COMPLETE
|
|
|
|
* callback.
|
|
|
|
*
|
|
|
|
* In this case we fail on the very first buffer and we need only
|
|
|
|
* do (1) and (3).
|
2014-08-02 22:27:12 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
2014-08-05 18:06:05 +02:00
|
|
|
priv->export.upper(priv->export.priv, AUDIO_CALLBACK_COMPLETE,
|
|
|
|
NULL, OK, NULL);
|
2014-08-02 22:27:12 +02:00
|
|
|
#else
|
2014-08-05 18:06:05 +02:00
|
|
|
priv->export.upper(priv->export.priv, AUDIO_CALLBACK_COMPLETE,
|
|
|
|
NULL, OK);
|
2014-08-02 22:27:12 +02:00
|
|
|
#endif
|
2014-07-23 03:23:05 +02:00
|
|
|
|
2020-10-07 10:12:55 +02:00
|
|
|
#ifndef CONFIG_AUDIO_FORMAT_RAW
|
2021-05-28 08:52:28 +02:00
|
|
|
}
|
|
|
|
|
2014-07-23 03:23:05 +02:00
|
|
|
/* This is not a WAV file! */
|
|
|
|
|
2016-06-11 23:50:49 +02:00
|
|
|
auderr("ERROR: Invalid PCM WAV file\n");
|
2020-10-07 10:12:55 +02:00
|
|
|
#endif
|
2021-05-28 08:52:28 +02:00
|
|
|
|
|
|
|
return -EINVAL;
|
2014-07-22 19:54:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_cancelbuffer
|
|
|
|
*
|
|
|
|
* Description:
|
2014-07-23 03:23:05 +02:00
|
|
|
* Cancel a previously enqueued buffer.
|
2014-07-22 19:54:13 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int pcm_cancelbuffer(FAR struct audio_lowerhalf_s *dev,
|
|
|
|
FAR struct ap_buffer_s *apb)
|
|
|
|
{
|
|
|
|
FAR struct pcm_decode_s *priv = (FAR struct pcm_decode_s *)dev;
|
2014-07-23 19:58:43 +02:00
|
|
|
FAR struct audio_lowerhalf_s *lower;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Defer the operation to the lower device driver */
|
|
|
|
|
|
|
|
lower = priv->lower;
|
|
|
|
DEBUGASSERT(lower && lower->ops->cancelbuffer);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Defer to lower cancelbuffer, apb=%p\n", apb);
|
2014-07-23 19:58:43 +02:00
|
|
|
return lower->ops->cancelbuffer(lower, apb);
|
2014-07-22 19:54:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_ioctl
|
|
|
|
*
|
|
|
|
* Description:
|
2014-07-23 03:23:05 +02:00
|
|
|
* Lower-half logic may support platform-specific ioctl commands.
|
2014-07-22 19:54:13 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-07-23 03:23:05 +02:00
|
|
|
static int pcm_ioctl(FAR struct audio_lowerhalf_s *dev, int cmd,
|
|
|
|
unsigned long arg)
|
2014-07-22 19:54:13 +02:00
|
|
|
{
|
|
|
|
FAR struct pcm_decode_s *priv = (FAR struct pcm_decode_s *)dev;
|
2014-07-23 03:23:05 +02:00
|
|
|
FAR struct audio_lowerhalf_s *lower;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Defer the operation to the lower device driver */
|
|
|
|
|
|
|
|
lower = priv->lower;
|
|
|
|
DEBUGASSERT(lower && lower->ops->ioctl);
|
|
|
|
|
2020-11-27 06:23:23 +01:00
|
|
|
audinfo("Defer to lower ioctl, cmd=%d arg=%ld\n", cmd, arg);
|
2014-07-23 03:23:05 +02:00
|
|
|
return lower->ops->ioctl(lower, cmd, arg);
|
2014-07-22 19:54:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_reserve
|
|
|
|
*
|
|
|
|
* Description:
|
2014-07-23 03:23:05 +02:00
|
|
|
* Reserve a session (may only be one per device or may be multiple) for
|
|
|
|
* use by a client. Client software can open audio devices and issue
|
|
|
|
* AUDIOIOC_GETCAPS calls freely, but other operations require a
|
|
|
|
* reservation. A session reservation will assign a context that must
|
|
|
|
* be passed with
|
2014-07-22 19:54:13 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
static int pcm_reserve(FAR struct audio_lowerhalf_s *dev, FAR void **session)
|
|
|
|
#else
|
|
|
|
static int pcm_reserve(FAR struct audio_lowerhalf_s *dev)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
FAR struct pcm_decode_s *priv = (FAR struct pcm_decode_s *)dev;
|
2014-07-23 03:23:05 +02:00
|
|
|
FAR struct audio_lowerhalf_s *lower;
|
2014-07-24 17:56:39 +02:00
|
|
|
int ret;
|
2014-07-23 03:23:05 +02:00
|
|
|
|
2014-07-24 17:56:39 +02:00
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
DEBUGASSERT(priv && session);
|
|
|
|
#else
|
2014-07-23 03:23:05 +02:00
|
|
|
DEBUGASSERT(priv);
|
2014-07-24 17:56:39 +02:00
|
|
|
#endif
|
2014-07-23 03:23:05 +02:00
|
|
|
|
|
|
|
/* It is not necessary to reserve the upper half. What we really need to
|
|
|
|
* do is to reserved the lower device driver for exclusive use by the PCM
|
|
|
|
* decoder. That effectively reserves the upper PCM decoder along with
|
|
|
|
* the lower driver (which is then not available for use by other
|
|
|
|
* decoders).
|
2014-07-24 17:56:39 +02:00
|
|
|
*
|
|
|
|
* We do, however, need to remember the session returned by the lower
|
|
|
|
* level.
|
2014-07-23 03:23:05 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
lower = priv->lower;
|
|
|
|
DEBUGASSERT(lower && lower->ops->reserve);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Defer to lower reserve\n");
|
2014-07-23 03:23:05 +02:00
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
2014-07-24 17:56:39 +02:00
|
|
|
ret = lower->ops->reserve(lower, &priv->session);
|
|
|
|
|
|
|
|
/* Return a copy of the session to the caller */
|
|
|
|
|
|
|
|
*session = priv->session;
|
|
|
|
|
2014-07-23 03:23:05 +02:00
|
|
|
#else
|
2014-07-24 17:56:39 +02:00
|
|
|
ret = lower->ops->reserve(lower);
|
2014-07-23 03:23:05 +02:00
|
|
|
#endif
|
2014-07-24 17:56:39 +02:00
|
|
|
|
|
|
|
return ret;
|
2014-07-22 19:54:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_release
|
|
|
|
*
|
|
|
|
* Description:
|
2014-07-23 03:23:05 +02:00
|
|
|
* Release a session.
|
2014-07-22 19:54:13 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
static int pcm_release(FAR struct audio_lowerhalf_s *dev, FAR void *session)
|
|
|
|
#else
|
|
|
|
static int pcm_release(FAR struct audio_lowerhalf_s *dev)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
FAR struct pcm_decode_s *priv = (FAR struct pcm_decode_s *)dev;
|
2014-07-23 03:23:05 +02:00
|
|
|
FAR struct audio_lowerhalf_s *lower;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
2021-02-25 13:48:46 +01:00
|
|
|
/* Release the lower driver. It is then available for use by other
|
|
|
|
* decoders (and we cannot use the lower driver either unless we re-
|
2014-07-23 03:23:05 +02:00
|
|
|
* reserve it).
|
|
|
|
*/
|
|
|
|
|
|
|
|
lower = priv->lower;
|
|
|
|
DEBUGASSERT(lower && lower->ops->release);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Defer to lower release\n");
|
2014-07-23 03:23:05 +02:00
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
return lower->ops->release(lower, session);
|
|
|
|
#else
|
|
|
|
return lower->ops->release(lower);
|
|
|
|
#endif
|
2014-07-22 19:54:13 +02:00
|
|
|
}
|
|
|
|
|
2014-07-25 18:07:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_callback
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Lower-to-upper level callback for buffer dequeueing.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - The value of the 'priv' field from out audio_lowerhalf_s.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
static void pcm_callback(FAR void *arg, uint16_t reason,
|
2014-07-28 02:33:17 +02:00
|
|
|
FAR struct ap_buffer_s *apb, uint16_t status,
|
|
|
|
FAR void *session)
|
2014-07-28 05:42:10 +02:00
|
|
|
#else
|
2014-07-28 02:33:17 +02:00
|
|
|
static void pcm_callback(FAR void *arg, uint16_t reason,
|
|
|
|
FAR struct ap_buffer_s *apb, uint16_t status)
|
2014-07-28 05:42:10 +02:00
|
|
|
#endif
|
2014-07-28 02:33:17 +02:00
|
|
|
{
|
|
|
|
FAR struct pcm_decode_s *priv = (FAR struct pcm_decode_s *)arg;
|
|
|
|
|
2014-07-28 20:45:22 +02:00
|
|
|
DEBUGASSERT(priv && priv->export.upper);
|
2014-07-28 05:42:10 +02:00
|
|
|
|
2019-09-29 20:52:20 +02:00
|
|
|
/* The buffer belongs to an upper level. Just forward the event to
|
2014-07-28 05:42:10 +02:00
|
|
|
* the next level up.
|
2014-07-28 02:33:17 +02:00
|
|
|
*/
|
|
|
|
|
2014-07-28 05:42:10 +02:00
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
priv->export.upper(priv->export.priv, reason, apb, status, session);
|
|
|
|
#else
|
|
|
|
priv->export.upper(priv->export.priv, reason, apb, status);
|
2014-07-25 18:07:32 +02:00
|
|
|
#endif
|
2014-07-28 02:33:17 +02:00
|
|
|
}
|
|
|
|
|
2014-07-22 19:54:13 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_decode_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the PCM device. The PCM device accepts and contains a
|
|
|
|
* low-level audio DAC-type device. It then returns a new audio lower
|
|
|
|
* half interface at adds a PCM decoding from end to the low-level
|
|
|
|
* audio device
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - A reference to the low-level audio DAC-type device to contain.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, a new audio device instance is returned that wraps the
|
|
|
|
* low-level device and provides a PCM decoding front end. NULL is
|
|
|
|
* returned on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct audio_lowerhalf_s *
|
|
|
|
pcm_decode_initialize(FAR struct audio_lowerhalf_s *dev)
|
|
|
|
{
|
|
|
|
FAR struct pcm_decode_s *priv;
|
|
|
|
FAR struct audio_ops_s *ops;
|
|
|
|
|
|
|
|
/* Allocate an instance of our private data structure */
|
|
|
|
|
2014-09-01 01:34:44 +02:00
|
|
|
priv = (FAR struct pcm_decode_s *)kmm_zalloc(sizeof(struct pcm_decode_s));
|
2014-07-22 19:54:13 +02:00
|
|
|
if (!priv)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
auderr("ERROR: Failed to allocate driver structure\n");
|
2014-07-22 19:54:13 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-09-01 01:34:44 +02:00
|
|
|
/* Initialize our private data structure. Since kmm_zalloc() was used for
|
2014-07-22 19:54:13 +02:00
|
|
|
* the allocation, we need to initialize only non-zero, non-NULL, non-
|
|
|
|
* false fields.
|
|
|
|
*/
|
|
|
|
|
2014-07-25 18:07:32 +02:00
|
|
|
/* Setup our operations */
|
|
|
|
|
2014-07-22 19:54:13 +02:00
|
|
|
ops = &priv->ops;
|
|
|
|
ops->getcaps = pcm_getcaps;
|
|
|
|
ops->configure = pcm_configure;
|
|
|
|
ops->shutdown = pcm_shutdown;
|
|
|
|
ops->start = pcm_start;
|
|
|
|
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_STOP
|
|
|
|
ops->stop = pcm_stop;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
|
|
|
|
ops->pause = pcm_pause;
|
|
|
|
ops->resume = pcm_resume;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (dev->ops->allocbuffer)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(dev->ops->freebuffer);
|
|
|
|
ops->allocbuffer = pcm_allocbuffer;
|
|
|
|
ops->freebuffer = pcm_freebuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
ops->enqueuebuffer = pcm_enqueuebuffer;
|
|
|
|
ops->cancelbuffer = pcm_cancelbuffer;
|
|
|
|
ops->ioctl = pcm_ioctl;
|
|
|
|
ops->reserve = pcm_reserve;
|
|
|
|
ops->release = pcm_release;
|
|
|
|
|
2014-07-25 18:07:32 +02:00
|
|
|
/* Set up our struct audio_lower_half that we will register with the
|
|
|
|
* system. The registration process will fill in the priv->export.upper
|
|
|
|
* and priv fields with the correct callback information.
|
|
|
|
*/
|
|
|
|
|
2014-07-22 19:54:13 +02:00
|
|
|
priv->export.ops = &priv->ops;
|
2014-07-25 18:07:32 +02:00
|
|
|
|
|
|
|
/* Save the struct audio_lower_half of the low-level audio device. Set
|
|
|
|
* out callback information for the lower-level audio device. Our
|
|
|
|
* callback will simply forward to the upper callback.
|
|
|
|
*/
|
|
|
|
|
2014-07-22 19:54:13 +02:00
|
|
|
priv->lower = dev;
|
2014-07-25 18:07:32 +02:00
|
|
|
dev->upper = pcm_callback;
|
|
|
|
dev->priv = priv;
|
2014-07-22 19:54:13 +02:00
|
|
|
|
|
|
|
return &priv->export;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_AUDIO && CONFIG_AUDIO_FORMAT_PCM */
|