alistair
3 years ago
6 changed files with 194 additions and 173 deletions
@ -0,0 +1,151 @@
@@ -0,0 +1,151 @@
|
||||
#include "audio.h" |
||||
|
||||
void generate_audio(void); |
||||
struct game_sounds game_sounds; |
||||
|
||||
Uint8 big_hum(unsigned int t);
|
||||
Uint8 func0(int t);
|
||||
|
||||
|
||||
void fill_audio(void *func, Uint8 *stream, int len) { |
||||
Uint8 (*wavegen)(int) = func; |
||||
|
||||
static int t = 1; |
||||
for(int i = 0; i < len; i++,t++) { |
||||
stream[i] = wavegen(t); |
||||
} |
||||
} |
||||
|
||||
void callbackfn3(void *unused, Uint8 *stream, int len) { |
||||
static int t = 1; |
||||
for(int i = 0; i < len; i++,t++) { |
||||
stream[i] = big_hum(t); |
||||
stream[i] = 0;
|
||||
} |
||||
} |
||||
|
||||
void start_audio(void) { |
||||
|
||||
//audio_format.callback = callbackfn3;
|
||||
/* just for playing background music
|
||||
if (SDL_OpenAudio(&audio_format, NULL)) { |
||||
fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError()); |
||||
exit(1); |
||||
} |
||||
*/ |
||||
//SDL_PauseAudio(0);
|
||||
if (Mix_OpenAudio(G_AUDIO_SFREQ,AUDIO_U8,1,G_AUDIO_BSIZE) < 0) { |
||||
fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError()); |
||||
exit(1); |
||||
} |
||||
generate_audio(); |
||||
Mix_Resume(-1); |
||||
} |
||||
|
||||
void play_game_sound(Mix_Chunk *chunk, int len, int channel) { |
||||
if (Mix_Playing(channel)) { |
||||
return; |
||||
} |
||||
Mix_FadeInChannelTimed(channel, chunk, -1, 1, len); |
||||
} |
||||
|
||||
void synthplay_now(Uint8 (*bbgen)(unsigned int t), int len) { |
||||
static Mix_Chunk *new_chunk = NULL; |
||||
|
||||
if (Mix_Playing(1)) { |
||||
return; |
||||
} |
||||
|
||||
if (new_chunk) |
||||
Mix_FreeChunk(new_chunk); |
||||
|
||||
int generate_len = len * (512) * 8; |
||||
|
||||
Uint8 raw_data[generate_len]; |
||||
fill_audio(bbgen, raw_data, generate_len); |
||||
|
||||
new_chunk = Mix_QuickLoad_RAW(raw_data, generate_len); |
||||
Mix_PlayChannelTimed(1, new_chunk, -1, -1); |
||||
} |
||||
|
||||
|
||||
|
||||
void callbackfn2(void *unused, Uint8 *stream, int len) { |
||||
static int t = 1; |
||||
int i; |
||||
int yeet = rand(); |
||||
for(i=0; i < len; i++,t++) { |
||||
stream[i] = func0(t); |
||||
} |
||||
} |
||||
|
||||
|
||||
void callbackfn(void *unused, Uint8 *stream, int len) |
||||
{ |
||||
static int t = 0; |
||||
int i; |
||||
int yeet = rand(); |
||||
for( i=0; i < len; i++,t++) { |
||||
/*
|
||||
stream[i] = (t*5&(t>>7))|(t*3&(t>>8)); |
||||
*/ |
||||
//stream[i] = (( t >> 10 ) & 42) * t;
|
||||
stream[i] = (t & yeet) % 73; |
||||
} |
||||
} |
||||
|
||||
/* Waveform Generators */ |
||||
|
||||
Uint8 big_hum(unsigned int t) { |
||||
return ( (sin(t*2*3.14159265/16000*200)+1) * 20) + ( (sin(t*2*3.14159265/16000*110)+1) * 20) + ( (sin(t*2*3.14159265/16000*230)+1) * 20) + ( (sin((t % 80)*2*3.14159265/16000 * 6)+1) * 12); |
||||
} |
||||
|
||||
unsigned int sine_wave_sound(int t) { |
||||
const double R=8000; // sample rate (samples per second)
|
||||
const double C=261.625565; // 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
|
||||
return (sin(t*2*M_PI/R*C)+1)*V; |
||||
} |
||||
|
||||
unsigned int winch_sound(int t) { |
||||
return (t % 80) | (t % 36) | (6 * (t % 6)); |
||||
} |
||||
|
||||
unsigned int beat_2(int t) { |
||||
return ((((t % 250) / 2)) | (t % 129)) % 110; |
||||
} |
||||
|
||||
Uint8 func0(int t) { |
||||
int note = 18; |
||||
if (t % 1240) { |
||||
note = (1 + rand()) % 18 + 1; |
||||
} |
||||
|
||||
fflush(stdout); |
||||
return (t % note); |
||||
} |
||||
|
||||
unsigned int func1(int t) { |
||||
int note = 10; |
||||
if (!(t % 8000)) { |
||||
note = (1 + rand()) % 10 + 5; |
||||
printf("%d %d\n", t, note); |
||||
} |
||||
return ((t & 110) % 73 | (t % 1711)); |
||||
} |
||||
|
||||
|
||||
void generate_audio(void) { |
||||
|
||||
const int sfx_len_ms = 2000; |
||||
unsigned int sfx_len = (G_AUDIO_SFREQ/1000) * G_AUDIO_BSIZE * sfx_len_ms; |
||||
Uint8 raw_data[sfx_len]; // 10 seconds
|
||||
|
||||
fill_audio(winch_sound, raw_data, sfx_len); |
||||
game_sounds.rope_pull = Mix_QuickLoad_RAW(raw_data, sfx_len); |
||||
fill_audio(beat_2, raw_data, sfx_len); |
||||
game_sounds.collision = Mix_QuickLoad_RAW(raw_data, sfx_len); |
||||
} |
||||
|
||||
|
@ -0,0 +1,26 @@
@@ -0,0 +1,26 @@
|
||||
|
||||
#ifndef AUDIO_H |
||||
#define AUDIO_H |
||||
|
||||
#include <SDL2/SDL_audio.h> |
||||
#include "types.h" |
||||
#include <math.h> |
||||
#include <stdlib.h> |
||||
|
||||
#define G_AUDIO_SFREQ 8000 |
||||
#define G_AUDIO_BSIZE 512 |
||||
|
||||
struct game_sounds { |
||||
unsigned int t; |
||||
Mix_Chunk *collision; |
||||
Mix_Chunk *rope_attach; |
||||
Mix_Chunk *rope_pull; |
||||
/* Looping samples (need backbuffer to fill while playing ) */ |
||||
Mix_Chunk *menu; |
||||
}; |
||||
|
||||
extern struct game_sounds game_sounds; |
||||
|
||||
void play_game_sound(Mix_Chunk *chunk, int len, int channel); |
||||
void start_audio(void);
|
||||
#endif |
Loading…
Reference in new issue