changeset 157:ed7e1fb91745

Add some audiofile test data, start on tests and reworking stream interface -- doesn't currently compile
author Chris Cannam
date Wed, 01 May 2013 07:58:39 +0100
parents 4ab709d2c1b0
children b6db07468ed1
files test/data/44100-2-16.wav test/data/8000-1-8.wav yetilab/stream/audiofile.yeti yetilab/stream/syntheticstream.yeti yetilab/stream/test/audiofile_reference.yeti yetilab/stream/test/test_stream.yeti
diffstat 6 files changed, 58 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
Binary file test/data/44100-2-16.wav has changed
Binary file test/data/8000-1-8.wav has changed
--- a/yetilab/stream/audiofile.yeti	Tue Apr 30 15:10:02 2013 +0100
+++ b/yetilab/stream/audiofile.yeti	Wed May 01 07:58:39 2013 +0100
@@ -55,11 +55,11 @@
         fi
     fi);
 
-readInterleaved' { format is ~AudioFormat, stream is ~AudioInputStream } nframes =
+readInterleaved' { format is ~AudioFormat, aistream is ~AudioInputStream } nframes =
    (channels = format#getChannels();
     bytesPerSample = format#getSampleSizeInBits() / 8;
     bytes = new byte[nframes * channels * bytesPerSample];
-    bytesRead = stream#read(bytes);
+    bytesRead = aistream#read(bytes);
     if bytesRead <= 0 then block.zeros 0;
     else
         n = int(bytesRead / bytesPerSample);
@@ -69,44 +69,44 @@
     fi;
    );
 
-read' { format is ~AudioFormat, stream is ~AudioInputStream } n =
-   (b = readInterleaved' { format, stream } n;
+read' { format is ~AudioFormat, aistream is ~AudioInputStream } n =
+   (b = readInterleaved' { format, aistream } n;
     channels = format#getChannels();
     ch.deinterleaved channels b;
    );
 
-readMono' { format is ~AudioFormat, stream is ~AudioInputStream } n =
-   (b = readInterleaved' { format, stream } n;
+readMono' { format is ~AudioFormat, aistream is ~AudioInputStream } n =
+   (b = readInterleaved' { format, aistream } n;
     channels = format#getChannels();
     ch.mixedDownFromInterleaved channels b;
    );
 
-// Note, all this assumes the stream is non-blocking (i.e. available()
-// is to the end of file)
+// Note, all this assumes aistream is non-blocking (i.e. available()
+// is to the end of file). Our stream interface does support
+// indefinite and infinite streams, but audiofile doesn't yet.
 
-available' { format is ~AudioFormat, stream is ~AudioInputStream } =
-    stream#available() / ((format#getSampleSizeInBits() / 8) * format#getChannels());
+available' { format is ~AudioFormat, aistream is ~AudioInputStream } =
+    aistream#available() / ((format#getSampleSizeInBits() / 8) * format#getChannels());
 
-close' { stream is ~AudioInputStream } =
-    stream#close();
+close' { aistream is ~AudioInputStream } =
+    aistream#close();
 
 open name is string -> 'a = 
    (f = new File(name);
-    stream = AudioSystem#getAudioInputStream(f);
-    format = stream#getFormat();
-    len = available' { format, stream }; // at start of stream
+    aistream = AudioSystem#getAudioInputStream(f);
+    format = aistream#getFormat();
+    len = available' { format, aistream }; // at start of stream
     str.stream {
-        stream,
         format,
-        len,
+        length = Known len,
         rate = format#getSampleRate(),
         channels = format#getChannels(),
-        get position () = len - available' { stream, format },
-        get available () = available' { stream, format },
-        read = read' { stream, format },
-        readInterleaved = readInterleaved' { stream, format },
-        readMono = readMono' { stream, format },
-        close () = close' { stream },
+        get position () = len - available' { aistream, format },
+        get available () = Known (available' { aistream, format }),
+        read = read' { aistream, format },
+        readInterleaved = readInterleaved' { aistream, format },
+        readMono = readMono' { aistream, format },
+        close () = close' { aistream },
     });
 
 {
--- a/yetilab/stream/syntheticstream.yeti	Tue Apr 30 15:10:02 2013 +0100
+++ b/yetilab/stream/syntheticstream.yeti	Wed May 01 07:58:39 2013 +0100
@@ -5,27 +5,26 @@
 vec = load yetilab.block.fvector;
 block = load yetilab.block.block;
 
-generated rate generator seconds =
+generated rate generator =
+    // generator takes time in seconds as arg, returns number in -1,+1 range
     str.monoStream {
         var position = 0,
-        len = int(seconds * rate + 0.5),
         rate,
         read count = 
-           (rc = min count (len - position);
-            result = vec.zeros rc;
-            for [0..rc-1] do i:
+           (result = vec.zeros count;
+            for [0..count-1] do i:
                 result[i] := generator ((position + i) / rate)
             done;
-            position := position + rc;
+            position := position + count;
             block.block result),
         close = \(),
         };
 
-sinusoid rate freq seconds =
-    generated rate (sin . (* (freq / (2*pi * rate)))) seconds;
+sinusoid rate freq =
+    generated rate (sin . (* (freq / (2*pi * rate))));
 
-whiteNoise rate seconds =
-    generated rate \((Math#random() * 2.0) - 1.0) seconds;
+whiteNoise rate =
+    generated rate \((Math#random() * 2.0) - 1.0);
 
 precalculated rate data is number -> ~double[] -> 'a =
    (n = vec.length data;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/yetilab/stream/test/audiofile_reference.yeti	Wed May 01 07:58:39 2013 +0100
@@ -0,0 +1,9 @@
+
+module yetilab.stream.test.audiofile_reference;
+
+syn = load yetilab.stream.syntheticstream;
+str = load yetilab.stream.stream;
+vec = load yetilab.block.fvector;
+block = load yetilab.block.block;
+
+    
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/yetilab/stream/test/test_stream.yeti	Wed May 01 07:58:39 2013 +0100
@@ -0,0 +1,17 @@
+
+module yetilab.stream.test.test_stream;
+
+str = load yetilab.stream.stream;
+block = load yetilab.block.block;
+
+{ compare, testStream } = load yetilab.test.test;
+
+[
+
+"generated": \(
+//    s = str.generated 10
+//        do i:
+)
+            
+] is hash<string, () -> boolean>;
+