medfall

A super great game engine
Log | Files | Refs

audio.cc (2462B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <math.h>
      4 
      5 #include "intrinsics.h"
      6 #include "mixer.h"
      7 #include "wave.h"
      8 #include "assets.h"
      9 #include "platform_audio_output.h"
     10 #include "autogdb.h"
     11 
     12 static SoundData make_sin_wave( u32 sample_rate, u32 frequency ) {
     13 	const u32 num_samples = sample_rate * ( 1.0f / frequency );
     14 
     15 	SoundData sound;
     16 	sound.samples = ( s16 * ) malloc( num_samples * sizeof( s16 ) );
     17 
     18 	sound.num_samples = num_samples;
     19 	sound.sample_rate = sample_rate;
     20 	sound.num_channels = 1;
     21 
     22 	for( u32 i = 0; i < num_samples; i++ ) {
     23 		const float t = ( float ) i / num_samples;
     24 		sound.samples[ i ] = S16_MAX * sinf( t * 2.0f * PI );
     25 	}
     26 
     27 	return sound;
     28 }
     29 
     30 static u8 memory[ megabytes( 64 ) ];
     31 int main( int argc, char ** argv ) {
     32 	install_debug_signal_handlers();
     33 
     34 	MemoryArena arena;
     35 	memarena_init( &arena, memory, sizeof( memory ) );
     36 
     37 	audio_output_init();
     38 	mixer_init();
     39 
     40 	AudioOutputDevice output_device;
     41 	audio_output_open( &output_device );
     42 
     43 	size_t wave_len;
     44 	u8 * wave = file_get_contents( "02 - Unbreakable.wav", &wave_len );
     45 	SoundData sound;
     46 	bool ok = wave_decode( &arena, wave, wave_len, &sound );
     47 	ASSERT( ok );
     48 
     49 	// mixer_play( make_sin_wave( sound.sample_rate, 200 ), MIXER_LOOP );
     50 
     51 	PlayingSoundID song = mixer_play( sound );
     52 	mixer_volume( song, 1 );
     53 
     54 	// snd_output_t *log;
     55 	// if (snd_output_stdio_attach(&log, stderr, 0) >= 0) {
     56 	// 	snd_pcm_dump(pcm, log); snd_output_close(log); }
     57 
     58 	char * line = NULL;
     59 	size_t n;
     60 
     61 	SoundData sin_data = make_sin_wave( sound.sample_rate, 500 );
     62 	PlayingSoundID sound_sin = INVALID_SOUND_ID;
     63 	float volume = 1.0f;
     64 	bool playing = false;
     65 	// mixer_set_volume( sound_sin, volume );
     66 
     67 	for( ;; ) {
     68 		printf( "%f %s %u\n", volume, playing ? "playing" : "stopped", sound_sin );
     69 
     70 #if PLATFORM_WINDOWS
     71 		Sleep( 185 );
     72 #else
     73 		getline( &line, &n, stdin );
     74 
     75 		if( strcmp( line, "+\n" ) == 0 ) {
     76 			volume = saturate( volume + 0.1f );
     77 			mixer_volume( sound_sin, volume );
     78 		}
     79 		else if( strcmp( line, "-\n" ) == 0 ) {
     80 			volume = saturate( volume - 0.1f );
     81 			mixer_volume( sound_sin, volume );
     82 		}
     83 		else if( strcmp( line, "s\n" ) == 0 ) {
     84 			if( playing ) {
     85 				mixer_stop( sound_sin );
     86 			}
     87 			else {
     88 				sound_sin = mixer_play( sin_data, MIXER_LOOP );
     89 				mixer_volume( sound_sin, volume );
     90 			}
     91 			playing = !playing;
     92 		}
     93 		else if( strcmp( line, "q\n" ) == 0 ) {
     94 			break;
     95 		}
     96 #endif
     97 
     98 		mixer_mix( &output_device.buffer, &arena, 8192, 0 );
     99 	}
    100 
    101 	audio_output_close( &output_device );
    102 
    103 	return 0;
    104 }