From dd4b9d2e51d626bce90f1163b38ffc7da956075a Mon Sep 17 00:00:00 2001 From: xdanep Date: Tue, 27 Dec 2022 10:30:29 -0600 Subject: [PATCH] Sound system improvements --- .vscode/tasks.json | 3 + README.md | 2 + src/Makefile | 10 ++-- src/cmd.c | 18 ++++-- src/files.c | 4 +- src/game.c | 16 ++--- src/include/cmd.h | 2 +- src/include/sound.h | 24 ++++++-- src/main.c | 4 +- src/menu.c | 4 +- src/sound.c | 138 +++++++++++++++++++++++++++++++++++++------- 11 files changed, 172 insertions(+), 53 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 55ab1f8..a2c4518 100755 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -10,11 +10,14 @@ "-pipe", "-flto", "-fPIC", + "-DLIBBSD_OVERLAY", + "-I/usr/include/bsd", "-g", "*.c", "-lncursesw", "-lSDL2_mixer", "-lSDL2", + "-lbsd", "-o", "${fileDirname}/main" ], diff --git a/README.md b/README.md index c12d764..21467c0 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ Below you can find compiling commands. ### -`sdl2_mixer` +### -`libbsd` + # To compile: ### $`make` diff --git a/src/Makefile b/src/Makefile index 746ca36..5e21c26 100644 --- a/src/Makefile +++ b/src/Makefile @@ -7,8 +7,8 @@ ## CC=gcc -CLIBS=-lncursesw -lSDL2_mixer -lSDL2 -CFLAGS=-pipe -O2 -flto -Wall -Wextra -fPIC -std=gnu17 +LLIBS=-lncursesw -lSDL2_mixer -lSDL2 -lbsd +CFLAGS=-pipe -O2 -flto -Wall -Wextra -fPIC -std=gnu17 -DLIBBSD_OVERLAY -I/usr/include/bsd OBJS= \ cmd.o \ @@ -18,14 +18,14 @@ OBJS= \ main.o \ menu.o \ screen.o \ - sound.o + sound.o default: all all: main main: $(OBJS) - $(CC) $(CFLAGS) -o vector $(OBJS) $(CLIBS) + $(CC) $(CFLAGS) -o vector $(OBJS) $(LLIBS) clean: @rm -rf *.o vector @@ -46,4 +46,4 @@ uninstall: @sudo rm /usr/share/icons/logo.png @sudo rm /usr/share/applications/vector.desktop @sudo rm ~/.config/vector/ -r - @sudo rm /opt/vector/ -r \ No newline at end of file + @sudo rm /opt/vector/ -r diff --git a/src/cmd.c b/src/cmd.c index 56cfe81..dc65855 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -1,8 +1,9 @@ // -// cmd.c -// Vector +// cmd.c -- Terminal Arguments // -// Created by Aarch-64 on 18/12/2022. +// This file is owned by the vector project +// +// Created by Aarch-64 on 18/12/2022 // Copyright (C) 2022 Agustín Gutíerrez. All rights reserved. // @@ -45,12 +46,12 @@ void extended(void) /* CLI arguments */ void cmd(int argc, char *argv[]) { - if (argc == 2) + if (argc > 1) { /* if (argument cmd 1) == argv_help */ if (!strcmp(argv[1], argv_help) || !strcmp(argv[1], argv_hel)) // || if (argument cmd 1) == argv_hel... { - printf("--version\t\t\tProgram version\n"); + puts("--version\t\t\tProgram version\n"); exit(EXIT_SUCCESS); } @@ -61,5 +62,10 @@ void cmd(int argc, char *argv[]) extended(); exit(EXIT_SUCCESS); } + else + { + puts("invalid argument\n"); + exit(EXIT_FAILURE); + } } -} \ No newline at end of file +} diff --git a/src/files.c b/src/files.c index 69c66de..e3555f8 100755 --- a/src/files.c +++ b/src/files.c @@ -95,7 +95,7 @@ unsigned short int LoadOptions(OPTION *O) if (O->mstate) // music initializator { - sound(SONG1, SOUNDFREQ, SOUNDBUFLEN); + sound(music_ini, SOUNDFREQ, SOUNDBUFLEN); } return 0; @@ -190,4 +190,4 @@ void saveScore(PLAYER P, int score) fprintf(fPtr, "%s score: %d\n", P.name, score); // reading PLAYER structure fclose(fPtr); // closing save file -} \ No newline at end of file +} diff --git a/src/game.c b/src/game.c index 865cc6a..1dde4cf 100755 --- a/src/game.c +++ b/src/game.c @@ -75,7 +75,7 @@ long int start() clear_s(); // clear screen if (O.mstate) - sound(SONG2, SOUNDFREQ, SOUNDBUFLEN); // start music + sound(music_game, SOUNDFREQ, SOUNDBUFLEN); // start music // setting colors init_pair(1, P.cstate, COLOR_BLACK); // user's choice text on black background @@ -231,7 +231,7 @@ long int start() if (O.mstate) { cleansound(); - wsound(SONG3, SOUNDFREQ, SOUNDBUFLEN); // starting main menu music + wsound(music_game_over, SOUNDFREQ, SOUNDBUFLEN); // starting main menu music sleep(1); cleansound(); // closing vlc } @@ -250,7 +250,7 @@ long int start() if (O.mstate) { cleansound(); - wsound(SONG3, SOUNDFREQ, SOUNDBUFLEN); // starting main menu music + wsound(music_game_over, SOUNDFREQ, SOUNDBUFLEN); // starting main menu music sleep(1); cleansound(); // closing vlc } @@ -269,9 +269,9 @@ long int start() if (O.mstate) { cleansound(); - wsound(SONG3, SOUNDFREQ, SOUNDBUFLEN); // starting main menu music + wsound(music_game_over, SOUNDFREQ, SOUNDBUFLEN); // starting main menu music sleep(1); - cleansound(); // closing vlc + cleansound(); // closing sdl } sleep(2); @@ -303,8 +303,8 @@ long int start() if (O.mstate) { - cleansound(); // closing vlc - sound(SONG1, SOUNDFREQ, SOUNDBUFLEN); // starting main menu music + cleansound(); // closing sdl + sound(music_ini, SOUNDFREQ, SOUNDBUFLEN); // starting main menu music } // restoring default screen options @@ -318,4 +318,4 @@ long int start() // score = 0; return score; -} \ No newline at end of file +} diff --git a/src/include/cmd.h b/src/include/cmd.h index 83cbe40..570dbc6 100644 --- a/src/include/cmd.h +++ b/src/include/cmd.h @@ -1,6 +1,6 @@ // DEFINE FOR CLI ARGUMENTS #define PROJNAME "Vector" -#define DISTVERSION "1.1.1" +#define DISTVERSION "1.1.2" #define BUILDVERSION 1 #define RELSTATUS "release" #define BUILDPTFORM "x86_64-pc-linux-gnu" diff --git a/src/include/sound.h b/src/include/sound.h index a742895..bb4334e 100644 --- a/src/include/sound.h +++ b/src/include/sound.h @@ -7,15 +7,29 @@ #include #include #include +#include +#include +#include -#define SONG1 "/opt/vector/music/menusound.xm" -#define SONG2 "/opt/vector/music/gamesound.mp3" -#define SONG3 "/opt/vector/music/gameover.wav" +// #define SONG1 "SelfControl.mp3" +// #define SONG2 "Vitality_-_Electro_Shock_Sport_Dance.ogg" + +#define SONG1 "menusound.xm" +#define SONG2 "gamesound.mp3" +#define SONG3 "gameover.wav" #define MAXCHAN 32 #define SOUNDFREQ 44100 -#define SOUNDBUFLEN 1024 - // music.mp3, 44100, 1024 +#define SOUNDBUFLEN 2048 + +extern char out_sound_dir[70]; +extern char music_ini[70]; +extern char music_game[70]; +extern char music_game_over[70]; + +extern bool nosound; + + // music.mp3, 44100, 1024 void sound(void *sound, int sound_freq, int soundbufferlen); // sound(SONG1, SOUNDFREQ, SOUNDBUFFLEN); void wsound(void *sound, int sound_freq, int soundbufferlen); void stop_sound(void *sound); // stop_sound(SONG1); diff --git a/src/main.c b/src/main.c index 3e74e5c..e062c38 100755 --- a/src/main.c +++ b/src/main.c @@ -35,7 +35,7 @@ int main(int argc, char *argv[]) if (O.mstate) { - sound(SONG1, SOUNDFREQ, SOUNDBUFLEN); + sound(music_ini, SOUNDFREQ, SOUNDBUFLEN); } } @@ -122,4 +122,4 @@ int main(int argc, char *argv[]) exit_s(); exit(EXIT_SUCCESS); -} \ No newline at end of file +} diff --git a/src/menu.c b/src/menu.c index ff9d684..b272ed7 100755 --- a/src/menu.c +++ b/src/menu.c @@ -89,7 +89,7 @@ void setmusic(OPTION *O) sprint("Music On\n"); - sound(SONG1, SOUNDFREQ, SOUNDBUFLEN); + sound(music_ini, SOUNDFREQ, SOUNDBUFLEN); } else @@ -208,4 +208,4 @@ void controls() setbuf(stdin, NULL); getchar(); -} \ No newline at end of file +} diff --git a/src/sound.c b/src/sound.c index 13ae61a..ee3816b 100644 --- a/src/sound.c +++ b/src/sound.c @@ -1,55 +1,124 @@ -/* sound.c -- API for song of vector */ - // -// sound.c -// Vector +// sound.c -- music API +// +// This file is owned by the vector project // // Created by Aarch-64 on 21/12/2022 // Copyright (C) 2022 Agustín Gutíerrez. All rights reserved. // -#include -#include +#ifdef WIN32 + #include +#else + #include + #include +#endif #include "include/sound.h" #include "include/screen.h" -// Use for wav files -// Mix_Chunk; -// Use for music files -// Mix_Music; -Mix_Music *mod = NULL; -Mix_Chunk *wmod = NULL; +char out_sound_dir[70]; +char music_ini[70]; +char music_game[70]; +char music_game_over[70]; + +#ifdef WIN32 + FMUSIC_MODULE *mod = NULL; + FSOUND_STREAM *stream = NULL; +#else + // Use for wav files + // Mix_Chunk; + // Use for music files + // Mix_Music; + Mix_Music *mod = NULL; + Mix_Chunk *wmod = NULL; + int stream; +#endif + +bool nosound = false; // Initialize SDL void init_sdl(void) { + openlog("Logs", LOG_PID, LOG_USER); + syslog(LOG_INFO, "Start logging"); + closelog(); + if (SDL_Init(SDL_INIT_AUDIO) < 0) + { winerr("Failed to init SDL"); + nosound = true; + } + else + { + #ifdef WIN32 + strcpy_s(out_sound_dir, sizeof(out_sound_dir, "/opt/vector/music/"); + #else + strlcpy(out_sound_dir, "/opt/vector/music/", sizeof(out_sound_dir)); + #endif + + #ifdef WIN32 + strcpy_s(music_ini, sizeof(out_sound_dir, out_sound_dir); + strcat_s(music_ini, sizeof(out_sound_dir, SONG1); + #else + strlcpy(music_ini, out_sound_dir, sizeof(music_ini)); + strlcat(music_ini, SONG1, sizeof(music_ini)); + #endif + + #ifdef WIN32 + strcpy_s(music_game, sizeof(out_sound_dir, out_sound_dir); + strcat_s(music_game, sizeof(out_sound_dir, SONG2); + #else + strlcpy(music_game, out_sound_dir, sizeof(music_game)); + strlcat(music_game, SONG2, sizeof(music_game)); + #endif + + #ifdef WIN32 + strcpy_s(music_game_over, sizeof(out_sound_dir, out_sound_dir); + strcat_s(music_game_over, sizeof(out_sound_dir, SONG3); + #else + strlcpy(music_game_over, out_sound_dir, sizeof(music_game_over)); + strlcat(music_game_over, SONG3, sizeof(music_game_over)); + #endif + } } // Example: SelfControl.mp3 | 44100 | 1024 // standar for sound frequency: 44100 // standar for sound buffer len: 1024 void sound(void *sound, int sound_freq, int soundbufferlen) -{ +{ if (sound == NULL) + { winerr("Error in Sound Library (path music)."); + nosound = true; + } else if (sound_freq < 1) + { winerr("Error in Sound Library (sound freq)."); + nosound = true; + } else if (soundbufferlen < 1) + { winerr("Error in Sound Library (sound buffer len)."); + nosound = true; + } init_sdl(); - //Initialize SDL_mixer if (Mix_OpenAudio(sound_freq, MIX_DEFAULT_FORMAT, 2, soundbufferlen) < 0) + { ar_winerr("sound init failed (SDL_mixer)", Mix_GetError()); + nosound = true; + } Mix_AllocateChannels(MAXCHAN); mod = Mix_LoadMUS(sound); if (mod == NULL) + { ar_winerr("Error in Sound Library (init music)", Mix_GetError()); + nosound = true; + } else Mix_PlayMusic(mod, -1); // mix, while reproduction */ -1 = always reproduction | 1 = one reproduction */ } @@ -60,22 +129,37 @@ void sound(void *sound, int sound_freq, int soundbufferlen) void wsound(void *sound, int sound_freq, int soundbufferlen) { if (sound == NULL) + { winerr("Error in Sound Library (path music)"); + nosound = true; + } else if (sound_freq < 1) + { winerr("Error in Sound Library (sound freq)"); + nosound = true; + } else if (soundbufferlen < 1) + { winerr("Error in Sound Library (sound buffer len)"); + nosound = true; + } init_sdl(); //Initialize SDL_mixer if (Mix_OpenAudio(sound_freq, MIX_DEFAULT_FORMAT, 2, soundbufferlen) < 0) + { ar_winerr("sound init failed (SDL_mixer)", Mix_GetError()); - + nosound = true; + } + Mix_AllocateChannels(MAXCHAN); wmod = Mix_LoadWAV(sound); if (wmod == NULL) - ar_winerr("Error in Sound Library (init music)", Mix_GetError()); + { + ar_winerr("Error in Sound Library (init music)", Mix_GetError()); + nosound = true; + } else Mix_PlayChannel(-1, wmod, -1); // channel, mix, while reproduction */ -1 = always reproduction | 1 = one reproduction */ } @@ -83,21 +167,31 @@ void wsound(void *sound, int sound_freq, int soundbufferlen) // Example: SelfControl.mp3 void stop_sound(void *sound) { - Mix_HaltMusic(); - Mix_FreeMusic(sound); + #ifdef WIN32 + FMUSIC_MODULE *mod = NULL; + FSOUND_STREAM *stream = NULL; + #else + Mix_HaltMusic(); // pause music + Mix_FreeMusic(sound); // free memory for music + #endif } // Example: SelfControl.wav void stop_wsound(void *sound) { - Mix_FreeChunk(sound); + Mix_FreeChunk(sound); // free memory for music } void cleansound(void) { - Mix_CloseAudio(); - mod = NULL; - wmod = NULL; + #ifdef WIN32 + FSOUND_Stream_Close(stream); + #else + Mix_CloseAudio(); + SDL_Quit(); + mod = NULL; + wmod = NULL; + #endif } // Extras