#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define SAMPLERATE 22050
#define SAMPLESPERBUF (SAMPLERATE / 30)
#define BYTESPERSAMPLE 4
#define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
void fill_buffer(void* audioBuffer, size_t offset, size_t size, int frequency) {
u32* dest = (
u32*) audioBuffer;
for (int i = 0; i < size; i++) {
s16 sample = 0.3 * 0x7FFF * sin(frequency * (2 * M_PI) * (offset + i) / SAMPLERATE);
dest[i] = (sample << 16) | (sample & 0xffff);
}
}
int main(int argc, char **argv) {
printf("libctru filtered streamed audio\n");
bool fillBlock = false;
float mix[12];
memset(mix, 0, sizeof(mix));
mix[0] = 1.0;
mix[1] = 1.0;
int notefreq[] = {
220,
440, 880, 1760, 3520, 7040,
14080,
7040, 3520, 1760, 880, 440
};
int note = 4;
const char* filter_names[] = {
"None",
"Low-Pass",
"High-Pass",
"Band-Pass",
"Notch",
"Peaking"
};
int filter = 0;
ndspWaveBuf waveBuf[2];
memset(waveBuf,0,sizeof(waveBuf));
waveBuf[0].data_vaddr = &audioBuffer[0];
waveBuf[0].nsamples = SAMPLESPERBUF;
waveBuf[1].data_vaddr = &audioBuffer[SAMPLESPERBUF];
waveBuf[1].nsamples = SAMPLESPERBUF;
size_t stream_offset = 0;
fill_buffer(audioBuffer,stream_offset, SAMPLESPERBUF * 2, notefreq[note]);
stream_offset += SAMPLESPERBUF;
printf("Press up/down to change tone frequency\n");
printf("Press left/right to change filter\n");
printf("\x1b[6;1Hnote = %i Hz ", notefreq[note]);
printf("\x1b[7;1Hfilter = %s ", filter_names[filter]);
break;
note--;
if (note < 0) {
note = ARRAY_SIZE(notefreq) - 1;
}
printf("\x1b[6;1Hnote = %i Hz ", notefreq[note]);
note++;
if (note >= ARRAY_SIZE(notefreq)) {
note = 0;
}
printf("\x1b[6;1Hnote = %i Hz ", notefreq[note]);
}
bool update_params = false;
filter--;
if (filter < 0) {
filter = ARRAY_SIZE(filter_names) - 1;
}
update_params = true;
filter++;
if (filter >= ARRAY_SIZE(filter_names)) {
filter = 0;
}
update_params = true;
}
if (update_params) {
printf("\x1b[7;1Hfilter = %s ", filter_names[filter]);
switch (filter) {
default:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
}
fill_buffer(waveBuf[fillBlock].data_pcm16, stream_offset, waveBuf[fillBlock].nsamples, notefreq[note]);
stream_offset += waveBuf[fillBlock].nsamples;
fillBlock = !fillBlock;
}
}
return 0;
}
bool aptMainLoop(void)
Main function which handles sleep mode and HOME/power buttons - call this at the beginning of every f...
@ NDSP_FORMAT_STEREO_PCM16
Buffer contains Stereo PCM16.
Definition: channel.h:29
void ndspChnSetMix(int id, float mix[12])
Sets the mix parameters (volumes) of a channel.
void ndspChnWaveBufAdd(int id, ndspWaveBuf *buf)
Adds a wave buffer to the wave buffer queue of a channel.
@ NDSP_INTERP_LINEAR
Linear interpolation.
Definition: channel.h:44
bool ndspChnIirBiquadSetParamsNotchFilter(int id, float f0, float Q)
Sets the biquad to be a notch filter.
void ndspChnSetRate(int id, float rate)
Sets the sample rate of a channel.
bool ndspChnIirBiquadSetParamsLowPassFilter(int id, float f0, float Q)
Sets the biquad to be a low pass filter.
void ndspChnSetFormat(int id, u16 format)
Sets the format of a channel.
bool ndspChnIirBiquadSetParamsHighPassFilter(int id, float f0, float Q)
Sets the biquad to be a high pass filter.
bool ndspChnIirBiquadSetParamsPeakingEqualizer(int id, float f0, float Q, float gain)
Sets the biquad to be a peaking equalizer.
void ndspChnIirBiquadSetEnable(int id, bool enable)
Configures whether the IIR biquad filter of a channel is enabled.
void ndspChnSetInterp(int id, ndspInterpType type)
Sets the interpolation type of a channel.
bool ndspChnIirBiquadSetParamsBandPassFilter(int id, float f0, float Q)
Sets the biquad to be a band pass filter.
PrintConsole * consoleInit(gfxScreen_t screen, PrintConsole *console)
Initialise the console.
PrintConsole * consoleSelect(PrintConsole *console)
Make the specified console the render target.
Result DSP_FlushDataCache(const void *address, u32 size)
Flushes the cache.
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_TOP
Top screen.
Definition: gfx.h:26
void gfxExit(void)
Deinitializes and frees the LCD framebuffers.
void gfxFlushBuffers(void)
Flushes the data cache for the current framebuffers.
#define gspWaitForVBlank()
Waits for VBlank.
Definition: gspgpu.h:151
@ KEY_UP
D-Pad Up or Circle Pad Up.
Definition: hid.h:37
@ KEY_LEFT
D-Pad Left or Circle Pad Left.
Definition: hid.h:39
@ KEY_START
Start.
Definition: hid.h:15
@ KEY_RIGHT
D-Pad Right or Circle Pad Right.
Definition: hid.h:40
@ KEY_DOWN
D-Pad Down or Circle Pad Down.
Definition: hid.h:38
u32 hidKeysDown(void)
Returns a bitmask of newly pressed buttons, this frame.
void hidScanInput(void)
Scans HID for input data.
void * linearAlloc(size_t size)
Allocates a 0x80-byte aligned buffer.
void linearFree(void *mem)
Frees a buffer.
void ndspSetOutputMode(ndspOutputMode mode)
Sets the output mode.
@ NDSP_WBUF_DONE
The wave buffer has finished being played.
Definition: ndsp.h:53
void ndspExit(void)
Exits NDSP.
@ NDSP_OUTPUT_STEREO
Stereo sound.
Definition: ndsp.h:17
Result ndspInit(void)
Initializes NDSP.
Console structure used to store the state of a console render context.
Definition: console.h:77
int16_t s16
16-bit signed integer
Definition: types.h:27
uint32_t u32
32-bit unsigned integer
Definition: types.h:23