Browse Source

playing a WAV file

tth 1 year ago
parent
commit
1bd06fb67b
7 changed files with 141 additions and 26 deletions
  1. 14
    2
      README.md
  2. 7
    2
      audio/Makefile
  3. 27
    14
      audio/ao_output.c
  4. 3
    3
      audio/ao_output.h
  5. 70
    0
      audio/playfile.c
  6. 3
    0
      audio/playfile.h
  7. 17
    5
      audio/t.c

+ 14
- 2
README.md View File

@@ -2,9 +2,21 @@
2 2
 
3 3
 Les scratchmen utilisent des loopers en Flash 11.
4 4
 Il est temps qu'ils découvrent une interface ncurses
5
-pilotée au joystick :)
5
+pilotable au joystick ou par OSC :)
6 6
 
7 7
 ## Prérequis :
8 8
 
9
-libsndfile, libao, libcurses.
9
+libsndfile, libao, liblo, libcurses.avec les bons -dev !
10
+
11
+## Compilation :
12
+
13
+Pour le moment, vous êtes laissé à vous-même.
14
+
15
+## Hacking :
16
+
17
+La voie est libre.
18
+
19
+
20
+
21
+
10 22
 

+ 7
- 2
audio/Makefile View File

@@ -6,6 +6,11 @@ LIBS    = -lao -lsndfile -lm
6 6
 ao_output.o:       ao_output.c Makefile
7 7
 	$(CC) ${CCOPT} -c $<
8 8
 
9
-t:	t.c ao_output.o Makefile
10
-	$(CC) ${CCOPT} $< ao_output.o ${LIBS} -o $@
9
+playfile.o:       playfile.c Makefile
10
+	$(CC) ${CCOPT} -c $<
11
+
12
+OBJS    = ao_output.o playfile.o
13
+
14
+t:	t.c ${OBJS} Makefile
15
+	$(CC) ${CCOPT} $< ${OBJS} ${LIBS} -o $@
11 16
 

+ 27
- 14
audio/ao_output.c View File

@@ -1,5 +1,5 @@
1 1
 /*
2
- *      NcLooper        fonctions audio
2
+ *      NcLooper        fonctions audio output
3 3
  */
4 4
 
5 5
 #include  <stdio.h>
@@ -15,13 +15,14 @@
15 15
 
16 16
 /* --------------------------------------------------------------------- */
17 17
 
18
-static ao_device	*device;	/*  never alone with a singleton */
18
+extern int		verbosity;
19 19
 
20 20
 /* --------------------------------------------------------------------- */
21
-int init_ao_output(int smplrate)
21
+ao_device *init_ao_output(int smplrate)
22 22
 {
23 23
 int			default_driver;
24 24
 ao_sample_format	format;
25
+ao_device		*device;
25 26
 
26 27
 #if DEBUG_LEVEL
27 28
 fprintf(stderr, ">>> %s ( %d )\n", __func__, smplrate);
@@ -43,33 +44,39 @@ format.byte_format = AO_FMT_LITTLE;
43 44
 device = ao_open_live(default_driver, &format, NULL);
44 45
 if (device == NULL) {
45 46
         fprintf(stderr, "Error opening AO device.\n");
46
-        return -1;
47
+        return NULL;
47 48
         }
48 49
 
50
+#if DEBUG_LEVEL
51
+fprintf(stderr, "%s : device at %p\n", __func__, device);
52
+#endif
49 53
 
50
-return 0;
54
+return device;
51 55
 }
52 56
 /* --------------------------------------------------------------------- */
53
-int close_ao_output(void)
57
+int close_ao_output(ao_device *dev)
54 58
 {
55 59
 
56
-ao_close(device);
57
-ao_shutdown();
60
+if (NULL == dev) {
61
+	fprintf(stderr, "%s : no output to close\n", __func__);
62
+	return -1;
63
+	}
58 64
 
59
-device = NULL;
65
+ao_close(dev);
66
+ao_shutdown();
60 67
 
61 68
 return 0;
62 69
 }
63 70
 /* --------------------------------------------------------------------- */
