-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.c
183 lines (146 loc) · 4.62 KB
/
audio.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
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
181
182
183
/*
██ ▀███ ██
▄██▄ ██
▄█▀██▄ ▀███ ▀███ ▄█▀▀███ ▀███ ▄██▀██▄
▄█ ▀██ ██ ██ ▄██ ██ ██ ██▀ ▀██
████████ ██ ██ ███ ██ ██ ██ ██
█▀ ██ ██ ██ ▀██ ██ ██ ██▄ ▄██
▄███▄ ▄████▄ ▀████▀███▄ ▀████▀███▄████▄ ▀█████▀
*/
#define MINIAUDIO_IMPLEMENTATION
#define MA_NO_MP3
#define MA_NO_FLAC
#include "./inc/miniaudio.h"
#include "./inc/giraffe.h"
#include "./inc/debugmalloc.h"
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
/* GLOBAL VARIABLES (MiniAudio) (SORRY) */
// Decoder
ma_decoder decoder;
ma_result resultDecoder;
ma_result result;
// Length
ma_uint64 length;
ma_result resultFrameSec;
// Resource manager
ma_result resultRm;
ma_resource_manager_config config;
ma_resource_manager resourceManager;
// Audio engine
ma_uint64 cursor;
ma_uint64 resultCursor;
ma_engine engine;
ma_result resultSound;
ma_sound sound;
char* dir = "/home/i3hunor/Giraffe/songs/";
int songSelect(char** array, int sizeArray){
int selected = 0;
/* Print the songs in the list */
for(int i = 0; i < sizeArray; i++){
printf("%d\t| %s\n",i+1, array[i]);
}
/* Stores the selected songs index */
printf("Select a song! ");
scanf("%d", &selected);
return selected-1;
}
/* RETURNS THE VALUE OF THE READ FILE LENGTH IN SEC */
int lenInSec(){
resultFrameSec = ma_data_source_get_length_in_pcm_frames(&decoder , &length);
if (resultFrameSec != MA_SUCCESS) {return result;}
return (int) length / (float) decoder.outputSampleRate;
}
/* RETURNS THE TIME THAT HAS ELLAPSED SINCE THE START OF THE SONG */
int songCurrSec() {
float currSec;
result = ma_sound_get_cursor_in_seconds(&sound, &currSec);
if (MA_SUCCESS != result) return -1;
return (int) currSec;
}
void restartSong(){
ma_sound_seek_to_pcm_frame(&sound, 0);
}
/* PROGRESS "BAR" */
void progressBar(){
int iter = 0;
while(songFinished != 0){
//printf("result = %d ", (int) cursor);
printf("\r%i/%i | %d", iter, lenInSec(), songCurrSec());
fflush(stdout);
sleep(1);
iter++;
}
}
/* DECODES THE .WAV FILE, SO I CAN READ INFORMATION ABOUT IT
* FOR EXAMPLE LENGTH IN SEC ETC.*/
int decodeSong(char* filePath){
/* Decode */
resultDecoder = ma_decoder_init_file(filePath, NULL, &decoder); // Decode inicial
if (resultDecoder != MA_SUCCESS) {
printf("Decode error\n");
return -1;
}
return 0;
}
/* INITIALIZE THE RESOURCE MANAGER AND THE SOUND Engine */
int setupMA(){
/* ResourceManager */
config = ma_resource_manager_config_init();
resultRm = ma_resource_manager_init(&config, &resourceManager);
if (resultRm != MA_SUCCESS) {
printf("ResourceManager error");
return -1;
}
/* Sound engine */
resultSound = ma_engine_init(NULL, &engine);
if (resultSound != MA_SUCCESS) {
printf("Engine error");
return -1;
}
return 0;
}
/* RETURNS 1 IF THE SONG IS FINISHED AND 0 IF IT IS STILL PLAYING */
int songFinished() {
if (ma_sound_at_end(&sound)) {
return 1;
}
return 0;
}
void playSong(char* songFile){
ma_sound_init_from_file(&engine, songFile , MA_SOUND_FLAG_STREAM, NULL , NULL, &sound);
while (songFinished() != 1) {
ma_sound_start(&sound);
}
}
/* RETURNS IF A SONG IS CURRENTLY PLAYING */
int isPlaying(){
return ma_sound_is_playing(&sound);
}
/* MiniAudio CLEANUP, BASICALLY FREES THE ALLOCATED MEMORY BY THE
* MA ENGINE AND DECODER*/
void cleaupMA(){
ma_decoder_uninit(&decoder);
ma_engine_uninit(&engine);
}
/*
int main(){
char* dir = "./songs/";
int songArraySize; // size of the songList
char** songArray = dir_read(dir, &songArraySize); // Reading dir MEMORY ALREDY ALLOCATED
int selected = songSelect(songArray, songArraySize);
char* songPath = concat(dir, songArray[selected]);
char* fp = songPath;
decodeSong(fp);
setupMA();
INIT + PLAYING THE SELECTED SONG
ma_sound_init_from_file(&engine, fp, MA_SOUND_FLAG_STREAM, NULL , NULL, &sound);
ma_sound_start(&sound);
//ma_engine_play_sound(&engine, fp, NULL);
COMMENT : ITTENI SORRAL RENDESEN MŰKÖDIK A PROGRESS BAR
if(resultSound == MA_SUCCESS) printf("Currently playing: %s \n", fp);
progesssBar(resultSound);
void cleaupMA();
}
*/