Permalink
Browse files

iOS: small changes to pitch tracking

  • Loading branch information...
1 parent eb13f6e commit d8b8ba6710d48b54a8046f330a561d0881874811 @charlieroberts-rit charlieroberts-rit committed Oct 1, 2011
Showing with 31 additions and 9 deletions.
  1. +1 −0 iOS/Control/Classes/MIDI.m
  2. +6 −3 iOS/Control/Plugins/AudioInput.mm
  3. +24 −6 www/js/AudioPitch.js
@@ -256,6 +256,7 @@ - (void)send:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
int msgType = [[midiDict objectForKey:[arguments objectAtIndex:0]] intValue];
if(msgType != Sysex) {
+ //NSLog(@"msgType = %d, value = %d", msgType, [[arguments objectAtIndex:1] intValue] - 1);
myMessage.data[0] = msgType + [[arguments objectAtIndex:1] intValue] - 1;
myMessage.data[1] = [[arguments objectAtIndex:2] intValue];
@@ -112,7 +112,7 @@ static OSStatus inRenderProc (void *inRefCon,
float freqBinSize = 22050.f / (inNumberFrames / 2);
freq = maxBin * freqBinSize;
- printf("freq = %f\n", maxBin * freqBinSize);
+ //printf("freq = %f\n", maxBin * freqBinSize);
}
jsCallBack = [[NSMutableString alloc] initWithFormat:@"audioPitch._onPitchUpdate(%f);", freq];
@@ -209,6 +209,9 @@ - (PhoneGapCommand*) initWithWebView:(UIWebView*)theWebView {
return self;
}
+- (void)restart:(NSMutableArray *)arguments withDict:(NSMutableDictionary *) options {
+ AudioOutputUnitStart(inRemoteIOUnit);
+}
- (void)start:(NSMutableArray *)arguments withDict:(NSMutableDictionary *) options {
if([[arguments objectAtIndex:0] isEqualToString:@"volume"]) {
@@ -223,7 +226,7 @@ - (void)start:(NSMutableArray *)arguments withDict:(NSMutableDictionary *) optio
if([[arguments objectAtIndex:1] isEqualToString:@"zero"]){
pitchMode = PITCH_ZERO;
} else {
- //NSLog(@"setting mode to HPS");
+ NSLog(@"setting mode to HPS");
pitchMode = PITCH_HPS;
}
@@ -295,7 +298,7 @@ - (void)start:(NSMutableArray *)arguments withDict:(NSMutableDictionary *) optio
if(!initialized) {
AudioUnitInitialize(inRemoteIOUnit);
AudioOutputUnitStart(inRemoteIOUnit);
- initialized = YES;
+ //initialized = YES;
}
}
View
@@ -6,6 +6,7 @@ function AudioPitch(props) {
this.number = 21;
this.noteName = "A0";
this.freq = 0;
+ this.freqs = [];
this.hardwareMin = 0;
this.hardwareMax = 127;
@@ -18,7 +19,7 @@ function AudioPitch(props) {
this.max = (typeof props.max != "undefined") ? props.max : this.hardwareMax;
this.min = (typeof props.min != "undefined") ? props.min : this.hardwareMin;
}
-
+ this.maxFreqs = 4;
this.userDefinedRange = this.max - this.min;
this.pitch = 0;
@@ -32,18 +33,35 @@ AudioPitch.prototype = new Widget();
AudioPitch.prototype.start = function() {
PhoneGap.exec("AudioInput.start", "pitch", this.mode);
-}
+}
+
+AudioPitch.prototype.restart = function() {
+ PhoneGap.exec("AudioInput.restart");
+}
AudioPitch.prototype.stop = function() {
PhoneGap.exec("AudioInput.stop", "pitch");
}
AudioPitch.prototype._onPitchUpdate = function(newFreq) {
- this.freq = newFreq;
- this.pitch = Math.floor(69 + 12 * Math.log(newFreq / 440) / Math.log(2));
+ if(newFreq > 20)
+ this.freqs.unshift(newFreq);
+
+ // basic averaging
+ var freqTotal = 0;
+ for(var i = 0; i < this.freqs.length; i++) {
+ freqTotal += this.freqs[i];
+ }
+ this.freq = freqTotal / this.freqs.length;
+ while(this.freqs.length >= this.maxFreqs) { this.freqs.pop(); }
+
+ this.pitch = Math.round(69 + 12 * Math.log(newFreq / 440) / Math.log(2));
this.octave = Math.round(this.pitch / 12) - 2;
- this.number = Math.round(this.pitch % 12) + 1;
- this.noteName = this.noteNames[this.number] + this.octave;
+ this.number = Math.round(this.pitch % 12);
+ if(this.octave < 128)
+ this.noteName = this.noteNames[this.number] + this.octave;
+ else
+ this.noteName = "-";
// console.log("PITCH NAME = " + this.pitch);

0 comments on commit d8b8ba6

Please sign in to comment.