2014-07-22 19:54:13 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* audio/pcm_decode.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Based on the original audio framework from:
|
|
|
|
*
|
|
|
|
* Author: Ken Pettit <pettitkd@gmail.com>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <semaphore.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
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Configuration ************************************************************/
|
2014-07-23 03:23:05 +02:00
|
|
|
#define CONFIG_PCM_DEBUG 1 /* For now */
|
2014-07-22 19:54:13 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
/* 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
|
|
|
|
* few of the methods based upon what is supported by the the lower level
|
|
|
|
* 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
|
|
|
|
|
|
|
/* This is a copy of the WAV file header, in host endian order */
|
|
|
|
|
|
|
|
struct wav_header_s wav;
|
|
|
|
|
|
|
|
/* Set to true once we have parse a valid header and have begun stream
|
|
|
|
* audio.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool streaming;
|
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
|
|
|
|
|
2014-07-23 03:23:05 +02:00
|
|
|
#ifdef CONFIG_ENDIAN_BIG
|
|
|
|
static uint16_t pcm_leuint16(uint16_t value);
|
|
|
|
static uint16_t pcm_leuint32(uint32_t value);
|
|
|
|
#else
|
|
|
|
# define pcm_leuint16(v) (v)
|
|
|
|
# define pcm_leuint32(v) (v)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline bool pcm_validwav(FAR const struct wav_header_s *wav);
|
|
|
|
static bool pcm_parsewav(FAR struct pcm_decode_s *priv, uint8_t *data);
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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)
|
|
|
|
{
|
2014-07-23 03:23:05 +02:00
|
|
|
dbg( "Wave file header\n");
|
|
|
|
dbg( " Chunk ID: 0x%08x\n", wav->hdr.chunkid);
|
|
|
|
dbg( " Chunk Size: %u\n", wav->hdr.chunklen);
|
|
|
|
dbg( " Format: 0x%08x\n", wav->hdr.format);
|
|
|
|
dbg( " SubChunk ID: 0x%08x\n", wav->fmt.chunkid);
|
|
|
|
dbg( " Subchunk1 Size: %u\n", wav->fmt.chunklen);
|
|
|
|
dbg( " Audio Format: 0x%04x\n", wav->fmt.format);
|
|
|
|
dbg( " Num. Channels: %d\n", wav->fmt.nchannels);
|
|
|
|
dbg( " Sample Rate: %u\n", wav->fmt.samprate);
|
|
|
|
dbg( " Byte Rate: %u\n", wav->fmt.byterate);
|
|
|
|
dbg( " Block Align: %d\n", wav->fmt.align);
|
|
|
|
dbg( " Bits Per Sample: %d\n", wav->fmt.bpsamp);
|
|
|
|
dbg( " Subchunk2 ID: 0x%08x\n", wav->data.chunkid);
|
|
|
|
dbg( " Subchunk2 Size: %u\n", wav->data.chunklen);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_leuint16
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get a 16-bit value stored in little endian order for a big-endian
|
|
|
|
* machine.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ENDIAN_BIG
|
|
|
|
static uint16_t pcm_leuint16(uint16_t value)
|
|
|
|
{
|
|
|
|
return (((value & 0x00ff) << 8) |
|
|
|
|
((value >> 8) & 0x00ff));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_leuint16
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get a 16-bit value stored in little endian order for a big-endian
|
|
|
|
* machine.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_ENDIAN_BIG
|
|
|
|
static uint16_t pcm_leuint32(uint32_t value)
|
|
|
|
{
|
|
|
|
return (((value & 0x000000ff) << 24) |
|
|
|
|
((value & 0x0000ff00) << 8) |
|
|
|
|
((value & 0x00ff0000) >> 8) |
|
|
|
|
((value & 0xff000000) >> 24));
|
2014-07-22 23:54:56 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline bool pcm_validwav(FAR const struct wav_header_s *wav)
|
|
|
|
{
|
2014-07-23 03:23:05 +02:00
|
|
|
return (wav->hdr.chunkid == WAV_HDR_CHUNKID &&
|
|
|
|
wav->hdr.format == WAV_HDR_FORMAT &&
|
|
|
|
wav->fmt.chunkid == WAV_FMT_CHUNKID &&
|
|
|
|
wav->fmt.chunklen == WAV_FMT_CHUNKLEN &&
|
|
|
|
wav->fmt.format == WAV_FMT_FORMAT &&
|
|
|
|
wav->data.chunkid == WAV_DATA_CHUNKID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_parsewav
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Parse and verify the WAV file header.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static bool pcm_parsewav(FAR struct pcm_decode_s *priv, uint8_t *data)
|
|
|
|
{
|
|
|
|
FAR const struct wav_header_s *wav = (FAR const struct wav_header_s *)data;
|
|
|
|
|
|
|
|
/* Transfer the purported WAV file header into our private storage,
|
|
|
|
* correcting for endian issues as needed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->wav.hdr.chunkid = pcm_leuint32(wav->hdr.chunkid);
|
|
|
|
priv->wav.hdr.chunklen = pcm_leuint32(wav->hdr.chunklen);
|
|
|
|
priv->wav.hdr.format = pcm_leuint32(wav->hdr.format);
|
|
|
|
|
|
|
|
priv->wav.fmt.chunkid = pcm_leuint32(wav->fmt.chunkid);
|
|
|
|
priv->wav.fmt.chunklen = pcm_leuint32(wav->fmt.chunklen);
|
|
|
|
priv->wav.fmt.format = pcm_leuint16(wav->fmt.format);
|
|
|
|
priv->wav.fmt.nchannels = pcm_leuint16(wav->fmt.nchannels);
|
|
|
|
priv->wav.fmt.samprate = pcm_leuint32(wav->fmt.samprate);
|
|
|
|
priv->wav.fmt.byterate = pcm_leuint32(wav->fmt.byterate);
|
|
|
|
priv->wav.fmt.align = pcm_leuint16(wav->fmt.align);
|
|
|
|
priv->wav.fmt.bpsamp = pcm_leuint16(wav->fmt.bpsamp);
|
|
|
|
|
|
|
|
priv->wav.data.chunkid = pcm_leuint32(wav->data.chunkid);
|
|
|
|
priv->wav.data.chunklen = pcm_leuint32(wav->data.chunklen);
|
|
|
|
|
|
|
|
/* Dump the converted wave header information */
|
|
|
|
|
|
|
|
pcm_dump(&priv->wav);
|
|
|
|
|
|
|
|
/* And return true if the the file is a valid WAV header file */
|
|
|
|
|
|
|
|
return pcm_validwav(&priv->wav);
|
2014-07-22 23:54:56 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
auddbg("Lower getcaps() failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (caps->ac_subtype == AUDIO_TYPE_QUERY)
|
|
|
|
{
|
|
|
|
*((uint16_t *)&caps->ac_format[0]) = (1 << (AUDIO_FMT_PCM - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
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
|
|
|
|
{
|
|
|
|
FAR struct pcm_decode_s *priv = (FAR struct pcm_decode_s *)dev;
|
|
|
|
#warning Missing logic
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
|
|
*
|
2014-07-23 03:23:05 +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;
|
|
|
|
#warning Missing logic
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_start
|
|
|
|
*
|
|
|
|
* Description:
|
2014-07-23 03:23:05 +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;
|
|
|
|
#warning Missing logic
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
FAR struct pcm_decode_s *priv = (FAR struct pcm_decode_s *)dev;
|
|
|
|
#warning Missing logic
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pcm_pause
|
|
|
|
*
|
|
|
|
* Description:
|
2014-07-23 03:23:05 +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;
|
|
|
|
#warning Missing logic
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
FAR struct pcm_decode_s *priv = (FAR struct pcm_decode_s *)dev;
|
|
|
|
#warning Missing logic
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
|
|
|
* driver will perform a standard kumalloc using normal user-space
|
|
|
|
* 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
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
audvdbg("Received buffer %p, streaming=%d\n", apb, priv->streaming);
|
|
|
|
|
|
|
|
lower = priv->lower;
|
|
|
|
DEBUGASSERT(lower && lower->ops->enqueuebuffer);
|
|
|
|
|
|
|
|
/* Are we streaming yet? */
|
|
|
|
|
|
|
|
if (priv->streaming)
|
|
|
|
{
|
|
|
|
/* Yes, just give the buffer to the lower driver */
|
|
|
|
|
|
|
|
return lower->ops->enqueuebuffer(lower, apb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
audvdbg("curbyte=%d nbytes=%d nmaxbytes=%d bytesleft=%d\n",
|
|
|
|
apb->curbyte, apb->nbytes, apb->nmaxbytes, bytesleft);
|
|
|
|
|
|
|
|
if (bytesleft >= sizeof(struct wav_header_s))
|
|
|
|
{
|
|
|
|
/* Parse and verify the candidate WAV file header */
|
|
|
|
|
|
|
|
if (pcm_parsewav(priv, &apb->samp[apb->curbyte]))
|
|
|
|
{
|
|
|
|
/* Now we are streaming */
|
|
|
|
|
|
|
|
priv->streaming = true;
|
|
|
|
|
|
|
|
/* Bump up the data offset and pass the buffer to the lower level */
|
|
|
|
|
|
|
|
apb->curbyte += sizeof(struct wav_header_s);
|
|
|
|
return lower->ops->enqueuebuffer(lower, apb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is not a WAV file! */
|
|
|
|
|
|
|
|
auddbg("ERROR: Invalid WAV file\n");
|
|
|
|
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;
|
|
|
|
#warning Missing logic
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* 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).
|
|
|
|
*/
|
|
|
|
|
|
|
|
lower = priv->lower;
|
|
|
|
DEBUGASSERT(lower && lower->ops->reserve);
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
return lower->ops->reserve(lower, session);
|
|
|
|
#else
|
|
|
|
return lower->ops->reserve(lower);
|
|
|
|
#endif
|
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);
|
|
|
|
|
|
|
|
/* Release the lower driver.. it is then available for use by other
|
|
|
|
* decoders (and we cannot use the lower driver wither unless we re-
|
|
|
|
* reserve it).
|
|
|
|
*/
|
|
|
|
|
|
|
|
lower = priv->lower;
|
|
|
|
DEBUGASSERT(lower && lower->ops->release);
|
|
|
|
|
|
|
|
#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
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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 */
|
|
|
|
|
|
|
|
priv = (FAR struct pcm_decode_s *)kzalloc(sizeof(struct pcm_decode_s));
|
|
|
|
if (!priv)
|
|
|
|
{
|
|
|
|
auddbg("ERROR: Failed to allocate driver structure\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize our private data structure. Since kzalloc() was used for
|
|
|
|
* the allocation, we need to initialize only non-zero, non-NULL, non-
|
|
|
|
* false fields.
|
|
|
|
*/
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
priv->export.ops = &priv->ops;
|
|
|
|
priv->export.priv = priv;
|
|
|
|
priv->lower = dev;
|
|
|
|
|
|
|
|
return &priv->export;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_AUDIO && CONFIG_AUDIO_FORMAT_PCM */
|
|
|
|
|