Improved playtone and implemented echo example

This commit is contained in:
hexkyz 2018-03-02 20:17:36 +00:00
parent b9e9291e37
commit ad4b5af40b
3 changed files with 373 additions and 33 deletions

183
audio/echo/Makefile Normal file
View File

@ -0,0 +1,183 @@
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITPRO)),)
$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=<path to>/devkitpro")
endif
TOPDIR ?= $(CURDIR)
include $(DEVKITPRO)/libnx/switch_rules
#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# DATA is a list of directories containing data files
# INCLUDES is a list of directories containing header files
# EXEFS_SRC is the optional input directory containing data copied into exefs, if anything this normally should only contain "main.npdm".
#
# NO_ICON: if set to anything, do not use icon.
# NO_NACP: if set to anything, no .nacp file is generated.
# APP_TITLE is the name of the app stored in the .nacp file (Optional)
# APP_AUTHOR is the author of the app stored in the .nacp file (Optional)
# APP_VERSION is the version of the app stored in the .nacp file (Optional)
# APP_TITLEID is the titleID of the app stored in the .nacp file (Optional)
# ICON is the filename of the icon (.jpg), relative to the project folder.
# If not set, it attempts to use one of the following (in this order):
# - <Project name>.jpg
# - icon.jpg
# - <libnx folder>/default_icon.jpg
#---------------------------------------------------------------------------------
TARGET := $(notdir $(CURDIR))
BUILD := build
SOURCES := source
DATA := data
INCLUDES := include
EXEFS_SRC := exefs_src
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH := -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIE
CFLAGS := -g -Wall -O2 -ffunction-sections \
$(ARCH) $(DEFINES)
CFLAGS += $(INCLUDE) -DSWITCH
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11
ASFLAGS := -g $(ARCH)
LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
LIBS := -lnx -lm
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS := $(PORTLIBS) $(LIBNX)
#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------
export OUTPUT := $(CURDIR)/$(TARGET)
export TOPDIR := $(CURDIR)
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
$(foreach dir,$(DATA),$(CURDIR)/$(dir))
export DEPSDIR := $(CURDIR)/$(BUILD)
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
#---------------------------------------------------------------------------------
export LD := $(CC)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
export LD := $(CXX)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------
export OFILES := $(addsuffix .o,$(BINFILES)) \
$(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD)
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
export BUILD_EXEFS_SRC := $(TOPDIR)/$(EXEFS_SRC)
ifeq ($(strip $(ICON)),)
icons := $(wildcard *.jpg)
ifneq (,$(findstring $(TARGET).jpg,$(icons)))
export APP_ICON := $(TOPDIR)/$(TARGET).jpg
else
ifneq (,$(findstring icon.jpg,$(icons)))
export APP_ICON := $(TOPDIR)/icon.jpg
endif
endif
else
export APP_ICON := $(TOPDIR)/$(ICON)
endif
ifeq ($(strip $(NO_ICON)),)
export NROFLAGS += --icon=$(APP_ICON)
endif
ifeq ($(strip $(NO_NACP)),)
export NROFLAGS += --nacp=$(CURDIR)/$(TARGET).nacp
endif
ifneq ($(APP_TITLEID),)
export NACPFLAGS += --titleid=$(APP_TITLEID)
endif
.PHONY: $(BUILD) clean all
#---------------------------------------------------------------------------------
all: $(BUILD)
$(BUILD):
@[ -d $@ ] || mkdir -p $@
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
#---------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -fr $(BUILD) $(TARGET).pfs0 $(TARGET).nso $(TARGET).nro $(TARGET).nacp $(TARGET).elf
#---------------------------------------------------------------------------------
else
.PHONY: all
DEPENDS := $(OFILES:.o=.d)
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
all : $(OUTPUT).pfs0 $(OUTPUT).nro
$(OUTPUT).pfs0 : $(OUTPUT).nso
$(OUTPUT).nso : $(OUTPUT).elf
ifeq ($(strip $(NO_NACP)),)
$(OUTPUT).nro : $(OUTPUT).elf $(OUTPUT).nacp
else
$(OUTPUT).nro : $(OUTPUT).elf
endif
$(OUTPUT).elf : $(OFILES)
#---------------------------------------------------------------------------------
# you need a rule like this for each extension you use as binary data
#---------------------------------------------------------------------------------
%.bin.o : %.bin
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)
-include $(DEPENDS)
#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------

148
audio/echo/source/main.c Normal file
View File

