Skip to content

Commit

Permalink
new example with play 3d sound
Browse files Browse the repository at this point in the history
  • Loading branch information
vuvk committed Apr 27, 2020
1 parent 2fd993a commit 5d83054
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 7 deletions.
15 changes: 8 additions & 7 deletions build_without_meson.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/bin/sh
mkdir build
cd build
valac ../src/alureplay.vala --vapidir=../vapi --pkg=openal --pkg=alure --pkg=glib-2.0 --pkg=gobject-2.0
valac ../src/alurestream.vala --vapidir=../vapi --pkg=openal --pkg=alure --pkg=glib-2.0 --pkg=gobject-2.0
valac ../src/alurephysfs.vala --vapidir=../vapi --pkg=openal --pkg=alure --pkg=glib-2.0 --pkg=gobject-2.0 --pkg=physfs
valac ../src/alurephysfsstream.vala --vapidir=../vapi --pkg=openal --pkg=alure --pkg=glib-2.0 --pkg=gobject-2.0 --pkg=physfs
#!/bin/sh
mkdir build
cd build
valac ../src/alureplay.vala --vapidir=../vapi --pkg=openal --pkg=alure --pkg=glib-2.0 --pkg=gobject-2.0
valac ../src/alurestream.vala --vapidir=../vapi --pkg=openal --pkg=alure --pkg=glib-2.0 --pkg=gobject-2.0
valac ../src/alurephysfs.vala --vapidir=../vapi --pkg=openal --pkg=alure --pkg=glib-2.0 --pkg=gobject-2.0 --pkg=physfs
valac ../src/alurephysfsstream.vala --vapidir=../vapi --pkg=openal --pkg=alure --pkg=glib-2.0 --pkg=gobject-2.0 --pkg=physfs
valac ../src/alurestereo.vala --vapidir=../vapi --pkg=openal --pkg=alure --pkg=glib-2.0 --pkg=gobject-2.0 --pkg=physfs -X -lm
4 changes: 4 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ dependencies = [
dependency('alure'),
]

cc = meson.get_compiler('c')
m_dep = cc.find_library('m', required : false)

vapi_dir = meson.current_source_dir() / 'vapi'
add_project_arguments(['--vapidir', vapi_dir], language: 'vala')

executable('alureplay', 'src/alureplay.vala', dependencies: dependencies)
executable('alurestream', 'src/alurestream.vala', dependencies: dependencies)
executable('alurephysfs', 'src/alurephysfs.vala', dependencies: [dependencies, dependency('physfs')])
executable('alurephysfsstream', 'src/alurephysfsstream.vala', dependencies: [dependencies, dependency('physfs')])
executable('alurestereo', 'src/alurestereo.vala', dependencies: [dependencies, dependency('physfs'), m_dep])
Binary file added samples/vase3.wav
Binary file not shown.
107 changes: 107 additions & 0 deletions src/alurestereo.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@

public class AlureStereo {
private static bool quit = false;
private static Alure.Stream stream;
private static AL.Source src;

private const int CHUNK_LENGTH = 128000;
private const int NUM_BUFS = 3;
private const int64 DELAY = 20; // in sec
private const string FILE_NAME = "../samples/vase3.wav";

private const float DEG_TO_RAD_COEFF = (float)(Math.PI / 180.0);
private const float RADIUS = 1.5f;

private static float deg_to_rad(float deg) {
return deg * DEG_TO_RAD_COEFF;
}

// callback for repeat sound
static void eos_callback(void* userdata, AL.ALuint source) {
Alure.stop_source(source, false);

unowned Alure.Stream stream = (Alure.Stream)userdata;
stream.rewind();
if (!Alure.play_source_stream(src, stream, NUM_BUFS, 0, eos_callback, userdata)) {
stderr.printf(@"Failed to play stream: $(Alure.get_error_string())\n");
quit = true;
}
}

private static void free_resources() {
if (AL.is_source(src)) {
Alure.stop_source(src, false);

if (stream != null) {
stream.destroy();
stream = null;
}

AL.delete_source(1, out src);
}

Alure.shutdown_device();
}

public static int main(string[] args) {
if (!Alure.init_device()) {
stderr.printf(@"Failed to open OpenAL device: $(Alure.get_error_string())\n");
return 1;
}

AL.ALfloat[] position = { 0, 0, -1 };
AL.ALfloat angle = 0.0f;

AL.gen_source(1, out src);
if (AL.get_error() != AL.Error.NO_ERROR) {
stderr.printf("Failed to create OpenAL source!\n");
Alure.shutdown_device();
return 1;
}

src.set_paramfv(AL.POSITION, position);
src.set_parami(AL.SOURCE_RELATIVE, AL.TRUE);

Alure.stream_size_is_microsec(true);

stream = new Alure.Stream.from_file(FILE_NAME, CHUNK_LENGTH);
if (stream == null) {
stderr.printf(@"Could not load $FILE_NAME: $(Alure.get_error_string())\n");
free_resources();
return 1;
}

if (!Alure.play_source_stream(src, stream, NUM_BUFS, 0, eos_callback, stream)) {
stderr.printf(@"Failed to play stream: $(Alure.get_error_string())\n");
quit = true;
}

var start_dt = new DateTime.now_local();


while (!quit) {
Alure.sleep(0.125f);
Alure.update();

// update source position
angle += 5;
if (angle > 360.0f) {
angle -= 360.0f;
}
position[0] = RADIUS * Math.cosf(deg_to_rad(angle));
position[2] = -RADIUS * Math.sinf(deg_to_rad(angle));
src.set_paramfv(AL.POSITION, position);

var cur_dt = new DateTime.now_local();
int64 diff = (int64)cur_dt.difference(start_dt);
diff /= 1000000; // to sec
if (diff >= DELAY) {
quit = true;
print("Bye-bye!\n");
}
}

free_resources();
return 0;
}
}

0 comments on commit 5d83054

Please sign in to comment.