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