audio/mic/source/main.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <3ds.h>
int main()
{
gfxInitDefault();
consoleInit(GFX_BOTTOM, NULL);
bool initialized = true;
u32 micbuf_size = 0x30000;
u32 micbuf_pos = 0;
u8* micbuf = memalign(0x1000, micbuf_size);
printf("Initializing CSND...\n");
if(R_FAILED(csndInit()))
{
initialized = false;
printf("Could not initialize CSND.\n");
} else printf("CSND initialized.\n");
printf("Initializing MIC...\n");
if(R_FAILED(micInit(micbuf, micbuf_size)))
{
initialized = false;
printf("Could not initialize MIC.\n");
} else printf("MIC initialized.\n");
u32 micbuf_datasize = micGetSampleDataSize();
u32 audiobuf_size = 0x100000;
u32 audiobuf_pos = 0;
u8* audiobuf = linearAlloc(audiobuf_size);
if(initialized) printf("Hold A to record, release to play.\n");
printf("Press START to exit.\n");
while(aptMainLoop())
{
hidScanInput();
gspWaitForVBlank();
u32 kDown = hidKeysDown();
if (kDown & KEY_START)
break; // break in order to return to hbmenu
if(initialized)
{
if(kDown & KEY_A)
{
audiobuf_pos = 0;
micbuf_pos = 0;
printf("Stopping audio playback...\n");
CSND_SetPlayState(0x8, 0);
if(R_FAILED(CSND_UpdateInfo(0))) printf("Failed to stop audio playback.\n");
printf("Starting sampling...\n");
if(R_SUCCEEDED(MICU_StartSampling(MICU_ENCODING_PCM16_SIGNED, MICU_SAMPLE_RATE_16360, 0, micbuf_datasize, true))) printf("Now recording.\n");
else printf("Failed to start sampling.\n");
}
if((hidKeysHeld() & KEY_A) && audiobuf_pos < audiobuf_size)
{
u32 micbuf_readpos = micbuf_pos;
micbuf_pos = micGetLastSampleOffset();
while(audiobuf_pos < audiobuf_size && micbuf_readpos != micbuf_pos)
{
audiobuf[audiobuf_pos] = micbuf[micbuf_readpos];
audiobuf_pos++;
micbuf_readpos = (micbuf_readpos + 1) % micbuf_datasize;
}
}
if(hidKeysUp() & KEY_A)
{
printf("Stoping sampling...\n");
if(R_FAILED(MICU_StopSampling())) printf("Failed to stop sampling.\n");
printf("Starting audio playback...\n");
if(R_SUCCEEDED(GSPGPU_FlushDataCache(audiobuf, audiobuf_pos)) && R_SUCCEEDED(csndPlaySound(0x8, SOUND_ONE_SHOT | SOUND_FORMAT_16BIT, 16360, 1.0, 0.0, (u32*)audiobuf, NULL, audiobuf_pos))) printf("Now playing.\n");
else printf("Failed to start playback.\n");
}
}
gfxFlushBuffers();
gfxSwapBuffers();
}
linearFree(audiobuf);
micExit();
free(micbuf);
csndExit();
gfxExit();
return 0;
}
3ds.h
Central 3DS header.
aptMainLoop
bool aptMainLoop(void)
Main function which handles sleep mode and HOME/power buttons - call this at the beginning of every f...
consoleInit
PrintConsole * consoleInit(gfxScreen_t screen, PrintConsole *console)
Initialise the console.
CSND_UpdateInfo
Result CSND_UpdateInfo(bool waitDone)
Updates CSND information.
csndInit
Result csndInit(void)
Initializes CSND.
csndPlaySound
Result csndPlaySound(int chn, u32 flags, u32 sampleRate, float vol, float pan, void *data0, void *data1, u32 size)
Plays a sound.
CSND_SetPlayState
void CSND_SetPlayState(u32 channel, u32 value)
Sets a channel's play state.
SOUND_FORMAT_16BIT
@ SOUND_FORMAT_16BIT
PCM16.
Definition: csnd.h:72
SOUND_ONE_SHOT
@ SOUND_ONE_SHOT
Play the sound once.
Definition: csnd.h:70
csndExit
void csndExit(void)
Exits CSND.
gfxSwapBuffers
void gfxSwapBuffers(void)
Updates the configuration of both screens.
gfxInitDefault
void gfxInitDefault(void)
Initializes the LCD framebuffers with default parameters This is equivalent to calling:
GFX_BOTTOM
@ GFX_BOTTOM
Bottom screen.
Definition: gfx.h:27
gfxExit
void gfxExit(void)
Deinitializes and frees the LCD framebuffers.
gfxFlushBuffers
void gfxFlushBuffers(void)
Flushes the data cache for the current framebuffers.
GSPGPU_FlushDataCache
Result GSPGPU_FlushDataCache(const void *adr, u32 size)
Flushes memory from the data cache.
gspWaitForVBlank
#define gspWaitForVBlank()
Waits for VBlank.
Definition: gspgpu.h:151
KEY_START
@ KEY_START
Start.
Definition: hid.h:15
KEY_A
@ KEY_A
A.
Definition: hid.h:12
hidKeysHeld
u32 hidKeysHeld(void)
Returns a bitmask of held buttons.
hidKeysDown
u32 hidKeysDown(void)
Returns a bitmask of newly pressed buttons, this frame.
hidScanInput
void hidScanInput(void)
Scans HID for input data.
hidKeysUp
u32 hidKeysUp(void)
Returns a bitmask of newly released buttons, this frame.
linearAlloc
void * linearAlloc(size_t size)
Allocates a 0x80-byte aligned buffer.
linearFree
void linearFree(void *mem)
Frees a buffer.
micInit
Result micInit(u8 *buffer, u32 bufferSize)
Initializes MIC.
micGetSampleDataSize
u32 micGetSampleDataSize(void)
Gets the size of the sample data area within the shared memory buffer.
MICU_ENCODING_PCM16_SIGNED
@ MICU_ENCODING_PCM16_SIGNED
Signed 16-bit PCM.
Definition: mic.h:13
micExit
void micExit(void)
Exits MIC.
MICU_StartSampling
Result MICU_StartSampling(MICU_Encoding encoding, MICU_SampleRate sampleRate, u32 offset, u32 size, bool loop)
Begins sampling microphone input.
MICU_StopSampling
Result MICU_StopSampling(void)
Stops sampling microphone input.
micGetLastSampleOffset
u32 micGetLastSampleOffset(void)
Gets the offset within the shared memory buffer of the last sample written.
MICU_SAMPLE_RATE_16360
@ MICU_SAMPLE_RATE_16360
16364.479 Hz
Definition: mic.h:20
R_FAILED
#define R_FAILED(res)
Checks whether a result code indicates failure.
Definition: result.h:11
R_SUCCEEDED
#define R_SUCCEEDED(res)
Checks whether a result code indicates success.
Definition: result.h:9
u8
uint8_t u8
would be nice if newlib had this already
Definition: types.h:21
u32
uint32_t u32
32-bit unsigned integer
Definition: types.h:23