annotate src/samer/midi/MidiRecorder.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
rev   line source
samer@0 1 /*
samer@0 2 * Copyright (c) 2000, Samer Abdallah, King's College London.
samer@0 3 * All rights reserved.
samer@0 4 *
samer@0 5 * This software is provided AS iS and WITHOUT ANY WARRANTY;
samer@0 6 * without even the implied warranty of MERCHANTABILITY or
samer@0 7 * FITNESS FOR A PARTICULAR PURPOSE.
samer@0 8 */
samer@0 9
samer@0 10 package samer.midi;
samer@0 11 import samer.core.*;
samer@0 12 import samer.core.types.*;
samer@0 13 import samer.tools.*;
samer@0 14 import samer.maths.*;
samer@0 15 import java.util.*;
samer@0 16 import java.io.*;
samer@0 17 import javax.sound.midi.*;
samer@0 18
samer@0 19 /**
samer@0 20 Attempts to do some temporal smoothing on the
samer@0 21 imput vector. Its a bit like a median filter.
samer@0 22 A note doesn't switch on until the corresponding
samer@0 23 vector element has been above threshold for
samer@0 24 <countThresh> consecutive steps, and similarly
samer@0 25 for switching off.
samer@0 26 */
samer@0 27
samer@0 28 public class MidiRecorder extends MidiRecorderBase implements Task
samer@0 29 {
samer@0 30 Vec in;
samer@0 31 int n;
samer@0 32 int [] nnums, chans;
samer@0 33 double [] x;
samer@0 34 VInteger offset;
samer@0 35 VBoolean pedal;
samer@0 36 VDouble factor;
samer@0 37
samer@0 38 public MidiRecorder(Vec input) throws Exception {
samer@0 39
samer@0 40 Shell.push(node);
samer@0 41 factor = new VDouble("factor",128,0);
samer@0 42 offset = new VInteger("offset",0,0);
samer@0 43 pedal = new VBoolean("pedal",false,0);
samer@0 44 // setAgent(this);
samer@0 45 // addAgent(new Saver(this));
samer@0 46 Shell.pop();
samer@0 47
samer@0 48 in = input;
samer@0 49 n = in.size();
samer@0 50 x = in.array();
samer@0 51
samer@0 52 nnums = new int[n];
samer@0 53 chans = new int[n];
samer@0 54 for (int i=0; i<n; i++) { nnums[i]=i; chans[i]=0; }
samer@0 55 }
samer@0 56
samer@0 57 public int mapVelocity(double x) {
samer@0 58 int vel=(int)(factor.value*x);
samer@0 59 if (vel>127) vel=127;
samer@0 60 return vel;
samer@0 61 }
samer@0 62
samer@0 63 public void dispose() {}
samer@0 64 public void starting() {}
samer@0 65 public void stopping() {}
samer@0 66
samer@0 67 public void run()
samer@0 68 {
samer@0 69 for (int i=0; i<n; i++) {
samer@0 70
samer@0 71 if (x[i]>0) {
samer@0 72 int nnum=nnums[i]+offset.value;
samer@0 73 int vel=mapVelocity(x[i]);
samer@0 74 noteOn(chans[i],nnum, vel);
samer@0 75 } else if (x[i]<0 && !pedal.value) {
samer@0 76 int nnum=nnums[i]+offset.value;
samer@0 77 noteOff(chans[i],nnum);
samer@0 78 }
samer@0 79 }
samer@0 80 tick();
samer@0 81 }
samer@0 82 }