@ -0,0 +1,148 @@
#include <string.h>
#include <stdio.h>
#include <malloc.h>
#include <math.h>
#include <switch.h>
#define SAMPLERATE 48000
#define CHANNELCOUNT 2
#define FRAMERATE (1000 / 30)
#define SAMPLECOUNT (SAMPLERATE / FRAMERATE)
#define BYTESPERSAMPLE 2
int main(int argc, char **argv)
{
Result rc = 0;
gfxInitDefault();
// Initialize console. Using NULL as the second argument tells the console library to use the internal console structure as current one.
consoleInit(NULL);
AudioInBuffer audin_buf;
AudioOutBuffer audout_buf;
AudioInBuffer *released_in_buffer;
AudioOutBuffer *released_out_buffer;
u32 released_in_count;
u32 released_out_count;
// Make sure the sample buffer size is aligned to 0x1000 bytes.
u32 data_size = (SAMPLECOUNT * CHANNELCOUNT * BYTESPERSAMPLE);
u32 buffer_size = (data_size + 0xfff) & ~0xfff;
// Allocate the buffers.
u8* in_buf_data = memalign(0x1000, buffer_size);
u8* out_buf_data = memalign(0x1000, buffer_size);
// Ensure buffers were properly allocated.
if ((in_buf_data == NULL) || (out_buf_data == NULL))
{
rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
printf("Failed to allocate sample data buffers\n");
}
if (R_SUCCEEDED(rc))
{
memset(in_buf_data, 0, buffer_size);
memset(out_buf_data, 0, buffer_size);
}
if (R_SUCCEEDED(rc))
{
// Initialize the default audio input device.
rc = audinInitialize();
printf("audinInitialize() returned 0x%x\n", rc);
}
if (R_SUCCEEDED(rc))
{
// Initialize the default audio output device.
rc = audoutInitialize();
printf("audoutInitialize() returned 0x%x\n", rc);
}
if (R_SUCCEEDED(rc))
{
// Start audio capture.
rc = audinStartAudioIn();
printf("audinStartAudioIn() returned 0x%x\n", rc);
}
if (R_SUCCEEDED(rc))
{
// Start audio playback.
rc = audoutStartAudioOut();
printf("audoutStartAudioOut() returned 0x%x\n", rc);
}
// Prepare the input buffer.
audin_buf.next = NULL;
audin_buf.buffer = in_buf_data;
audin_buf.buffer_size = buffer_size;
audin_buf.data_size = data_size;
audin_buf.data_offset = 0;
// Prepare the output buffer.
audout_buf.next = NULL;
audout_buf.buffer = out_buf_data;
audout_buf.buffer_size = buffer_size;
audout_buf.data_size = data_size;
audout_buf.data_offset = 0;
// Prepare pointers and counters for released buffers.
released_in_buffer = NULL;
released_out_buffer = NULL;
released_in_count = 0;
released_out_count = 0;
// Append the initial input buffer.
rc = audinAppendAudioInBuffer(&audin_buf);
printf("audinAppendAudioInBuffer() returned 0x%x\n", rc);
// Append the initial output buffer.
rc = audoutAppendAudioOutBuffer(&audout_buf);
printf("audoutAppendAudioOutBuffer() returned 0x%x\n", rc);
while (appletMainLoop())
{
//Scan all the inputs. This should be done once for each frame
hidScanInput();
//hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame)
u32 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu
// Wait for audio capture and playback to finish.
audinWaitCaptureFinish(&released_in_buffer, &released_in_count, U64_MAX);
audoutWaitPlayFinish(&released_out_buffer, &released_out_count, U64_MAX);
// Copy the captured audio data into the playback buffer.
if ((released_in_buffer != NULL) && (released_out_buffer != NULL))
memcpy(released_out_buffer->buffer, released_in_buffer->buffer, released_in_buffer->data_size);
// Append the released buffers again.
audinAppendAudioInBuffer(released_in_buffer);
audoutAppendAudioOutBuffer(released_out_buffer);
gfxFlushBuffers();
gfxSwapBuffers();
gfxWaitForVsync();
}
// Stop audio capture.
rc = audinStopAudioIn();
printf("audinStopAudioIn() returned 0x%x\n", rc);
// Stop audio playback.
rc = audoutStopAudioOut();
printf("audoutStopAudioOut() returned 0x%x\n", rc);
// Terminate the default audio devices.
audinExit();
audoutExit();
gfxExit();
return 0;
}

View File

