1
1
Fork 0
Browse Source

working sound (ish)

emscriptem
alistair 3 years ago
parent
commit
e930f86014
  1. 59
      src/audio.c
  2. 3
      src/audio.h
  3. 29
      src/game.c
  4. 9
      src/main.c
  5. 1
      src/types.h

59
src/audio.c

@ -1,5 +1,7 @@ @@ -1,5 +1,7 @@
#include "audio.h"
#include <SDL2/SDL_keycode.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_mutex.h>
void generate_audio(void);
struct game_sounds game_sounds;
@ -40,7 +42,7 @@ void start_audio(void) { @@ -40,7 +42,7 @@ void start_audio(void) {
exit(1);
}
if (Mix_AllocateChannels(20)) {
if (Mix_AllocateChannels(20) < 0) {
fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError());
}
@ -48,15 +50,43 @@ void start_audio(void) { @@ -48,15 +50,43 @@ void start_audio(void) {
}
void play_game_sound(Mix_Chunk *chunk, int len, int channel) {
void
play_game_sound(Mix_Chunk *chunk, int len, int channel)
{
if (Mix_Playing(channel)) {
return;
}
Mix_FadeInChannelTimed(channel, chunk, -1, 1, len);
if(Mix_PlayChannelTimed(channel, chunk, -1, len) < 0)
fprintf(stderr, "Unable to play audio: %s\n", SDL_GetError());
//Mix_FadeInChannelTimed(channel, chunk, -1, 1, len);
}
Uint8
pitchshift_hit(unsigned int t, void *arg)
{
double *hitt = arg;
double hit = *hitt;
hit = 6 - hit;
int lower = 10 + 2 * (hit > 6 ? 6 : hit);
int higher = 1 + (hit > 6 ? 6 : hit);
// printf("mag %f higher %d lower %d\n", hit, higher, lower);
const double R=8000; // sample rate (samples per second)
const double C=125; // frequency of middle-C (hertz)
const double F=R/256; // bytebeat frequency of 1*t due to 8-bit truncation (hertz)
const double V=127; // a volume constant
double volume = (((t / 3000) > 2) ? 2 : t / 3000);
return ((sin(t*2*M_PI/R*(125 + hit))+1)*10 + (sin(t*2*M_PI/R*(250 + hit))+1)*(50 + hit) + (sin(t*2*M_PI/R*(900 + hit))+1)*100 + (sin(t*2*M_PI/R*(150+ hit))+1)*5) ;
return ((t < 3000) * ((t % higher) * (3000 - t % 3000) * 0.00351));
//+ ((t < 3500) * ((3500 - t % 3500)) * 0.0002 * (t % lower));
}
void synthplay_now(Uint8 (*bbgen)(unsigned int t), int len) {
void
synthplay_now(Uint8 (*bbgen)(unsigned int t, void *), void *arg, int len)
{
static Mix_Chunk *new_chunk = NULL;
if (Mix_Playing(1)) {
@ -66,13 +96,17 @@ void synthplay_now(Uint8 (*bbgen)(unsigned int t), int len) { @@ -66,13 +96,17 @@ void synthplay_now(Uint8 (*bbgen)(unsigned int t), int len) {
if (new_chunk)
Mix_FreeChunk(new_chunk);
int generate_len = len * (512) * 8;
int generate_len = len * (512);
Uint8 raw_data[generate_len];
fill_audio(bbgen, raw_data, generate_len);
unsigned int t = 1;
for(int i = 0; i < generate_len; i++,t++) {
raw_data[i] = bbgen(t, arg);
}
new_chunk = Mix_QuickLoad_RAW(raw_data, generate_len);
Mix_PlayChannelTimed(1, new_chunk, -1, -1);
Mix_PlayChannelTimed(1, new_chunk, -1, len);
}
@ -145,7 +179,7 @@ Uint8 func1(unsigned int t) { @@ -145,7 +179,7 @@ Uint8 func1(unsigned int t) {
Uint8 collision_test(unsigned int t) {
// try not to loop
return ((t < 3000) * ((t % 5) * (3000 - t % 3000) * 0.00351))
+ ((t < 3500) * ((3500 - t % 3500)) * 0.0002 * (t % 11));
+ ((t < 3500) * ((3500 - t % 3500)) * 0.0002 * (t % 14));
return ((t < 3000) * ((t % 5) * (3000 - t % 3000) * 0.00351))
+ ((4000 - t % 4000) * 0.0003 * (t % 128))
+ ((t < 3500) * ((3500 - t % 3500)) * 0.0002 * (t % 64)); // has a big low sound
@ -164,26 +198,27 @@ Uint8 rope_attach(unsigned int t) { @@ -164,26 +198,27 @@ Uint8 rope_attach(unsigned int t) {
void generate_audio(void) {
const int sfx_len_ms = 1000;
unsigned int sfx_len = (G_AUDIO_SFREQ/1000) * G_AUDIO_BSIZE * sfx_len_ms;
sfx_len = G_AUDIO_BSIZE;
Uint8 *raw_data = malloc(sfx_len);
fill_audio(winch_sound, raw_data, sfx_len);
game_sounds.rope_pull = Mix_QuickLoad_RAW(raw_data, sfx_len);
Mix_Volume(BC_ROPE_PULL, MIX_MAX_VOLUME * 0.4);
Mix_Volume(BC_ROPE_PULL, MIX_MAX_VOLUME * 0.3);
Uint8 *raw_data2 = malloc(sfx_len);
fill_audio(collision_test, raw_data2, sfx_len);
game_sounds.collision = Mix_QuickLoad_RAW(raw_data2, sfx_len);
//Mix_Volume(BC_COLLISION, MIX_MAX_VOLUME);
Mix_Volume(BC_COLLISION, MIX_MAX_VOLUME * 1.2);
Uint8 *raw_data3 = malloc(sfx_len);
fill_audio(big_hum, raw_data3, sfx_len);
game_sounds.background = Mix_QuickLoad_RAW(raw_data3, sfx_len);
Mix_Volume(BC_MUSIC, MIX_MAX_VOLUME * 0.2);
Mix_Volume(BC_MUSIC, MIX_MAX_VOLUME * 0.1);
Uint8 *raw_data4 = malloc(sfx_len);
fill_audio(rope_attach, raw_data4, sfx_len);
game_sounds.rope_attach = Mix_QuickLoad_RAW(raw_data4, sfx_len);
//Mix_Volume(BC_ROPE_ATTACH, MIX_MAX_VOLUME);
Mix_Volume(BC_ROPE_ATTACH, MIX_MAX_VOLUME * 0.1);
}

3
src/audio.h

@ -25,6 +25,9 @@ extern struct game_sounds game_sounds; @@ -25,6 +25,9 @@ extern struct game_sounds game_sounds;
void play_game_sound(Mix_Chunk *chunk, int len, int channel);
void start_audio(void);
Uint8 pitchshift_hit(unsigned int t, void *arg);
void synthplay_now(Uint8 (*bbgen)(unsigned int t, void *), void *arg, int len);
enum audio_channel_names {
BC_COLLISION = 1,
BC_ROPE_PULL = 2,

29
src/game.c

@ -975,7 +975,6 @@ void add_rope(int mouse_x, int mouse_y) { @@ -975,7 +975,6 @@ void add_rope(int mouse_x, int mouse_y) {
return;
}
play_game_sound(game_sounds.rope_attach, 400, -1);
Vect mouse;
mouse.x = mouse_x;
@ -1062,6 +1061,8 @@ void add_rope(int mouse_x, int mouse_y) { @@ -1062,6 +1061,8 @@ void add_rope(int mouse_x, int mouse_y) {
player.physics->strings[0].set_end_point(player.physics->strings, &end);
player.physics->strings[0].attached = true;
play_game_sound(game_sounds.rope_attach, 100, BC_ROPE_ATTACH);
}
}
@ -1079,6 +1080,14 @@ int process_collisions(Body *thing, Vect *trans) { @@ -1079,6 +1080,14 @@ int process_collisions(Body *thing, Vect *trans) {
int num_cols = 0;
int num = 0;
thing->was_colliding += thing->colliding > 0 ? 1 : -1;
if (thing->was_colliding < -1000) {
thing->was_colliding = -1000;
}
if (thing->was_colliding > 1000) {
thing->was_colliding = 1000;
}
for (int k = 0; k < world.items.size; k++) {
if (world.get(k).kind == STATIC_WALL_W
&& world.get(k).wall->physics->uid != thing->uid) {
@ -1249,6 +1258,11 @@ void advance_thing(Body * thing) { @@ -1249,6 +1258,11 @@ void advance_thing(Body * thing) {
translation.x = translation.x / mag;
translation.y = translation.y / mag;
if (mag > 0.10) {
Mix_Volume(BC_COLLISION, MIX_MAX_VOLUME * mag);
play_game_sound(game_sounds.collision, 230, BC_COLLISION);
}
// get velocity in direction of collision
mag = vect_scalar_projection(thing->vel, translation);
@ -1607,7 +1621,7 @@ void startgame(SDL_Renderer * ren) { @@ -1607,7 +1621,7 @@ void startgame(SDL_Renderer * ren) {
viewport_pos.x = player.physics->position.x - width / 2;
viewport_pos.y = player.physics->position.y - height / 2;
play_game_sound(game_sounds.background , -1, BC_MUSIC);
//get_room();
}
@ -1697,8 +1711,15 @@ int get_rand(int seed) { @@ -1697,8 +1711,15 @@ int get_rand(int seed) {
}
int step(void) {
static int first_run = 0;
if (!first_run) {
start_audio();
// play_game_sound(game_sounds.background, -1, BC_MUSIC);
first_run = 1;
}
if (player.physics->position.x > world.uniques_index[ROOM_W]->room
->floor.items[world.uniques_index[ROOM_W]->room->floor.numItems - 1]->position.x) {
return 1;
@ -1711,6 +1732,8 @@ int step(void) { @@ -1711,6 +1732,8 @@ int step(void) {
if (in_game && !game_paused) {
advance_things();
}
return 0;
}

9
src/main.c

@ -111,9 +111,8 @@ int game(void) { @@ -111,9 +111,8 @@ int game(void) {
physics_thread = SDL_CreateThread(physics_loop, "physics", (void *)ren);
start_audio();
play_game_sound(game_sounds.background , -1, BC_MUSIC);
int count = 0;
while (!close) {
SDL_Event event;
@ -142,12 +141,6 @@ int game(void) { @@ -142,12 +141,6 @@ int game(void) {
draw_end_screen(ren);
} else {
redraw(ren);
if (player.physics->colliding) {
count++;
play_game_sound(game_sounds.collision, 437, BC_COLLISION);
} else {
count = 0;
}
}
}
}

1
src/types.h

@ -86,6 +86,7 @@ typedef struct BodyStruct{ @@ -86,6 +86,7 @@ typedef struct BodyStruct{
int uid;
bool colliding;
int was_colliding;
// position in viewport (pixels)
SDL_Point screen_pos;

Loading…
Cancel
Save