apps/system/nxrecorder: Add nxrecorder application
This commit is contained in:
parent
09a3bc44b4
commit
392943a0a0
247
include/system/nxrecorder.h
Normal file
247
include/system/nxrecorder.h
Normal file
@ -0,0 +1,247 @@
|
||||
/****************************************************************************
|
||||
* apps/include/system/nxrecorder.h
|
||||
*
|
||||
* Copyright (C) 2017 Pinecone Inc. All rights reserved.
|
||||
* Author: Zhong An <zhongan@pinecone.net>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __APPS_INCLUDE_SYSTEM_NXRECORDER_H
|
||||
#define __APPS_INCLUDE_SYSTEM_NXRECORDER_H 1
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/* This structure describes the internal state of the NxRecorder */
|
||||
|
||||
struct nxrecorder_s
|
||||
{
|
||||
int state; /* Current recorder state */
|
||||
int devFd; /* File descriptor of active device */
|
||||
mqd_t mq; /* Message queue for the recordthread */
|
||||
char mqname[16]; /* Name of our message queue */
|
||||
pthread_t recordId; /* Thread ID of the recordthread */
|
||||
int crefs; /* Number of references to the recorder */
|
||||
sem_t sem; /* Thread sync semaphore */
|
||||
int fd; /* File descriptor of open file */
|
||||
char device[CONFIG_NAME_MAX]; /* Preferred audio device */
|
||||
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
||||
FAR void *session; /* Session assigment from device */
|
||||
#endif
|
||||
};
|
||||
|
||||
typedef int (*nxrecorder_func)(FAR struct nxrecorder_s *pRecorder, char *pargs);
|
||||
|
||||
/****************************************************************************
|
||||
* Public Data
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
#define EXTERN extern "C"
|
||||
extern "C"
|
||||
{
|
||||
#else
|
||||
#define EXTERN extern
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxrecorder_create
|
||||
*
|
||||
* Allocates and Initializes a NxRecorder context that is passed to all
|
||||
* nxrecorder routines. The recorder MUST be destroyed using the
|
||||
* nxrecorder_destroy() routine since the context is reference counted.
|
||||
* The context can be used in a mode where the caller creates the
|
||||
* context, starts a file recording, and then forgets about the context
|
||||
* and it will self free. This is because the nxrecorder_recordfile
|
||||
* will also create a reference to the context, so the client calling
|
||||
* nxrecorder_destroy() won't actually de-allocate anything. The freeing
|
||||
* will occur after the record thread has completed.
|
||||
*
|
||||
* Alternately, the caller can create the object and hold on to it, then
|
||||
* the context will persist until the original creator destroys it.
|
||||
*
|
||||
* Input Parameters: None
|
||||
*
|
||||
* Returned Value:
|
||||
* Pointer to created NxRecorder context or NULL if error.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR struct nxrecorder_s *nxrecorder_create(void);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxrecorder_release
|
||||
*
|
||||
* Reduces the reference count to the recorder and if it reaches zero,
|
||||
* frees all memory used by the context.
|
||||
*
|
||||
* Input Parameters:
|
||||
* pRecorder Pointer to the NxRecorder context
|
||||
*
|
||||
* Returned Value:
|
||||
* None
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void nxrecorder_release(FAR struct nxrecorder_s *pRecorder);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxrecorder_reference
|
||||
*
|
||||
* Increments the reference count to the recorder.
|
||||
*
|
||||
* Input Parameters:
|
||||
* pRecorder Pointer to the NxRecorder context
|
||||
*
|
||||
* Returned Value:
|
||||
* None
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void nxrecorder_reference(FAR struct nxrecorder_s *pRecorder);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxrecorder_setdevice
|
||||
*
|
||||
* Sets the preferred Audio device to use with the instance of the
|
||||
* nxrecorder. Without a preferred device set, the nxrecorder will search
|
||||
* the audio subsystem to find a suitable device depending on the
|
||||
* type of audio operation requested (i.e. an MP3 decoder device when
|
||||
* recording an MP3 file, a WAV decoder device for a WAV file, etc.).
|
||||
*
|
||||
* Input Parameters:
|
||||
* pRecorder - Pointer to the context to initialize
|
||||
* device - Pointer to pathname of the preferred device
|
||||
*
|
||||
* Returned Value:
|
||||
* OK if context initialized successfully, error code otherwise.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int nxrecorder_setdevice(FAR struct nxrecorder_s *pRecorder,
|
||||
FAR const char *device);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxrecorder_recordraw
|
||||
*
|
||||
* Plays the specified media file (from the filesystem) using the
|
||||
* Audio system. If a preferred device has been set, that device
|
||||
* will be used for the recordback, otherwise the first suitable device
|
||||
* found in the /dev/audio directory will be used.
|
||||
*
|
||||
* Input Parameters:
|
||||
* pRecorder - Pointer to the context to initialize
|
||||
* filename - Pointer to pathname of the file to record
|
||||
* nchannels - channels num
|
||||
* bpsampe - bit width
|
||||
* samprate - sample rate
|
||||
*
|
||||
*
|
||||
* Returned Value:
|
||||
* OK if file found, device found, and recordback started.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int nxrecorder_recordraw(FAR struct nxrecorder_s *pRecorder,
|
||||
FAR const char *filename, uint8_t nchannels,
|
||||
uint8_t bpsamp, uint32_t samprate);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxrecorder_stop
|
||||
*
|
||||
* Stops current recordback.
|
||||
*
|
||||
* Input Parameters:
|
||||
* pRecorder - Pointer to the context to initialize
|
||||
*
|
||||
* Returned Value:
|
||||
* OK if file found, device found, and recordback started.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_AUDIO_EXCLUDE_STOP
|
||||
int nxrecorder_stop(FAR struct nxrecorder_s *pRecorder);
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxrecorder_pause
|
||||
*
|
||||
* Pauses current recordback.
|
||||
*
|
||||
* Input Parameters:
|
||||
* pRecorder - Pointer to the context to initialize
|
||||
*
|
||||
* Returned Value:
|
||||
* OK if file found, device found, and recordback started.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
|
||||
int nxrecorder_pause(FAR struct nxrecorder_s *pRecorder);
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxrecorder_resume
|
||||
*
|
||||
* Resumes current recordback.
|
||||
*
|
||||
* Input Parameters:
|
||||
* pRecorder - Pointer to the context to initialize
|
||||
*
|
||||
* Returned Value:
|
||||
* OK if file found, device found, and recordback started.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
|
||||
int nxrecorder_resume(FAR struct nxrecorder_s *pRecorder);
|
||||
#endif
|
||||
|
||||
#undef EXTERN
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __APPS_INCLUDE_SYSTEM_NXRECORDER_H */
|
11
system/nxrecorder/.gitignore
vendored
Normal file
11
system/nxrecorder/.gitignore
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
/Make.dep
|
||||
/.depend
|
||||
/.built
|
||||
/*.asm
|
||||
/*.rel
|
||||
/*.lst
|
||||
/*.sym
|
||||
/*.adb
|
||||
/*.lib
|
||||
/*.src
|
||||
/*.obj
|
41
system/nxrecorder/Kconfig
Normal file
41
system/nxrecorder/Kconfig
Normal file
@ -0,0 +1,41 @@
|
||||
#
|
||||
# For a description of the syntax of this configuration file,
|
||||
# see the file kconfig-language.txt in the NuttX tools repository.
|
||||
#
|
||||
|
||||
config SYSTEM_NXRECORDER
|
||||
bool "NxRecorder pcm raw data Recorder"
|
||||
default n
|
||||
depends on AUDIO
|
||||
---help---
|
||||
Enable support for the NxRecorder pcm raw data recorder library
|
||||
and optional command line interface.
|
||||
|
||||
if SYSTEM_NXRECORDER
|
||||
|
||||
config NXRECORDER_RECORDTHREAD_STACKSIZE
|
||||
int "NxRecorder thread stack size"
|
||||
default 1500
|
||||
---help---
|
||||
Stack size to use with the NxRecorder record thread.
|
||||
|
||||
config NXRECORDER_COMMAND_LINE
|
||||
bool "Include nxrecorder command line application"
|
||||
default y
|
||||
---help---
|
||||
Compiles in code for the nxrecorder command line control.
|
||||
This is a text-based command line interface that uses
|
||||
the nxrecorder library to record pcm raw data, control the
|
||||
volume, balance, bass, etc.
|
||||
|
||||
if NXRECORDER_COMMAND_LINE
|
||||
|
||||
config NXRECORDER_INCLUDE_HELP
|
||||
bool "Include HELP command and text"
|
||||
default y
|
||||
---help---
|
||||
Compiles in the NxRecorder help text to provide online help
|
||||
for available commands with syntax.
|
||||
|
||||
endif
|
||||
endif
|
40
system/nxrecorder/Make.defs
Normal file
40
system/nxrecorder/Make.defs
Normal file
@ -0,0 +1,40 @@
|
||||
############################################################################
|
||||
# apps/system/nxplayer/Make.defs
|
||||
# Adds selected applications to apps/ build
|
||||
#
|
||||
# Copyright (C) 2016 Gregory Nutt. All rights reserved.
|
||||
# Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
ifeq ($(CONFIG_SYSTEM_NXRECORDER),y)
|
||||
CONFIGURED_APPS += system/nxrecorder
|
||||
endif
|
||||
|
154
system/nxrecorder/Makefile
Normal file
154
system/nxrecorder/Makefile
Normal file
@ -0,0 +1,154 @@
|
||||
############################################################################
|
||||
# apps/system/nxrecorder/Makefile
|
||||
# Copyright (C) 2017 Pinecone Inc. All rights reserved.
|
||||
# Author: Zhong An <zhongan@pinecone.net>
|
||||
|
||||
# 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
-include $(TOPDIR)/.config
|
||||
-include $(TOPDIR)/Make.defs
|
||||
include $(APPDIR)/Make.defs
|
||||
|
||||
# NxRecorder Library
|
||||
|
||||
ASRCS =
|
||||
CSRCS = nxrecorder.c
|
||||
|
||||
# NxRecorder Application
|
||||
|
||||
APPNAME = nxrecorder
|
||||
PRIORITY = SCHED_PRIORITY_DEFAULT
|
||||
STACKSIZE = 2048
|
||||
|
||||
ifeq ($(CONFIG_BUILD_LOADABLE),y)
|
||||
MAINSRC = nxrecorder_main.c
|
||||
else
|
||||
ifeq ($(CONFIG_NXRECORDER_COMMAND_LINE),y)
|
||||
MAINSRC = nxrecorder_main.c
|
||||
endif
|
||||
endif
|
||||
|
||||
AOBJS = $(ASRCS:.S=$(OBJEXT))
|
||||
COBJS = $(CSRCS:.c=$(OBJEXT))
|
||||
MAINOBJ = $(MAINSRC:.c=$(OBJEXT))
|
||||
|
||||
SRCS = $(ASRCS) $(CSRCS) $(MAINSRC)
|
||||
OBJS = $(AOBJS) $(COBJS)
|
||||
|
||||
ifneq ($(CONFIG_BUILD_LOADABLE),y)
|
||||
OBJS += $(MAINOBJ)
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_WINDOWS_NATIVE),y)
|
||||
BIN = ..\..\libapps$(LIBEXT)
|
||||
else
|
||||
ifeq ($(WINTOOL),y)
|
||||
BIN = ..\\..\\libapps$(LIBEXT)
|
||||
else
|
||||
BIN = ../../libapps$(LIBEXT)
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(WINTOOL),y)
|
||||
INSTALL_DIR = "${shell cygpath -w $(BIN_DIR)}"
|
||||
else
|
||||
INSTALL_DIR = $(BIN_DIR)
|
||||
endif
|
||||
|
||||
CONFIG_XYZ_PROGNAME ?= nxrecorder$(EXEEXT)
|
||||
PROGNAME = $(CONFIG_XYZ_PROGNAME)
|
||||
|
||||
ROOTDEPPATH = --dep-path .
|
||||
|
||||
# Common build
|
||||
|
||||
VPATH := :$(SRCDIR)
|
||||
|
||||
all: .built
|
||||
.PHONY: context depend clean distclean preconfig
|
||||
.PRECIOUS: ../../libapps$(LIBEXT)
|
||||
|
||||
$(AOBJS): %$(OBJEXT): %.S
|
||||
$(call ASSEMBLE, $<, $@)
|
||||
|
||||
$(COBJS) $(MAINOBJ): %$(OBJEXT): %.c
|
||||
$(call COMPILE, $<, $@)
|
||||
|
||||
.built: $(OBJS)
|
||||
$(call ARCHIVE, $(BIN), $(OBJS))
|
||||
$(Q) touch .built
|
||||
|
||||
ifeq ($(CONFIG_BUILD_LOADABLE),y)
|
||||
$(BIN_DIR)$(DELIM)$(PROGNAME): $(OBJS) $(MAINOBJ)
|
||||
@echo "LD: $(PROGNAME)"
|
||||
$(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(PROGNAME) $(ARCHCRT0OBJ) $(MAINOBJ) $(LDLIBS)
|
||||
$(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(PROGNAME)
|
||||
|
||||
install: $(BIN_DIR)$(DELIM)$(PROGNAME)
|
||||
|
||||
else
|
||||
install:
|
||||
|
||||
endif
|
||||
|
||||
# Register application
|
||||
|
||||
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
|
||||
ifeq ($(CONFIG_NXRECORDER_COMMAND_LINE),y)
|
||||
$(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat: $(DEPCONFIG) Makefile
|
||||
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(if $(CONFIG_BUILD_LOADABLE),,$(APPNAME)_main))
|
||||
|
||||
context: $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat
|
||||
else
|
||||
context:
|
||||
endif
|
||||
else
|
||||
context:
|
||||
endif
|
||||
|
||||
# Create dependencies
|
||||
|
||||
.depend: Makefile $(SRCS)
|
||||
$(Q) $(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $^ >Make.dep
|
||||
$(Q) touch $@
|
||||
|
||||
depend: .depend
|
||||
|
||||
clean:
|
||||
$(call DELFILE, .built)
|
||||
$(call CLEAN)
|
||||
|
||||
distclean: clean
|
||||
$(call DELFILE, Make.dep)
|
||||
$(call DELFILE, .depend)
|
||||
|
||||
preconfig:
|
||||
|
||||
-include Make.dep
|
1111
system/nxrecorder/nxrecorder.c
Normal file
1111
system/nxrecorder/nxrecorder.c
Normal file
File diff suppressed because it is too large
Load Diff
492
system/nxrecorder/nxrecorder_main.c
Normal file
492
system/nxrecorder/nxrecorder_main.c
Normal file
@ -0,0 +1,492 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* apps/system/nxrecorder/nxrecorder_main.c
|
||||
* Copyright (C) 2017 Pinecone Inc. All rights reserved.
|
||||
* Author: Zhong An <zhongan@pinecone.net>
|
||||
*
|
||||
* 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 <nuttx/audio/audio.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "system/readline.h"
|
||||
#include "system/nxrecorder.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define NXRECORDER_VER "1.00"
|
||||
|
||||
#ifdef CONFIG_NXRECORDER_INCLUDE_HELP
|
||||
# define NXRECORDER_HELP_TEXT(x) #x
|
||||
#else
|
||||
# define NXRECORDER_HELP_TEXT(x)
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
struct mp_cmd_s
|
||||
{
|
||||
const char *cmd; /* The command text */
|
||||
const char *arghelp; /* Text describing the args */
|
||||
nxrecorder_func pFunc; /* Pointer to command handler */
|
||||
const char *help; /* The help text */
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
static int nxrecorder_cmd_quit(FAR struct nxrecorder_s *pRecorder, char *parg);
|
||||
static int nxrecorder_cmd_recordraw(FAR struct nxrecorder_s *pRecorder, char *parg);
|
||||
static int nxrecorder_cmd_device(FAR struct nxrecorder_s *pRecorder, char *parg);
|
||||
|
||||
#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
|
||||
static int nxrecorder_cmd_pause(FAR struct nxrecorder_s *pRecorder, char *parg);
|
||||
static int nxrecorder_cmd_resume(FAR struct nxrecorder_s *pRecorder, char *parg);
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_AUDIO_EXCLUDE_STOP
|
||||
static int nxrecorder_cmd_stop(FAR struct nxrecorder_s *pRecorder, char *parg);
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_NXRECORDER_INCLUDE_HELP
|
||||
static int nxrecorder_cmd_help(FAR struct nxrecorder_s *pRecorder, char *parg);
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
static const struct mp_cmd_s g_nxrecorder_cmds[] =
|
||||
{
|
||||
{ "device", "devfile", nxrecorder_cmd_device, NXRECORDER_HELP_TEXT(Specify a preferred audio device) },
|
||||
#ifdef CONFIG_NXRECORDER_INCLUDE_HELP
|
||||
{ "h", "", nxrecorder_cmd_help, NXRECORDER_HELP_TEXT(Display help for commands) },
|
||||
{ "help", "", nxrecorder_cmd_help, NXRECORDER_HELP_TEXT(Display help for commands) },
|
||||
#endif
|
||||
{ "recordraw", "filename", nxrecorder_cmd_recordraw, NXRECORDER_HELP_TEXT(Record a pcm raw file) },
|
||||
#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
|
||||
{ "pause", "", nxrecorder_cmd_pause, NXRECORDER_HELP_TEXT(Pause record) },
|
||||
{ "resume", "", nxrecorder_cmd_resume, NXRECORDER_HELP_TEXT(Resume record) },
|
||||
#endif
|
||||
#ifndef CONFIG_AUDIO_EXCLUDE_STOP
|
||||
{ "stop", "", nxrecorder_cmd_stop, NXRECORDER_HELP_TEXT(Stop record) },
|
||||
#endif
|
||||
{ "q", "", nxrecorder_cmd_quit, NXRECORDER_HELP_TEXT(Exit NxRecorder) },
|
||||
{ "quit", "", nxrecorder_cmd_quit, NXRECORDER_HELP_TEXT(Exit NxRecorder) },
|
||||
};
|
||||
static const int g_nxrecorder_cmd_count = sizeof(g_nxrecorder_cmds) / sizeof(struct mp_cmd_s);
|
||||
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxrecorder_cmd_recordraw
|
||||
*
|
||||
* nxrecorder_cmd_recordraw() records the raw data file using the nxrecorder
|
||||
* context.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static int nxrecorder_cmd_recordraw(FAR struct nxrecorder_s *pRecorder, char *parg)
|
||||
{
|
||||
int ret;
|
||||
int channels = 0;
|
||||
int bpsamp = 0;
|
||||
int samprate = 0;
|
||||
char filename[128];
|
||||
|
||||
sscanf(parg, "%s %d %d %d", filename, &channels, &bpsamp, &samprate);
|
||||
|
||||
/* Try to record the file specified */
|
||||
|
||||
ret = nxrecorder_recordraw(pRecorder, filename, channels, bpsamp, samprate);
|
||||
|
||||
/* nxrecorder_recordfile returned values:
|
||||
*
|
||||
* OK File is being recorded
|
||||
* -EBUSY The media device is busy
|
||||
* -ENOSYS The media file is an unsupported type
|
||||
* -ENODEV No audio device suitable to record the media type
|
||||
* -ENOENT The media file was not found
|
||||
*/
|
||||
|
||||
switch (-ret)
|
||||
{
|
||||
case OK:
|
||||
break;
|
||||
|
||||
case ENODEV:
|
||||
printf("No suitable Audio Device found\n");
|
||||
break;
|
||||
|
||||
case EBUSY:
|
||||
printf("Audio device busy\n");
|
||||
break;
|
||||
|
||||
case ENOENT:
|
||||
printf("File %s not found\n", filename);
|
||||
break;
|
||||
|
||||
case ENOSYS:
|
||||
printf("Unknown audio format\n");
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("Error recording file: %d\n", -ret);
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxrecorder_cmd_stop
|
||||
*
|
||||
* nxrecorder_cmd_stop() stops record of currently recording file
|
||||
* context.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_AUDIO_EXCLUDE_STOP
|
||||
static int nxrecorder_cmd_stop(FAR struct nxrecorder_s *pRecorder, char *parg)
|
||||
{
|
||||
/* Stop the record */
|
||||
|
||||
nxrecorder_stop(pRecorder);
|
||||
|
||||
return OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxrecorder_cmd_pause
|
||||
*
|
||||
* nxrecorder_cmd_pause() pauses record of currently recording file
|
||||
* context.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
|
||||
static int nxrecorder_cmd_pause(FAR struct nxrecorder_s *pRecorder, char *parg)
|
||||
{
|
||||
/* Pause the record */
|
||||
|
||||
nxrecorder_pause(pRecorder);
|
||||
|
||||
return OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxrecorder_cmd_resume
|
||||
*
|
||||
* nxrecorder_cmd_resume() resumes record of currently recording file
|
||||
* context.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
|
||||
static int nxrecorder_cmd_resume(FAR struct nxrecorder_s *pRecorder, char *parg)
|
||||
{
|
||||
/* Resume the record */
|
||||
|
||||
nxrecorder_resume(pRecorder);
|
||||
|
||||
return OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxrecorder_cmd_device
|
||||
*
|
||||
* nxrecorder_cmd_device() sets the preferred audio device for record
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static int nxrecorder_cmd_device(FAR struct nxrecorder_s *pRecorder, char *parg)
|
||||
{
|
||||
int ret;
|
||||
char path[32];
|
||||
|
||||
/* First try to open the file directly */
|
||||
|
||||
ret = nxrecorder_setdevice(pRecorder, parg);
|
||||
if (ret == -ENOENT)
|
||||
{
|
||||
/* Append the /dev/audio path and try again */
|
||||
|
||||
#ifdef CONFIG_AUDIO_CUSTOM_DEV_PATH
|
||||
#ifdef CONFIG_AUDIO_DEV_ROOT
|
||||
snprintf(path, sizeof(path), "/dev/%s", parg);
|
||||
#else
|
||||
snprintf(path, sizeof(path), CONFIG_AUDIO_DEV_PATH "/%s", parg);
|
||||
#endif
|
||||
#else
|
||||
snprintf(path, sizeof(path), "/dev/audio/%s", parg);
|
||||
#endif
|
||||
ret = nxrecorder_setdevice(pRecorder, path);
|
||||
}
|
||||
|
||||
/* Test if the device file exists */
|
||||
|
||||
if (ret == -ENOENT)
|
||||
{
|
||||
/* Device doesn't exit. Report error */
|
||||
|
||||
printf("Device %s not found\n", parg);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Test if is is an audio device */
|
||||
|
||||
if (ret == -ENODEV)
|
||||
{
|
||||
printf("Device %s is not an audio device\n", parg);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Device set successfully */
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxrecorder_cmd_quit
|
||||
*
|
||||
* nxrecorder_cmd_quit() terminates the application
|
||||
****************************************************************************/
|
||||
|
||||
static int nxrecorder_cmd_quit(FAR struct nxrecorder_s *pRecorder, char *parg)
|
||||
{
|
||||
/* Stop the record if any */
|
||||
|
||||
#ifndef CONFIG_AUDIO_EXCLUDE_STOP
|
||||
nxrecorder_stop(pRecorder);
|
||||
#endif
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxrecorder_cmd_help
|
||||
*
|
||||
* nxrecorder_cmd_help() display the application's help information on
|
||||
* supported commands and command syntax.
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_NXRECORDER_INCLUDE_HELP
|
||||
static int nxrecorder_cmd_help(FAR struct nxrecorder_s *pRecorder, char *parg)
|
||||
{
|
||||
int x, len, maxlen = 0;
|
||||
int c;
|
||||
|
||||
/* Calculate length of longest cmd + arghelp */
|
||||
|
||||
for (x = 0; x < g_nxrecorder_cmd_count; x++)
|
||||
{
|
||||
len = strlen(g_nxrecorder_cmds[x].cmd) + strlen(g_nxrecorder_cmds[x].arghelp);
|
||||
if (len > maxlen)
|
||||
{
|
||||
maxlen = len;
|
||||
}
|
||||
}
|
||||
|
||||
printf("NxRecorder commands\n================\n");
|
||||
for (x = 0; x < g_nxrecorder_cmd_count; x++)
|
||||
{
|
||||
/* Print the command and it's arguments */
|
||||
|
||||
printf(" %s %s", g_nxrecorder_cmds[x].cmd, g_nxrecorder_cmds[x].arghelp);
|
||||
|
||||
/* Calculate number of spaces to print before the help text */
|
||||
|
||||
len = maxlen - (strlen(g_nxrecorder_cmds[x].cmd) + strlen(g_nxrecorder_cmds[x].arghelp));
|
||||
for (c = 0; c < len; c++)
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
|
||||
printf(" : %s\n", g_nxrecorder_cmds[x].help);
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nxrecorder
|
||||
*
|
||||
* nxrecorder() reads in commands from the console using the readline
|
||||
* system add-in and implemets a command-line based pcm raw data recorder
|
||||
* that uses the NuttX audio system to record pcm raw data files read in
|
||||
* from the audio device. Commands are provided for setting volume, base and
|
||||
* other audio features, as well as for pausing and stopping the
|
||||
* record.
|
||||
*
|
||||
* Input Parameters:
|
||||
* buf - The user allocated buffer to be filled.
|
||||
* buflen - the size of the buffer.
|
||||
* instream - The stream to read characters from
|
||||
* outstream - The stream to each characters to.
|
||||
*
|
||||
* Returned values:
|
||||
* On success, the (positive) number of bytes transferred is returned.
|
||||
* EOF is returned to indicate either an end of file condition or a
|
||||
* failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_BUILD_LOADABLE
|
||||
int main(int argc, FAR char *argv[])
|
||||
#else
|
||||
int nxrecorder_main(int argc, char *argv[])
|
||||
#endif
|
||||
{
|
||||
char buffer[64];
|
||||
int len, x, running;
|
||||
char *cmd, *arg;
|
||||
FAR struct nxrecorder_s *pRecorder;
|
||||
|
||||
printf("NxRecorder version " NXRECORDER_VER "\n");
|
||||
printf("h for commands, q to exit\n");
|
||||
printf("\n");
|
||||
|
||||
/* Initialize our NxRecorder context */
|
||||
|
||||
pRecorder = nxrecorder_create();
|
||||
if (pRecorder == NULL)
|
||||
{
|
||||
printf("Error: Out of RAM\n");
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
/* Loop until the user exits */
|
||||
|
||||
running = TRUE;
|
||||
while (running)
|
||||
{
|
||||
/* Print a prompt */
|
||||
|
||||
printf("nxrecorder> ");
|
||||
fflush(stdout);
|
||||
|
||||
/* Read a line from the terminal */
|
||||
|
||||
len = readline(buffer, sizeof(buffer), stdin, stdout);
|
||||
buffer[len] = '\0';
|
||||
if (len > 0)
|
||||
{
|
||||
if (buffer[len-1] == '\n')
|
||||
{
|
||||
buffer[len-1] = '\0';
|
||||
}
|
||||
|
||||
/* Parse the command from the argument */
|
||||
|
||||
cmd = strtok_r(buffer, " \n", &arg);
|
||||
if (cmd == NULL)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Remove leading spaces from arg */
|
||||
|
||||
while (*arg == ' ')
|
||||
{
|
||||
arg++;
|
||||
}
|
||||
|
||||
/* Find the command in our cmd array */
|
||||
|
||||
for (x = 0; x < g_nxrecorder_cmd_count; x++)
|
||||
{
|
||||
if (strcmp(cmd, g_nxrecorder_cmds[x].cmd) == 0)
|
||||
{
|
||||
/* Command found. Call it's handler if not NULL */
|
||||
|
||||
if (g_nxrecorder_cmds[x].pFunc != NULL)
|
||||
{
|
||||
g_nxrecorder_cmds[x].pFunc(pRecorder, arg);
|
||||
}
|
||||
|
||||
/* Test if it is a quit command */
|
||||
|
||||
if (g_nxrecorder_cmds[x].pFunc == nxrecorder_cmd_quit)
|
||||
{
|
||||
running = FALSE;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Test for Unknown command */
|
||||
|
||||
if (x == g_nxrecorder_cmd_count)
|
||||
{
|
||||
printf("%s: unknown nxrecorder command\n", buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Release the NxRecorder context */
|
||||
|
||||
nxrecorder_release(pRecorder);
|
||||
|
||||
return OK;
|
||||
}
|
Loading…
Reference in New Issue
Block a user