64
-int play_some_stuff(int notused)
71
+int play_some_stuff(ao_device *dev, int notused)
65 72
 {
66 73
 int			i, sample;
67 74
 char			*buffer;
68 75
 float			freq = 440.0;
69 76
 
70
-#define NB_SAMPLES	44100
77
+#define NB_SAMPLES	(44100*2)
71 78
 
72
-if (NULL == device) {
79
+if (NULL == dev) {
73 80
 	fprintf(stderr, "%s : please call 'init_ao_output' first\n",
74 81
 					__func__);
75 82
 	exit(1);
@@ -85,7 +92,13 @@ for (i = 0; i < NB_SAMPLES; i++) {
85 92
 	buffer[4*i] = buffer[4*i+2] = sample & 0xff;
86 93
 	buffer[4*i+1] = buffer[4*i+3] = (sample >> 8) & 0xff;
87 94
 	}
88
-ao_play(device, buffer, NB_SAMPLES);
95
+ao_play(dev, buffer, NB_SAMPLES);
96
+
97
+free(buffer);
89 98
 
90 99
 return 0;
91
-}
100
+}
101
+/* --------------------------------------------------------------------- */
102
+
103
+/* --------------------------------------------------------------------- */
104
+

+ 3
- 3
audio/ao_output.h View File

@@ -1,10 +1,10 @@
1 1
 
2 2
 /* generic output */
3 3
 
4
-int init_ao_output(int smplrate);
5
-int close_ao_output(void);
4
+ao_device * init_ao_output(int smplrate);
5
+int close_ao_output(ao_device *dev);
6 6
 
7 7
 
8 8
 /* tests functions */
9 9
 
10
-int play_some_stuff(int notused);
10
+int play_some_stuff(ao_device *dev, int notused);

+ 70
- 0
audio/playfile.c View File

@@ -0,0 +1,70 @@
1
+/*
2
+ *      NcLooper        fonctions audio output
3
+ */
4
+
5
+#include  <stdio.h>
6
+#include  <unistd.h>
7
+#include  <string.h>
8
+#include  <fcntl.h>
9
+#include  <math.h>
10
+
11
+#include <ao/ao.h>
12
+#include <sndfile.h>
13
+
14
+#include  "ao_output.h"
15
+
16
+/* --------------------------------------------------------------------- */
17
+
18
+extern int			verbosity;
19
+
20
+#define		T_BUFF_WAVES	16384
21
+
22
+/* --------------------------------------------------------------------- */
23
+
24
+int blast_this_file(char *fname, ao_device *dev, int loop)
25
+{
26
+SNDFILE                 *sndf;
27
+SF_INFO                 sfinfo;
28
+int                     foo, lus;
29
+short			*buffer;
30
+
31
+#if DEBUG_LEVEL
32
+fprintf(stderr, ">>> %s ( '%s' %p %d )\n", __func__, fname, dev, loop);
33
+#endif
34
+
35
+sndf = sf_open(fname, SFM_READ, &sfinfo);
36
+if (NULL==sndf)
37
+	{
38
+	perror("sf_open");
39
+	abort();
40
+	}
41
+
42
+buffer = calloc(T_BUFF_WAVES, sizeof(short)*2);
43
+
44
+#if DEBUG_LEVEL
45
+fprintf(stderr, "samplerate : %d\n", sfinfo.samplerate);
46
+fprintf(stderr, "frames     : %ld\n", sfinfo.frames);
47
+fprintf(stderr, "seekable   : %d\n", sfinfo.seekable);
48
+#endif
49
+
50
+while ((lus = sf_read_short(sndf, buffer, T_BUFF_WAVES))) {
51
+
52
+#if DEBUG_LEVEL
53
+        fprintf(stderr, "%s : %d bytes read\n", fname, lus);
54
+#endif
55
+	foo = ao_play(dev, buffer, T_BUFF_WAVES*2);
56
+#if DEBUG_LEVEL
57
+        fprintf(stderr, "%s : %d played\n", fname, foo);
58
+#endif
59
+
60
+	}
61
+
62
+/* do some cleanup */
63
+free(buffer);
64
+sf_close(sndf);
65
+
66
+return 0;
67
+}
68
+
69
+
70
+/* --------------------------------------------------------------------- */

+ 3
- 0
audio/playfile.h View File

@@ -0,0 +1,3 @@
1
+
2
+
3
+int blast_this_file(char *fname, ao_device *dev, int loop);

+ 17
- 5
audio/t.c View File

@@ -3,25 +3,37 @@
3 3
  */
4 4
  
5 5
 #include  <stdio.h>
6
+#include <unistd.h>
7
+
8
+#include <ao/ao.h>
6 9
 
7 10
 #include  "ao_output.h"
11
+#include  "playfile.h"
8 12
 
9 13
 /* --------------------------------------------------------------------- */
10 14
 
15
+int			verbosity;
16
+
11 17
 /* --------------------------------------------------------------------- */
12 18
 
13 19
 int main(int argc, char *argv[])
14 20
 {
15
-int             foo;
21
+int			foo;
22
+ao_device		*device;
16 23
 
17 24
 
18
-foo = init_ao_output(44100);
19
-fprintf(stderr, "AO init    -> %d\n", foo);
25
+device = init_ao_output(44100);
26
+fprintf(stderr, "AO init    -> %p\n", device);
20 27
 
21
-foo = play_some_stuff(0);
28
+foo = play_some_stuff(device, 0);
22 29
 fprintf(stderr, "play stuff -> %d\n", foo);
23 30
 
24
-foo = close_ao_output();
31
+sleep(1);
32
+
33
+foo = blast_this_file("/home/tth/BU/vrac/1337.wav", device, 0);
34
+fprintf(stderr, "blast file -> %d\n", foo);
35
+
36
+foo = close_ao_output(device);
25 37
 fprintf(stderr, "AO close   -> %d\n", foo);
26 38
 
27 39
 

Loading…
Cancel
Save