Messy, and needs much more work, but sound effects work.
authorDavid Griffith <dave@661.org>
Thu, 5 Dec 2013 00:43:07 +0000 (16:43 -0800)
committerDavid Griffith <dave@661.org>
Thu, 5 Dec 2013 00:43:07 +0000 (16:43 -0800)
Makefile
src/curses/ux_audio.c
src/curses/ux_init.c

index d11f1028eacdaf9c3075ce1ade81785a9ffca7fe..927424a20a57f47b3539d4247feb5f2cc356cb3c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -193,7 +193,7 @@ CURSES_DEFS = $(OPT_DEFS) $(COLOR_DEFS) $(SOUND_DEFS) $(SOUNDCARD) \
 
 FLAGS = $(OPTS) $(CURSES_DEFS) $(INCL)
 
-SOUND_LIB = -lao -ldl -lm
+SOUND_LIB = -lao -ldl -lm -lsndfile
 
 $(NAME): $(NAME)-curses
 curses:  $(NAME)-curses
index 8f9c7de9a14ab3ee35591dab6da78c10f2617586..4a702bc6d4d4dd108ee069b53fce7878da49f9b2 100644 (file)
 #include <curses.h>
 #endif
 
+#include <stdio.h>
 #include <string.h>
 #include <ao/ao.h>
+#include <sndfile.h>
 #include <math.h>
 
 #include "ux_frotz.h"
@@ -41,7 +43,7 @@ typedef struct EFFECT {
     int type;
     int active;
     int voice;
-    char *buffer;
+    int *buffer;
     int buflen;
     int repeats;
     int volume;
@@ -64,7 +66,7 @@ static EFFECT *getaiff(FILE *, size_t, int, int);
 static EFFECT *geteffect(int);
 static EFFECT *new_effect(int, int);
 
-
+/*
 int ux_initsound(void)
 {
     ao_sample_format format;
@@ -100,6 +102,7 @@ void ux_stopsound(void)
     ao_close(device);
     ao_shutdown();
 }
+*/
 
 static EFFECT *geteffect(int num)
 {
@@ -154,37 +157,77 @@ static EFFECT *new_effect(int type, int num)
 }
 
 
-static EFFECT *getaiff(FILE *f, size_t pos, int len, int num)
+static EFFECT *getaiff(FILE *fp, size_t pos, int len, int num)
 {
+    ao_device *device;
+    ao_sample_format format;
+    int default_driver;
+    SNDFILE     *sndfile;
+    SF_INFO     sf_info;
+
     EFFECT *sample;
     void *data;
     int size;
     int count;
 
+    int frames_read;
+
+    int *buffer;
+
+
     sample = new_effect(SFX_TYPE, num);
     if (sample == NULL || sample == 0)
        return sample;
 
-    if (fseek(f, pos, SEEK_SET) != 0)
+    if (fseek(fp, pos, SEEK_SET) != 0)
        return NULL;
 
-    count = 0;
-    sample->buffer = malloc(sizeof(int) * len);
+    ao_initialize();
+    default_driver = ao_default_driver_id();
+
+    sf_info.format = 0;
+    sndfile = sf_open_fd(fileno(fp), SFM_READ, &sf_info, 1);
+
+    memset(&format, 0, sizeof(ao_sample_format));
+
+    format.byte_format = AO_FMT_NATIVE;
+    format.bits = 32;
+    format.channels = sf_info.channels;
+    format.rate = sf_info.samplerate;
 
+    device = ao_open_live(default_driver, &format, NULL /* no options */);
+    if (device == NULL) {
+        printf("Error opening sound device.\n");
+        exit(1);
+    }
+
+    buffer = malloc(sizeof(int) * sf_info.frames * sf_info.channels);
+
+    sf_read_int(sndfile, buffer, sf_info.frames);
+
+    ao_play(device, (char *)buffer, sf_info.frames * sizeof(int));
+    ao_close(device);
+    ao_shutdown();
+
+    sf_close(sndfile);
+
+/*
     while (count <= len) {
        fread(sample->buffer + count, 1, 1, f);
        count++;
     }
 
     sample->buflen = count;
-
+*/
     return sample;
 }
 
 static void startsample()
 {
+/*
     if (e_sfx == NULL) return;
     ao_play(device, e_sfx->buffer, e_sfx->buflen);
+*/
 }
 
 static void stopsample()
@@ -266,7 +309,7 @@ void os_start_sample (int number, int volume, int repeats, zword eos)
 {
     EFFECT *e;
 
-    if (!audiorunning) return;
+//    if (!audiorunning) return;
     e = geteffect(number);
     if (e == NULL) return;
 //    if (e->type == SFX_TYPE) stopsample();
index ab5238915281fb806515ef1834bd746cd5512537..6b74f2f9d3d66fdd4831dcb1678072f41a6bcf8f 100644 (file)
@@ -543,7 +543,7 @@ FILE *os_load_story(void)
          break;
     }
 
-    ux_initsound();
+//    ux_initsound();
 //    os_start_sample(3, 8, 1, 0);
 //    exit(1);