added sound

This commit is contained in:
Alexandre 2025-02-25 21:22:58 +01:00
parent a3f6bb5f59
commit 8f0f033e81
15 changed files with 149 additions and 4 deletions

View File

@ -22,6 +22,7 @@
"stdlib.h": "c", "stdlib.h": "c",
"stb_image.h": "c", "stb_image.h": "c",
"string.h": "c", "string.h": "c",
"unistd.h": "c" "unistd.h": "c",
"pthread.h": "c"
} }
} }

View File

@ -1,6 +1,6 @@
CC = gcc CC = gcc
FLAGS = -Wall -Wextra -g FLAGS = -Wall -Wextra -g
LFLAGS = -lm src/glad.c -ldl -lglfw -lcglm -lpng -lSOIL LFLAGS = -lm src/glad.c -ldl -lglfw -lcglm -lpng -lSOIL -lpthread
all: bin/back all: bin/back
@ -10,7 +10,7 @@ test: bin/back
mem: bin/back mem: bin/back
valgrind --leak-check=full ./bin/back valgrind --leak-check=full ./bin/back
bin/back: obj/main.o obj/generation.o obj/display.o obj/proj.o obj/entities.o obj/bullets.o obj/menus.o obj/move.o obj/base.o obj/hash.o bin/back: obj/main.o obj/generation.o obj/display.o obj/proj.o obj/entities.o obj/bullets.o obj/menus.o obj/music.o obj/move.o obj/base.o obj/hash.o
mkdir -p bin mkdir -p bin
$(CC) $(FLAGS) $^ $(LFLAGS) -o $@ $(CC) $(FLAGS) $^ $(LFLAGS) -o $@
@ -26,6 +26,7 @@ obj/bullets.o: src/bullets.c
obj/move.o: src/move.c obj/move.o: src/move.c
obj/base.o: src/base.c obj/base.o: src/base.c
obj/proj.o: src/proj.c obj/proj.o: src/proj.c
obj/music.o: src/music.c
obj/menus.o: src/menus.c obj/menus.o: src/menus.c
obj/hash.o: src/hash.c obj/hash.o: src/hash.c

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
obj/music.o Normal file

Binary file not shown.

BIN
sound/audio/smw_coin.wav Normal file

Binary file not shown.

BIN
sound/audio/tetris_2.wav Executable file

Binary file not shown.

1
sound/compilation.sh Normal file
View File

@ -0,0 +1 @@
gcc sound/playsound.c -lSDL2 -o sound/music

BIN
sound/music Executable file

Binary file not shown.

23
sound/playsound.c Normal file
View File

@ -0,0 +1,23 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <SDL2/SDL.h>
int main(int argc, char** argv) {
assert(argc == 2);
if(SDL_Init(SDL_INIT_AUDIO)) {fprintf(stderr, "cannot initialize audio");exit(1);}
SDL_AudioSpec wavSpec;
Uint32 wavLength;
Uint8 *wavBuffer;
SDL_LoadWAV(argv[1], &wavSpec, &wavBuffer, &wavLength);
SDL_AudioDeviceID deviceId = SDL_OpenAudioDevice(NULL, 0, &wavSpec, NULL, 0);
int success = SDL_QueueAudio(deviceId, wavBuffer, wavLength);
SDL_PauseAudioDevice(deviceId, 0);
usleep((int)((wavLength)/44100.0*100000.0));
SDL_CloseAudioDevice(deviceId);
SDL_FreeWAV(wavBuffer);
SDL_Quit();
return 0;
}
// yummy

View File

