forked from Oaisus/libansnd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
180 lines (142 loc) · 4.63 KB
/
main.c
File metadata and controls
180 lines (142 loc) · 4.63 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <gccore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <math.h>
#include <ansndlib.h>
static void print_error(s32 error);
static void setup_video();
int main(int argc, char** argv) {
// setup text terminal and controller input
setup_video();
printf("ansnd library example program: simple looping\n");
printf("Initializing ansnd library...\n");
ansnd_initialize();
printf("ansnd library initialized.\n");
printf("Generating audio data...\n");
// generate a simple 500Hz sine wave
u32 sound_length_seconds = 1;
u32 sound_sample_rate = 48000;
u32 sound_frequency_hz = 500;
// create a buffer aligned at a 32 byte boundary
u32 sound_samples = sound_sample_rate * sound_length_seconds;
u32 sound_buffer_size = sound_samples * 2;
s16* sound_buffer = (s16*)memalign(32, sound_buffer_size);
// generate samples
for (u32 i = 0; i < sound_samples; ++i) {
sound_buffer[i] = sin(2 * M_PI * sound_frequency_hz * ((f32)i / (f32)sound_sample_rate)) * 0.95f * 32767;
}
// flush the sound data from the CPU cache
DCFlushRange(sound_buffer, sound_buffer_size);
#if defined(HW_DOL)
// on the GameCube, sound samples need to be transferred to ARAM before they can be used
const u32 aram_buffers = 1;
u32 aram_memory[aram_buffers];
AR_Init(aram_memory, aram_buffers);
ARQ_Init();
u32 sound_buffer_ptr = AR_Alloc(sound_buffer_size);
ARQRequest aram_request;
ARQ_PostRequest(&aram_request, 0, ARQ_MRAMTOARAM, ARQ_PRIO_HI, sound_buffer_ptr, MEM_VIRTUAL_TO_PHYSICAL(sound_buffer), sound_buffer_size);
#elif defined(HW_RVL)
// Wii just needs to convert the pointer from virtual to physical
u32 sound_buffer_ptr = MEM_VIRTUAL_TO_PHYSICAL(sound_buffer);
#endif
// create a voice config struct
ansnd_pcm_voice_config_t voice_config;
memset(&voice_config, 0, sizeof(ansnd_pcm_voice_config_t));
voice_config.samplerate = sound_sample_rate;
voice_config.format = ANSND_VOICE_PCM_FORMAT_SIGNED_16_PCM;
voice_config.channels = 1;
voice_config.pitch = 1.0f;
voice_config.left_volume = 0.5f;
voice_config.right_volume = 0.5f;
voice_config.frame_data_ptr = sound_buffer_ptr;
voice_config.frame_count = sound_samples;
voice_config.start_offset = 0;
// set voice to loop through the entire sound buffer
voice_config.loop_start_offset = 0;
voice_config.loop_end_offset = voice_config.frame_count;
printf("Allocating voice...\n");
s32 voice_id = ansnd_allocate_voice();
if (voice_id < 0) {
print_error(voice_id);
printf("Voice allocation failed.\n");
printf("Exiting...\n");
VIDEO_WaitVSync();
return 0;
}
printf("Voice allocation complete.\n");
printf("Configuring voice...\n");
s32 error = ansnd_configure_pcm_voice(voice_id, &voice_config);
if (error < 0) {
print_error(error);
printf("Voice configuration failed.\n");
printf("Exiting...\n");
VIDEO_WaitVSync();
return 0;
}
printf("Voice configuration complete.\n");
printf("\n\nGenerated sound:\n");
printf("\t%dHz sine wave\n", sound_frequency_hz);
printf("\t%d seconds long\n", sound_length_seconds);
printf("\n\nPress A to play.\n");
printf("Press B to stop.\n");
printf("\n\nPress the START button to exit.\n\n");
while (true) {
PAD_ScanPads();
u32 pressed_buttons = PAD_ButtonsDown(0);
if (pressed_buttons & PAD_BUTTON_START) {
break;
}
if (pressed_buttons & PAD_BUTTON_A) {
error = ansnd_start_voice(voice_id);
print_error(error);
}
if (pressed_buttons & PAD_BUTTON_B) {
error = ansnd_stop_voice(voice_id);
print_error(error);
}
// Wait for the next frame
VIDEO_WaitVSync();
}
printf("Deallocating voice\n");
error = ansnd_deallocate_voice(voice_id);
if (error < 0) {
printf("Voice deallocation failed.\n");
}
printf("Voice deallocated.\n");
printf("Shutting down ansnd library...\n");
ansnd_uninitialize();
free(sound_buffer);
#if defined(HW_DOL)
for (u32 i = 0; i < aram_buffers; ++i) {
AR_Free(NULL);
}
#endif
printf("Exiting...\n");
// Update the screen one last time
VIDEO_WaitVSync();
return 0;
}
static void print_error(s32 error) {
if (error == ANSND_ERROR_OK) {
return;
}
printf("\tansnd library Error %d:\n\t\t", error);
printf("%s\n", ansnd_get_error_string(error));
}
static void *xfb = NULL;
static void setup_video() {
VIDEO_Init();
PAD_Init();
GXRModeObj* rmode = VIDEO_GetPreferredMode(NULL);
xfb = SYS_AllocateFramebuffer(rmode);
CON_Init(xfb, 0, 0, rmode->fbWidth, rmode->xfbHeight, rmode->fbWidth * VI_DISPLAY_PIX_SZ);
VIDEO_Configure(rmode);
VIDEO_SetNextFramebuffer(xfb);
VIDEO_SetBlack(false);
VIDEO_Flush();
VIDEO_WaitForFlush();
printf("\nTerminal Output Initialized\n");
}