package org.gillius.jalleg.example;

import java.nio.ShortBuffer;

import static org.gillius.jalleg.binding.AllegroLibrary.ALLEGRO_AUDIO_DEPTH.ALLEGRO_AUDIO_DEPTH_INT16;
import static org.gillius.jalleg.binding.AllegroLibrary.*;
import static org.gillius.jalleg.binding.AllegroLibrary.ALLEGRO_CHANNEL_CONF.ALLEGRO_CHANNEL_CONF_1;
import static org.gillius.jalleg.binding.AllegroLibrary.ALLEGRO_PLAYMODE.ALLEGRO_PLAYMODE_LOOP;

/**
 * Plays a square wave at 200hz
 */
public class AudioExample {
	public static void main(String[] args) throws Exception {
		al_install_system(ALLEGRO_VERSION_INT, null);
		al_install_audio();

		int sampleSize = al_get_channel_count(ALLEGRO_CHANNEL_CONF_1).intValue() *
		                 al_get_audio_depth_size(ALLEGRO_AUDIO_DEPTH_INT16).intValue();
		int bytes = sampleSize * 220;
		ShortBuffer buf = al_malloc(bytes).getByteBuffer(0, bytes).asShortBuffer();

		for (int i=0; i < 220; ++i) {
			if (i <= 110)
				buf.put(i, Short.MIN_VALUE);
			else
				buf.put(i, Short.MAX_VALUE);
		}

		ALLEGRO_SAMPLE s = al_create_sample(buf, 220, 44100, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_1, true);

		al_reserve_samples(1);
		System.out.println(al_play_sample(s, 0.05f, ALLEGRO_AUDIO_PAN_NONE, 1f, ALLEGRO_PLAYMODE_LOOP, null));
		Thread.sleep(2000);

		al_destroy_sample(s);
		al_uninstall_audio();
		al_uninstall_system();
	}
}

@@ -391,76 +391,424 @@
 GRO_
-PLAYMODE.ALLEGRO_PLAYMODE_LOOP;%0A%0A/**%0A * Plays a square wave at 200hz
+MIXER_QUALITY.ALLEGRO_MIXER_QUALITY_POINT;%0Aimport static org.gillius.jalleg.binding.AllegroLibrary.ALLEGRO_PLAYMODE.ALLEGRO_PLAYMODE_LOOP;%0A%0A/**%0A * Plays a square wave at 200hz. Since a square wave is so simple, we can generate a square wave of any frequency%0A * perfectly with only a 2 sample buffer, assuming the mixer is set to point interpolation (if it was default of linear,%0A * we'd actually get triangle waves).
 %0A */
@@ -1157,12 +1157,100 @@
  * 2
-20
 ;%0A
+%09%09//Get the memory from Allegro itself, since it will manage and free it on its own heap.%0A
 %09%09Sh
@@ -1327,60 +1327,8 @@
 ();%0A
-%0A
-%09%09for (int i=0; i %3C 220; ++i) %7B%0A%09%09%09if (i %3C= 110)%0A%09%09
 %09%09bu
@@ -1325,33 +1325,33 @@
 er();%0A%09%09buf.put(
-i
+0
 , Short.MIN_VALU
@@ -1343,18 +1343,18 @@
  Short.M
-IN
+AX
 _VALUE);
@@ -1358,18 +1358,8 @@
 E);%0A
-%09%09%09else%0A%09%09
 %09%09bu
@@ -1364,17 +1364,17 @@
 buf.put(
-i
+1
 , Short.
@@ -1366,34 +1366,34 @@
 f.put(1, Short.M
-AX
+IN
 _VALUE);%0A%09%09%7D%0A%0A%09%09
@@ -1389,12 +1389,141 @@
 E);%0A
+%0A
 %09%09
-%7D%0A
+//Create the sample of 2 samples at 2 samples per second. That means the speed passed to al_play_sample will be the%0A%09%09//frequency.
 %0A%09%09A
@@ -1567,17 +1567,11 @@
 f, 2
-20, 44100
+, 2
 , AL
@@ -1629,16 +1629,301 @@
 ue);%0A%0A%09%09
+//Setting mixer quality works only before sample instances are attached to it (which happens with al_reserve_samples)%0A%09%09//We need point sampling to produce a true square wave%0A%09%09al_restore_default_mixer();%0A%09%09al_set_mixer_quality(al_get_default_mixer(), ALLEGRO_MIXER_QUALITY_POINT);%0A%0A%09%09
 al_reser
@@ -1943,27 +1943,8 @@
 ;%0A%09%09
-System.out.println(
 al_p
@@ -1961,12 +1961,9 @@
 (s, 
-0.05
+1
 f, A
@@ -1985,17 +1985,19 @@
 N_NONE, 
-1
+200
 f, ALLEG
@@ -2019,17 +2019,16 @@
 P, null)
-)
 ;%0A%09%09Thre
@@ -2040,16 +2040,129 @@
 eep(
+1
 2
-0
 00);%0A%0A
+%09%09//Since we set free_buf parameter to true in al_create_sample, this will al_free the al_malloc'd buffer above.%0A
 %09%09al

Document and optimize AudioExample