93 lines
2.0 KiB
C
93 lines
2.0 KiB
C
#include <ao/ao.h>
|
|
#include <mpg123.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include "csoundbox.h"
|
|
#include "config.h"
|
|
#include <string.h>
|
|
#include <ncurses.h>
|
|
#define BITS 8
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
cfginit();
|
|
|
|
inputLocal();
|
|
cfgdestroy();
|
|
return 0;
|
|
}
|
|
|
|
void inputLocal(void) {
|
|
initscr();
|
|
nonl(); //no newline
|
|
noecho();
|
|
keypad(stdscr, FALSE); //Disable the F1-12 keypad
|
|
curs_set(0); //Disable the cursor
|
|
|
|
printCursesWelcome();
|
|
|
|
char input;
|
|
while ((input = getch()) != 13) {
|
|
playSound(lookupSounds(input));
|
|
}
|
|
|
|
endwin();
|
|
}
|
|
|
|
void printCursesWelcome(void) {
|
|
int y, x;
|
|
getmaxyx(stdscr, y, x);
|
|
//center
|
|
y /= 2;
|
|
x /= 2;
|
|
const char *msg = "Press enter to exit.";
|
|
mvprintw(y, x - (strlen(msg) / 2), msg);
|
|
}
|
|
|
|
void playSound(const char *path) {
|
|
mpg123_handle *mh;
|
|
unsigned char *buffer;
|
|
size_t buffer_size;
|
|
size_t done;
|
|
int err;
|
|
|
|
int driver;
|
|
ao_device *dev;
|
|
|
|
ao_sample_format format;
|
|
int channels, encoding;
|
|
long rate;
|
|
|
|
/* initializations */
|
|
ao_initialize();
|
|
driver = ao_default_driver_id();
|
|
mpg123_init();
|
|
mh = mpg123_new(NULL, &err);
|
|
buffer_size = mpg123_outblock(mh);
|
|
buffer = (unsigned char*) malloc(buffer_size * sizeof(unsigned char));
|
|
|
|
/* open the file and get the decoding format */
|
|
mpg123_open(mh, path);
|
|
mpg123_getformat(mh, &rate, &channels, &encoding);
|
|
|
|
/* set the output format and open the output device */
|
|
format.bits = mpg123_encsize(encoding) * BITS;
|
|
format.rate = rate;
|
|
format.channels = channels;
|
|
format.byte_format = AO_FMT_NATIVE;
|
|
format.matrix = 0;
|
|
dev = ao_open_live(driver, &format, NULL);
|
|
|
|
/* decode and play */
|
|
while (mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK)
|
|
ao_play(dev, (char *)buffer, done);
|
|
|
|
/* clean up */
|
|
free(buffer);
|
|
ao_close(dev);
|
|
mpg123_close(mh);
|
|
mpg123_delete(mh);
|
|
mpg123_exit();
|
|
ao_shutdown();
|
|
}
|