changeset 58:f6c70e462cc3

Tidy up Vamp interface and eliminate some Java types
author Chris Cannam
date Wed, 09 Jan 2013 21:36:17 +0000
parents 08b2b9fce25c
children 6ce0ffdc7b00 7e48a92b6bbd
files vamp.yeti
diffstat 1 files changed, 109 insertions(+), 82 deletions(-) [+]
line wrap: on
line diff
--- a/vamp.yeti	Wed Jan 09 21:35:37 2013 +0000
+++ b/vamp.yeti	Wed Jan 09 21:36:17 2013 +0000
@@ -3,7 +3,7 @@
 import org.vamp_plugins:
        Plugin, Plugin$InputDomain,
        PluginLoader, PluginLoader$AdapterFlags,
-       ParameterDescriptor, OutputDescriptor,
+       ParameterDescriptor, OutputDescriptor, OutputDescriptor$SampleType,
        RealTime, Feature;
 
 import java.lang: UnsatisfiedLinkError;
@@ -14,77 +14,6 @@
 fr = load framer;
 af = load audiofile;
 
-listPlugins () =
-   (try
-        map string PluginLoader#getInstance()#listPlugins();
-    catch UnsatisfiedLinkError e:
-        eprintln "Warning: Unable to obtain plugin list:\n\(e)";
-        [];
-    yrt);
-   
-categoryOf key =
-    list PluginLoader#getInstance()#getPluginCategory(key);
-
-parameterDescriptor pd is ~ParameterDescriptor -> 'a = {
-    identifier = pd#identifier,
-    name = pd#name,
-    description = pd#description,
-    unit = pd#unit,
-    minValue = pd#minValue,
-    maxValue = pd#maxValue,
-    defaultValue = pd#defaultValue,
-    isQuantized = pd#isQuantized,
-    quantizeStep = pd#quantizeStep,
-    valueNames = map string pd#valueNames
-    };
-
-outputDescriptor od is ~OutputDescriptor -> 'a = {
-    identifier = od#identifier,
-    name = od#name,
-    description = od#description,
-    unit = od#unit,
-    hasFixedBinCount = od#hasFixedBinCount,
-    binCount = od#binCount,
-    binNames = map string od#binNames,
-    hasKnownExtents = od#hasKnownExtents,
-    minValue = od#minValue,
-    maxValue = od#maxValue,
-    isQuantized = od#isQuantized,
-    quantizeStep = od#quantizeStep,
-    sampleType = od#sampleType,
-    sampleRate = od#sampleRate,
-    hasDuration = od#hasDuration,
-    };
-
-plugin key p is string -> ~Plugin -> 'a = {
-    plugin = p,
-    key,
-    get apiVersion () = p#getVampApiVersion(),
-    get identifier () = p#getIdentifier(),
-    get name () = p#getName(),
-    get description () = p#getDescription(),
-    get maker () = p#getMaker(),
-    get copyright () = p#getCopyright(),
-    get version () = p#getPluginVersion(),
-    get category () = PluginLoader#getInstance()#getPluginCategory(key),
-    get parameters () = map parameterDescriptor p#getParameterDescriptors(),
-    parameter identifier = p#getParameter(identifier),
-    setParameter identifier value = p#setParameter(identifier, value),
-    get programs () = map string p#getPrograms(),
-    get currentProgram () = p#getCurrentProgram(),
-    selectProgram pr = p#selectProgram(pr),
-    get inputDomain () = p#getInputDomain(),
-    get preferredBlockSize () = p#getPreferredBlockSize(),
-    get preferredStepSize () = p#getPreferredStepSize(),
-    get minChannelCount () = p#getMinChannelCount(),
-    get maxChannelCount () = p#getMaxChannelCount(),
-    initialise channels blocksize hop = p#initialise(channels, blocksize, hop),
-    reset () = p#reset(),
-    get outputs () = map outputDescriptor p#getOutputDescriptors(),
-    process floats time is ~float[][] -> ~RealTime -> 'a = p#process(floats, 0, time),
-    dispose () = p#dispose(),
-    };
-
 realTime r is ~RealTime -> number = r#sec() + (r#nsec() / 1000000000);
 
 feature f is ~Feature -> 'a = {
@@ -104,10 +33,107 @@
         list result
     fi;
 
