/
main.c
120 lines (99 loc) · 3.49 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <inttypes.h>
#include <switch.h>
#include "sample_bin.h"
// Sample comes from this website:
// https://www.soundjay.com/magic-sound-effect.html
int main(void)
{
consoleInit(NULL);
// Configure our supported input layout: a single player with standard controller styles
padConfigureInput(1, HidNpadStyleSet_NpadStandard);
// Initialize the default gamepad (which reads handheld mode inputs as well as the first connected controller)
PadState pad;
padInitializeDefault(&pad);
printf("Simple audren demonstration program\n");
static const AudioRendererConfig arConfig =
{
.output_rate = AudioRendererOutputRate_48kHz,
.num_voices = 24,
.num_effects = 0,
.num_sinks = 1,
.num_mix_objs = 1,
.num_mix_buffers = 2,
};
size_t mempool_size = (sample_bin_size + 0xFFF) &~ 0xFFF;
void* mempool_ptr = memalign(0x1000, mempool_size);
memcpy(mempool_ptr, sample_bin, sample_bin_size);
armDCacheFlush(mempool_ptr, mempool_size);
AudioDriverWaveBuf wavebuf = {0};
wavebuf.data_raw = mempool_ptr;
wavebuf.size = sample_bin_size;
wavebuf.start_sample_offset = 0;
wavebuf.end_sample_offset = sample_bin_size/2;
//wavebuf.is_looping = true;
AudioDriver drv;
Result res;
res = audrenInitialize(&arConfig);
bool initedDriver = false;
bool initedAudren = R_SUCCEEDED(res);
if (!initedAudren)
printf("audrenInitialize: %08" PRIx32 "\n", res);
else
{
printf("audren initted!\n");
res = audrvCreate(&drv, &arConfig, 2);
initedDriver = R_SUCCEEDED(res);
if (R_FAILED(res))
printf("audrvCreate: %08" PRIx32 "\n", res);
else
{
int mpid = audrvMemPoolAdd(&drv, mempool_ptr, mempool_size);
audrvMemPoolAttach(&drv, mpid);
static const u8 sink_channels[] = { 0, 1 };
/*int sink =*/ audrvDeviceSinkAdd(&drv, AUDREN_DEFAULT_DEVICE_NAME, 2, sink_channels);
res = audrvUpdate(&drv);
printf("audrvUpdate: %" PRIx32 "\n", res);
res = audrenStartAudioRenderer();
printf("audrenStartAudioRenderer: %" PRIx32 "\n", res);
audrvVoiceInit(&drv, 0, 1, PcmFormat_Int16, 48000);
audrvVoiceSetDestinationMix(&drv, 0, AUDREN_FINAL_MIX_ID);
audrvVoiceSetMixFactor(&drv, 0, 1.0f, 0, 0);
audrvVoiceSetMixFactor(&drv, 0, 1.0f, 0, 1);
audrvVoiceStart(&drv, 0);
}
}
printf("done. Press A to play a sound.\n");
// Main loop
while (appletMainLoop())
{
padUpdate(&pad);
u64 kDown = padGetButtonsDown(&pad);
if (kDown & HidNpadButton_Plus)
break;
if (initedDriver)
{
if (kDown & HidNpadButton_A)
{
audrvVoiceStop(&drv, 0);
audrvVoiceAddWaveBuf(&drv, 0, &wavebuf);
audrvVoiceStart(&drv, 0);
}
res = audrvUpdate(&drv);
if (R_FAILED(res))
printf("audrvUpdate: %" PRIx32 "\n", res);
if (wavebuf.state == AudioDriverWaveBufState_Playing)
printf("sample count = %" PRIu32 "\n", audrvVoiceGetPlayedSampleCount(&drv, 0));
}
consoleUpdate(NULL);
}
if (initedDriver)
audrvClose(&drv);
if (initedAudren)
audrenExit();
consoleExit(NULL);
return 0;
}