@ -23,6 +23,7 @@
#include "entities.h" #include "entities.h"
#include "display.h" #include "display.h"
#include "generation.h" #include "generation.h"
#include "music.h"
double sim_time; double sim_time;
int triCount; int triCount;
@ -699,17 +700,20 @@ int main_alt() {
// glfw: terminate, clearing all previously allocated GLFW resources. // glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------ // ------------------------------------------------------------------
glfwTerminate(); glfwTerminate();
printf("\n<0>\n\n");
end_music();
return 0; return 0;
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
srand(time(NULL)); srand(time(NULL));
init_music();
triCount = 0; triCount = 0;
sim_time = 0.0; sim_time = 0.0;
gamemode = 0; gamemode = 0;
newRoomCount = 7; newRoomCount = 7;
switchRoom = false; switchRoom = false;
newRoomName = "templates/"; newRoomName = "templates/";
//read_png_file("res/question_block.png"); //play_sound("sound/audio/tetris_2.wav");
return main_alt(); return main_alt();
} }

View File

@ -13,6 +13,7 @@
#include "entities.h" #include "entities.h"
#include "proj.h" #include "proj.h"
#include "move.h" #include "move.h"
#include "music.h"
// ---------------------------------------------------------------------------------------------------- // // ---------------------------------------------------------------------------------------------------- //
double sensitivity = 0.06; double sensitivity = 0.06;
@ -286,6 +287,9 @@ bool is_colliding(float dtime) {
if(vstd->ents[k]->onDeath != NULL) { if(vstd->ents[k]->onDeath != NULL) {
(*vstd->ents[k]->onDeath)(dtime); (*vstd->ents[k]->onDeath)(dtime);
} }
if(vstd->ents[k]->entity_type == 0) {
play_sound("sound/audio/smw_coin.wav");
}
remove_entity(vstd->ents, &vstd->ent_memlen, &vstd->ent_len, k); remove_entity(vstd->ents, &vstd->ent_memlen, &vstd->ent_len, k);
is_clipping = false; is_clipping = false;
exists = false; exists = false;

102
src/music.c Normal file
View File

@ -0,0 +1,102 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <unistd.h>
#include <termios.h>
#include <limits.h>
#include <time.h>
#include <pthread.h>
#include <stdatomic.h>
#include "hash.h"
#include "base.h"
#include "music.h"
static pthread_t threads[32];
static bool threadIsFree[32];
static int threadID;
static int thrID;
pthread_mutex_t lock;
void init_music() {
pthread_mutex_init(&lock, NULL);
threadID = 0;
for(int k = 0; k < 32; k++) {
threadIsFree[k] = true;
}
}
void end_music() {
pthread_mutex_destroy(&lock);
for(int k = 0; k < 32; k++) {
if(!threadIsFree[k]) {
printf("[pthread] killing %d\n", k);
pthread_cancel(threads[k]);
}
}
}
char* get_name(char* filename) {
char* res = malloc(sizeof(char)*100);
res[0] = '.';
res[1] = '/';
res[2] = 's';
res[3] = 'o';
res[4] = 'u';
res[5] = 'n';
res[6] = 'd';
res[7] = '/';
res[8] = 'm';
res[9] = 'u';
res[10] = 's';
res[11] = 'i';
res[12] = 'c';
res[13] = ' ';
int k = 14;
while(filename[k-14] != '\0' && k < 99) {
res[k] = filename[k-14];
k += 1;
}
res[k] = '\0';
return res;
}
void* mainMusic(void* arg) {
int localThreadID = thrID;
char* name = get_name((char*)arg);
system(name);
free(name);
threadIsFree[localThreadID] = true;
printf("thread %d is done\n", localThreadID);
return NULL;
}
bool get_id(int* res) {
int lp = 0;
while(lp < 32) {
if(threadIsFree[(lp+threadID)%32]) {
*res = (lp+threadID)%32;
return true;
}
lp += 1;
}
return false;
}
void play_sound(char* filename) {
pthread_mutex_lock(&lock);
int id;
if(get_id(&id)) {
threadIsFree[id] = false;
thrID = id;
threadID += 1;
pthread_create(&threads[id], NULL, mainMusic, filename);
} else {
fprintf(stderr, "WARNING : cannot play audio %s (out of space)\n", filename);
}
pthread_mutex_unlock(&lock);
}

9
src/music.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef MUSIC_H
#define MUSIC_H
void init_music();
void end_music();
void play_sound(char* filename);
#endif