-featuresFromSet outputNo f is number -> ~TreeMap -> 'a =
-    featureList (f#get(outputNo as ~Integer));
+numberOf n is ~Object -> number =
+   (n unsafely_as ~Integer)#intValue();
 
-converted outputNo fl is number -> list?<~TreeMap> -> 'a =
+featureSet fs is ~TreeMap -> 'a =
+   (s = [:];
+    kk = list fs#keySet()#toArray(); //!!! ugh
+    for kk do k: s[numberOf k] := featureList fs#get(k) done;
+    s);
+
+listPlugins () =
+   (try
+        map string PluginLoader#getInstance()#listPlugins();
+    catch UnsatisfiedLinkError e:
+        eprintln "Warning: Unable to obtain plugin list:\n\(e)";
+        [];
+    yrt);
+   
+categoryOf key =
+    list PluginLoader#getInstance()#getPluginCategory(key);
+
+inputDomain d is ~Plugin$InputDomain -> 'a = 
+    if d == Plugin$InputDomain#FREQUENCY_DOMAIN then
+        FrequencyDomain ()
+    else
+        TimeDomain ()
+    fi;
+
+parameterDescriptor pd is ~ParameterDescriptor -> 'a = {
+    identifier = pd#identifier,
+    name = pd#name,
+    description = pd#description,
+    unit = pd#unit,
+    minValue = pd#minValue,
+    maxValue = pd#maxValue,
+    defaultValue = pd#defaultValue,
+    isQuantized = pd#isQuantized,
+    quantizeStep = pd#quantizeStep,
+    valueNames = map string pd#valueNames
+    };
+
+sampleType t is ~OutputDescriptor$SampleType -> 'a =
+    if t == OutputDescriptor$SampleType#OneSamplePerStep then
+        OneSamplePerStep ()
+    elif t == OutputDescriptor$SampleType#FixedSampleRate then
+        FixedSampleRate ()
+    else
+        VariableSampleRate ()
+    fi;
+
+outputDescriptor od is ~OutputDescriptor -> 'a = {
+    identifier = od#identifier,
+    name = od#name,
+    description = od#description,
+    unit = od#unit,
+    hasFixedBinCount = od#hasFixedBinCount,
+    binCount = od#binCount,
+    binNames = map string od#binNames,
+    hasKnownExtents = od#hasKnownExtents,
+    minValue = od#minValue,
+    maxValue = od#maxValue,
+    isQuantized = od#isQuantized,
+    quantizeStep = od#quantizeStep,
+    sampleType = sampleType od#sampleType,
+    sampleRate = od#sampleRate,
+    hasDuration = od#hasDuration,
+    };
+
+plugin key p is string -> ~Plugin -> 'a = {
+    plugin = p,
+    key,
+    get apiVersion () = p#getVampApiVersion(),
+    get identifier () = p#getIdentifier(),
+    get name () = p#getName(),
+    get description () = p#getDescription(),
+    get maker () = p#getMaker(),
+    get copyright () = p#getCopyright(),
+    get version () = p#getPluginVersion(),
+    get category () = PluginLoader#getInstance()#getPluginCategory(key),
+    get parameters () = array (map parameterDescriptor p#getParameterDescriptors()),
+    parameter identifier = p#getParameter(identifier),
+    setParameter identifier value = p#setParameter(identifier, value),
+    get programs () = array (map string p#getPrograms()),
+    get currentProgram () = p#getCurrentProgram(),
+    selectProgram pr = p#selectProgram(pr),
+    get inputDomain () = inputDomain p#getInputDomain(),
+    get preferredBlockSize () = p#getPreferredBlockSize(),
+    get preferredStepSize () = p#getPreferredStepSize(),
+    get minChannelCount () = p#getMinChannelCount(),
+    get maxChannelCount () = p#getMaxChannelCount(),
+    initialise { channels, blocksize, hop } = p#initialise(channels, blocksize, hop),
+    reset () = p#reset(),
+    get outputs () = array (map outputDescriptor p#getOutputDescriptors()),
+    process blocks time is 'a -> ~RealTime -> 'b = 
+        featureSet p#process((map block.floats blocks) as ~float[][], 0, time),
+    getRemainingFeatures () = featureSet p#getRemainingFeatures(),
+    dispose () = p#dispose(),
+    };
+
+featuresFromSet outputNo f = if outputNo in f then f[outputNo] else [] fi;
+
+converted outputNo fl =
     map (featuresFromSet outputNo) fl;
 
 outputNumberByName p name =
@@ -119,15 +145,14 @@
 
 processed { p, sampleRate, hop } frames count =
     case frames of
-        this::rest:
-            p.process
-                ([block.floats this] as ~float[][])
-                RealTime#frame2RealTime(count, sampleRate)
+        frame::rest:
+            p.process [frame] RealTime#frame2RealTime(count, sampleRate)
             :.
             \(processed { p, sampleRate, hop } rest (count + hop));
         _: 
-           (p.dispose ();
-            []);
+           (rf = p.getRemainingFeatures ();
+            p.dispose ();
+            [rf]);
     esac;
 
 process key output stream =
@@ -135,12 +160,14 @@
     blockSize = p.preferredBlockSize;
     hop = p.preferredStepSize;
     outputNo = outputNumberByName p output;
-    if p.initialise 1 blockSize hop then
+    if p.initialise { channels = 1, blocksize = blockSize, hop } then
         converted outputNo
            (processed
             { p, sampleRate = stream.sampleRate, hop } 
             (fr.frames { framesize = blockSize, hop } stream)
             0);
+        // If processing completed successfully, then p is disposed by
+        // processed and stream is closed by the framer
     else
         p.dispose();
         stream.close();