@ -6,8 +6,10 @@
#include <switch.h> #include <switch.h>
#define SAMPLERATE 48000 #define SAMPLERATE 48000
#define SAMPLESPERBUF (SAMPLERATE / 10) #define CHANNELCOUNT 2
#define BYTESPERSAMPLE 4 #define FRAMERATE (1000 / 30)
#define SAMPLECOUNT (SAMPLERATE / FRAMERATE)
#define BYTESPERSAMPLE 2
void fill_audio_buffer(void* audio_buffer, size_t offset, size_t size, int frequency) { void fill_audio_buffer(void* audio_buffer, size_t offset, size_t size, int frequency) {
if (audio_buffer == NULL) return; if (audio_buffer == NULL) return;
@ -25,8 +27,6 @@ void fill_audio_buffer(void* audio_buffer, size_t offset, size_t size, int frequ
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
Result rc = 0; Result rc = 0;
AudioOutBuffer source_buffer;
AudioOutBuffer released_buffer;
int notefreq[] = { int notefreq[] = {
220, 220,
@ -40,24 +40,29 @@ int main(int argc, char **argv)
// Initialize console. Using NULL as the second argument tells the console library to use the internal console structure as current one. // Initialize console. Using NULL as the second argument tells the console library to use the internal console structure as current one.
consoleInit(NULL); consoleInit(NULL);
// Make sure the sample buffer is aligned to 0x1000 bytes AudioOutBuffer audout_buf;
u32 raw_data_size = (SAMPLESPERBUF * BYTESPERSAMPLE * 2); AudioOutBuffer *audout_released_buf;
u32 raw_data_size_aligned = (raw_data_size + 0xfff) & ~0xfff;
u8* raw_data = memalign(0x1000, raw_data_size_aligned);
// Ensure buffer was properly allocated // Make sure the sample buffer size is aligned to 0x1000 bytes.
if (raw_data == NULL) u32 data_size = (SAMPLECOUNT * CHANNELCOUNT * BYTESPERSAMPLE);
u32 buffer_size = (data_size + 0xfff) & ~0xfff;
// Allocate the buffer.
u8* out_buf_data = memalign(0x1000, buffer_size);
// Ensure buffers were properly allocated.
if (out_buf_data == NULL)
{ {
rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory); rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
printf("Failed to allocate sample data buffer\n"); printf("Failed to allocate sample data buffers\n");
} }
if (R_SUCCEEDED(rc)) if (R_SUCCEEDED(rc))
memset(raw_data, 0, raw_data_size_aligned); memset(out_buf_data, 0, buffer_size);
if (R_SUCCEEDED(rc)) if (R_SUCCEEDED(rc))
{ {
// Initialize the default audio output device // Initialize the default audio output device.
rc = audoutInitialize(); rc = audoutInitialize();
printf("audoutInitialize() returned 0x%x\n", rc); printf("audoutInitialize() returned 0x%x\n", rc);
} }
@ -89,91 +94,95 @@ int main(int argc, char **argv)
if (kDown & KEY_A) if (kDown & KEY_A)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[0]); fill_audio_buffer(out_buf_data, 0, data_size, notefreq[0]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_B) if (kDown & KEY_B)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[1]); fill_audio_buffer(out_buf_data, 0, data_size, notefreq[1]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_Y) if (kDown & KEY_Y)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[2]); fill_audio_buffer(out_buf_data, 0, data_size, notefreq[2]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_X) if (kDown & KEY_X)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[3]); fill_audio_buffer(out_buf_data, 0, data_size, notefreq[3]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_DLEFT) if (kDown & KEY_DLEFT)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[4]); fill_audio_buffer(out_buf_data, 0, data_size, notefreq[4]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_DUP) if (kDown & KEY_DUP)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[5]); fill_audio_buffer(out_buf_data, 0, data_size, notefreq[5]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_DRIGHT) if (kDown & KEY_DRIGHT)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[6]); fill_audio_buffer(out_buf_data, 0, data_size, notefreq[6]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_DDOWN) if (kDown & KEY_DDOWN)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[7]); fill_audio_buffer(out_buf_data, 0, data_size, notefreq[7]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_L) if (kDown & KEY_L)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[8]); fill_audio_buffer(out_buf_data, 0, data_size, notefreq[8]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_R) if (kDown & KEY_R)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[9]); fill_audio_buffer(out_buf_data, 0, data_size, notefreq[9]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_ZL) if (kDown & KEY_ZL)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[10]); fill_audio_buffer(out_buf_data, 0, data_size, notefreq[10]);
play_tone = true; play_tone = true;
} }
if (kDown & KEY_ZR) if (kDown & KEY_ZR)
{ {
fill_audio_buffer(raw_data, 0, SAMPLESPERBUF * 2, notefreq[11]); fill_audio_buffer(out_buf_data, 0, data_size, notefreq[11]);
play_tone = true; play_tone = true;
} }
if (R_SUCCEEDED(rc) && play_tone) if (R_SUCCEEDED(rc) && play_tone)
{ {
// Prepare the audio data source buffer. // Prepare the audio data source buffer.
source_buffer.next = 0; audout_buf.next = NULL;
source_buffer.buffer = raw_data; audout_buf.buffer = out_buf_data;
source_buffer.buffer_size = raw_data_size; audout_buf.buffer_size = buffer_size;
source_buffer.data_size = SAMPLESPERBUF * 2; audout_buf.data_size = data_size;
source_buffer.data_offset = 0; audout_buf.data_offset = 0;
// Play this buffer once. // Prepare pointer for the released buffer.
rc = audoutPlayBuffer(&source_buffer, &released_buffer); audout_released_buf = NULL;
play_tone = false;
// Play the buffer.
rc = audoutPlayBuffer(&audout_buf, &audout_released_buf);
if (R_FAILED(rc)) if (R_FAILED(rc))
printf("audoutPlayBuffer() returned 0x%x\n", rc); printf("audoutPlayBuffer() returned 0x%x\n", rc);
play_tone = false;
} }
gfxFlushBuffers(); gfxFlushBuffers();