Skip to content

Commit

Permalink
README and forkme.
Browse files Browse the repository at this point in the history
  • Loading branch information
cwilso committed Oct 5, 2012
1 parent 8042485 commit be871b3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Naive simple pitch detection

I whipped this app up to start experimenting with pitch detection, and also to test live audio input. It performs a naive (zero-crossing based) pitch detection algorithm in realtime. It works best today with whistling (which has a clear, simple waveform); it needs some severe help with more complex waveforms, even like guitar. I'll get back to this eventually. :) There are much better tuners out there right now - like [Craig Spence's work](http://phenomnomnominal.github.com/).

Check it out, feel free to fork, submit pull requests, etc.

-Chris
Binary file added img/forkme.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
<body>
<script src="js/pitchdetect.js"></script>

<button onclick="this.innerText = togglePlayback()">start</button>
<button onclick="this.innerText = togglePlayback()">use demo audio</button>
<button onclick="toggleLiveInput()">use live input</button>
<!--<button onclick="updatePitch(0);">sample</button>-->

<div id="detector" class="vague">
Expand All @@ -32,6 +33,7 @@
<div class="note"><span id="note">--</span></div>
<div id="detune"><span id="detune_amt">--</span><span id="flat">cents &#9837;</span><span id="sharp">cents &#9839;</span></div>
</div>
<a href="https://github.com/cwilso/pitchdetect" style="position: absolute; z-index:2; top: 0; right: 0; border: 0;"><img src="img/forkme.png" alt="Fork me on GitHub"></a>

</body>
</html>
39 changes: 37 additions & 2 deletions js/pitchdetect.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var detectorElem,

window.onload = function() {
var request = new XMLHttpRequest();
request.open("GET", "../sounds/whistling2.ogg", true);
request.open("GET", "../sounds/whistling3.ogg", true);
request.responseType = "arraybuffer";
request.onload = function() {
audioContext.decodeAudioData( request.response, function(buffer) {
Expand Down Expand Up @@ -49,8 +49,37 @@ window.onload = function() {
};
reader.readAsArrayBuffer(e.dataTransfer.files[0]);
return false;
};
};



}

function error() {
alert('Stream generation failed.');
}

function getUserMedia(dictionary, callback) {
try {
navigator.webkitGetUserMedia(dictionary, callback, error);
} catch (e) {
alert('webkitGetUserMedia threw exception :' + e);
}
}

function gotStream(stream) {
// Create an AudioNode from the stream.
var mediaStreamSource = audioContext.createMediaStreamSource(stream);

// Connect it to the destination.
analyser = audioContext.createAnalyser();
analyser.fftSize = 2048;
mediaStreamSource.connect( analyser );
updatePitch();
}

function toggleLiveInput() {
getUserMedia({audio:true}, gotStream);
}

function togglePlayback() {
Expand All @@ -76,6 +105,7 @@ function togglePlayback() {
analyser.connect( audioContext.destination );
sourceNode.noteOn( now );
isPlaying = true;
isLiveInput = false;
updatePitch();

return "stop";
Expand Down Expand Up @@ -146,12 +176,17 @@ function updatePitch( time ) {
// find the first point
var last_zero = findNextPositiveZeroCrossing( 0 );

var n=0;
// keep finding points, adding cycle lengths to array
while ( last_zero != -1) {
var next_zero = findNextPositiveZeroCrossing( last_zero + 1 );
if (next_zero > -1)
cycles.push( next_zero - last_zero );
last_zero = next_zero;

n++;
if (n>1000)
break;
}

// 1?: average the array
Expand Down

0 comments on commit be871b3

Please sign in to comment.