medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

linux_audio.cc (4283B)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <math.h>

#include <alsa/asoundlib.h>

#include "intrinsics.h"
#include "wave.h"
#include "assets.h"
#include "audio.h"
#include "platform_thread.h"

#define FRAMES 1024

static int milliseconds( int ms ) {
	return ms * 1000;
}

static u32 sample_rate;
static u8 mix_memory[ kilobytes( 512 ) ];
static MemoryArena mix_arena;

static THREAD( mix_worker ) {
	snd_pcm_t * pcm = ( snd_pcm_t * ) data;

	s16 samples[ 2048 ];
	AudioOutputBuffer buffer;
	buffer.sample_rate = sample_rate;
	buffer.num_samples = array_count( samples );
	buffer.samples = samples;

	while( true ) {
		audio_mix( &mix_arena, &buffer );

		for( u32 i = 0; i < buffer.num_samples; ) {
			// s16 * channels[ 2 ] = { sound.samples + i, sound.samples + sound.num_samples + i };
			s16 * channels[ 2 ] = { buffer.samples + i, buffer.samples + i };

			snd_pcm_sframes_t written = snd_pcm_writen( pcm, ( void ** ) channels, buffer.num_samples - i );
			if( written <= 0 ) {
				snd_pcm_recover( pcm, written, 1 );
			}
			else {
				i += written;
			}
		}
	}

	THREAD_END;
}

static void error_handler( const char * file, int line, const char * function, int err, const char * fmt, ... ) {
	printf( "error in %s at %s:%d\n", file, function, line );

	va_list args;
	va_start( args, fmt );
	vprintf( fmt, args );
	va_end( args );
	printf( "\n" );
}

static SoundData * make_sin_wave( u32 sample_rate, u32 frequency ) {
	const u32 num_samples = sample_rate * ( 1.0f / frequency );

	SoundData * sound = ( SoundData * ) malloc( sizeof( SoundData ) );
	sound->samples = ( s16 * ) malloc( num_samples * sizeof( s16 ) );

	sound->num_samples = num_samples;
	sound->sample_rate = sample_rate;
	sound->num_channels = 1;

	for( u32 i = 0; i < num_samples; i++ ) {
		const float t = ( float ) i / num_samples;
		sound->samples[ i ] = INT16_MAX * sinf( t * 2.0f * M_PI );
	}

	return sound;
}

static u8 memory[ megabytes( 64 ) ];
int main( int argc, char ** argv ) {
	snd_lib_error_set_handler( error_handler );

	MemoryArena arena;
	memarena_init( &arena, memory, sizeof( memory ) );

	memarena_init( &mix_arena, mix_memory, sizeof( mix_memory ) );

	audio_init( &arena );

	u8 * wave = file_get_contents( "02 - Unbreakable.wav" );
	SoundData sound;
	bool ok = wave_decode( &arena, wave, &sound );
	assert( ok );

	SoundData * t = make_sin_wave( sound.sample_rate, 200 );
	sound.samples = t->samples;
	sound.num_samples = t->num_samples;
	// TODO: remove this when the mixer can do resampling
	sample_rate = sound.sample_rate;

	SoundData * sin_data = make_sin_wave( sound.sample_rate, 500 );

	audio_set_volume( audio_play_sound( &arena, sound, true ), 0.8 );
	Sound sound_sin = audio_play_sound( &arena, *sin_data, true );
	bool playing = true;

	const char * pcmname = argc == 2 ? argv[ 1 ] : "default";

	snd_pcm_t * pcm;
	const int ok_open = snd_pcm_open( &pcm, pcmname, SND_PCM_STREAM_PLAYBACK, 0 );

	if( ok_open != 0 ) {
		printf( "nope: %s\n", snd_strerror( ok_open ) );
		return 1;
	}

	const int channels = sound.num_channels;
	const int sample_rate = sound.sample_rate;
	const int latency = milliseconds( 10 );

	const int ok_set_params = snd_pcm_set_params( pcm,
		SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_NONINTERLEAVED,
		channels, sample_rate, 1, latency );

	if( ok_set_params != 0 ) {
		printf( "nope: %s\n", snd_strerror( ok_open ) );
		return 1;
	}

	// snd_output_t *log;
	// if (snd_output_stdio_attach(&log, stderr, 0) >= 0) {
	// 	snd_pcm_dump(pcm, log); snd_output_close(log); }

	Thread mix_thread;
	thread_init( &mix_thread, mix_worker, pcm );

	char * line = NULL;
	size_t n;

	float volume = 0.1f;
	audio_set_volume( sound_sin, volume );
	while( true ) {
		getline( &line, &n, stdin );

		if( strcmp( line, "+\n" ) == 0 ) {
			volume += 0.1f;
			audio_set_volume( sound_sin, volume );
		}
		else if( strcmp( line, "-\n" ) == 0 ) {
			volume -= 0.1f;
			audio_set_volume( sound_sin, volume );
		}
		else if( strcmp( line, "s\n" ) == 0 ) {
			if( playing ) {
				audio_stop_sound( sound_sin );
			}
			else {
				sound_sin = audio_play_sound( &arena, *sin_data, true );
				audio_set_volume( sound_sin, volume );
			}
			playing = !playing;
		}

		printf( "%f\n", volume );
	}

	assert( snd_pcm_close( pcm ) == 0 );

	return 0;
}