Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating pitch detection package #1310

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libs/pitch-detection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Pitch Detection

A small pitch detection library.
292 changes: 292 additions & 0 deletions libs/pitch-detection/pitch-detection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,292 @@
// enum Note {
// //% blockIdentity=music.noteFrequency enumval=262
// C = 262,
// //% block=C#
// //% blockIdentity=music.noteFrequency enumval=277
// CSharp = 277,
// //% blockIdentity=music.noteFrequency enumval=294
// D = 294,
// //% blockIdentity=music.noteFrequency enumval=311
// Eb = 311,
// //% blockIdentity=music.noteFrequency enumval=330
// E = 330,
// //% blockIdentity=music.noteFrequency enumval=349
// F = 349,
// //% block=F#
// //% blockIdentity=music.noteFrequency enumval=370
// FSharp = 370,
// //% blockIdentity=music.noteFrequency enumval=392
// G = 392,
// //% block=G#
// //% blockIdentity=music.noteFrequency enumval=415
// GSharp = 415,
// //% blockIdentity=music.noteFrequency enumval=440
// A = 440,
// //% blockIdentity=music.noteFrequency enumval=466
// Bb = 466,
// //% blockIdentity=music.noteFrequency enumval=494
// B = 494,
// //% blockIdentity=music.noteFrequency enumval=131
// C3 = 131,
// //% block=C#3
// //% blockIdentity=music.noteFrequency enumval=139
// CSharp3 = 139,
// //% blockIdentity=music.noteFrequency enumval=147
// D3 = 147,
// //% blockIdentity=music.noteFrequency enumval=156
// Eb3 = 156,
// //% blockIdentity=music.noteFrequency enumval=165
// E3 = 165,
// //% blockIdentity=music.noteFrequency enumval=175
// F3 = 175,
// //% block=F#3
// //% blockIdentity=music.noteFrequency enumval=185
// FSharp3 = 185,
// //% blockIdentity=music.noteFrequency enumval=196
// G3 = 196,
// //% block=G#3
// //% blockIdentity=music.noteFrequency enumval=208
// GSharp3 = 208,
// //% blockIdentity=music.noteFrequency enumval=220
// A3 = 220,
// //% blockIdentity=music.noteFrequency enumval=233
// Bb3 = 233,
// //% blockIdentity=music.noteFrequency enumval=247
// B3 = 247,
// //% blockIdentity=music.noteFrequency enumval=262
// C4 = 262,
// //% block=C#4
// //% blockIdentity=music.noteFrequency enumval=277
// CSharp4 = 277,
// //% blockIdentity=music.noteFrequency enumval=294
// D4 = 294,
// //% blockIdentity=music.noteFrequency enumval=311
// Eb4 = 311,
// //% blockIdentity=music.noteFrequency enumval=330
// E4 = 330,
// //% blockIdentity=music.noteFrequency enumval=349
// F4 = 349,
// //% block=F#4
// //% blockIdentity=music.noteFrequency enumval=370
// FSharp4 = 370,
// //% blockIdentity=music.noteFrequency enumval=392
// G4 = 392,
// //% block=G#4
// //% blockIdentity=music.noteFrequency enumval=415
// GSharp4 = 415,
// //% blockIdentity=music.noteFrequency enumval=440
// A4 = 440,
// //% blockIdentity=music.noteFrequency enumval=466
// Bb4 = 466,
// //% blockIdentity=music.noteFrequency enumval=494
// B4 = 494,
// //% blockIdentity=music.noteFrequency enumval=523
// C5 = 523,
// //% block=C#5
// //% blockIdentity=music.noteFrequency enumval=555
// CSharp5 = 555,
// //% blockIdentity=music.noteFrequency enumval=587
// D5 = 587,
// //% blockIdentity=music.noteFrequency enumval=622
// Eb5 = 622,
// //% blockIdentity=music.noteFrequency enumval=659
// E5 = 659,
// //% blockIdentity=music.noteFrequency enumval=698
// F5 = 698,
// //% block=F#5
// //% blockIdentity=music.noteFrequency enumval=740
// FSharp5 = 740,
// //% blockIdentity=music.noteFrequency enumval=784
// G5 = 784,
// //% block=G#5
// //% blockIdentity=music.noteFrequency enumval=831
// GSharp5 = 831,
// //% blockIdentity=music.noteFrequency enumval=880
// A5 = 880,
// //% blockIdentity=music.noteFrequency enumval=932
// Bb5 = 932,
// //% blockIdentity=music.noteFrequency enumval=988
// B5 = 988,
// }

// enum BeatFraction {
// //% block=1
// Whole = 1,
// //% block="1/2"
// Half = 2,
// //% block="1/4"
// Quarter = 4,
// //% block="1/8"
// Eighth = 8,
// //% block="1/16"
// Sixteenth = 16,
// //% block="2"
// Double = 32,
// //% block="4",
// Breve = 64,
// //% block="1/3",
// Triplet = 128
// }

enum NoteName{
//% blockIdentity=music.noteFrequency enumval=262
C = 262,
//% block=C#
//% blockIdentity=music.noteFrequency enumval=277
CSharp = 277,
//% blockIdentity=music.noteFrequency enumval=294
D = 294,
//% blockIdentity=music.noteFrequency enumval=311
Eb = 311,
//% blockIdentity=music.noteFrequency enumval=330
E = 330,
//% blockIdentity=music.noteFrequency enumval=349
F = 349,
//% block=F#
//% blockIdentity=music.noteFrequency enumval=370
FSharp = 370,
//% blockIdentity=music.noteFrequency enumval=392
G = 392,
//% block=G#
//% blockIdentity=music.noteFrequency enumval=415
GSharp = 415,
//% blockIdentity=music.noteFrequency enumval=440
A = 440,
//% blockIdentity=music.noteFrequency enumval=466
Bb = 466,
//% blockIdentity=music.noteFrequency enumval=494
B = 494,
//% blockIdentity=music.noteFrequency enumval=131
C3 = 131,
//% block=C#3
//% blockIdentity=music.noteFrequency enumval=139
CSharp3 = 139,
//% blockIdentity=music.noteFrequency enumval=147
D3 = 147,
//% blockIdentity=music.noteFrequency enumval=156
Eb3 = 156,
//% blockIdentity=music.noteFrequency enumval=165
E3 = 165,
//% blockIdentity=music.noteFrequency enumval=175
F3 = 175,
//% block=F#3
//% blockIdentity=music.noteFrequency enumval=185
FSharp3 = 185,
//% blockIdentity=music.noteFrequency enumval=196
G3 = 196,
//% block=G#3
//% blockIdentity=music.noteFrequency enumval=208
GSharp3 = 208,
//% blockIdentity=music.noteFrequency enumval=220
A3 = 220,
//% blockIdentity=music.noteFrequency enumval=233
Bb3 = 233,
//% blockIdentity=music.noteFrequency enumval=247
B3 = 247,
//% blockIdentity=music.noteFrequency enumval=262
C4 = 262,
//% block=C#4
//% blockIdentity=music.noteFrequency enumval=277
CSharp4 = 277,
//% blockIdentity=music.noteFrequency enumval=294
D4 = 294,
//% blockIdentity=music.noteFrequency enumval=311
Eb4 = 311,
//% blockIdentity=music.noteFrequency enumval=330
E4 = 330,
//% blockIdentity=music.noteFrequency enumval=349
F4 = 349,
//% block=F#4
//% blockIdentity=music.noteFrequency enumval=370
FSharp4 = 370,
//% blockIdentity=music.noteFrequency enumval=392
G4 = 392,
//% block=G#4
//% blockIdentity=music.noteFrequency enumval=415
GSharp4 = 415,
//% blockIdentity=music.noteFrequency enumval=440
A4 = 440,
//% blockIdentity=music.noteFrequency enumval=466
Bb4 = 466,
//% blockIdentity=music.noteFrequency enumval=494
B4 = 494,
//% blockIdentity=music.noteFrequency enumval=523
C5 = 523,
//% block=C#5
//% blockIdentity=music.noteFrequency enumval=555
CSharp5 = 555,
//% blockIdentity=music.noteFrequency enumval=587
D5 = 587,
//% blockIdentity=music.noteFrequency enumval=622
Eb5 = 622,
//% blockIdentity=music.noteFrequency enumval=659
E5 = 659,
//% blockIdentity=music.noteFrequency enumval=698
F5 = 698,
//% block=F#5
//% blockIdentity=music.noteFrequency enumval=740
FSharp5 = 740,
//% blockIdentity=music.noteFrequency enumval=784
G5 = 784,
//% block=G#5
//% blockIdentity=music.noteFrequency enumval=831
GSharp5 = 831,
//% blockIdentity=music.noteFrequency enumval=880
A5 = 880,
//% blockIdentity=music.noteFrequency enumval=932
Bb5 = 932,
//% blockIdentity=music.noteFrequency enumval=988
B5 = 988,
}

//* looked at music extension for guidance *//
namespace pitchdetection {
let beatsPerMinute: number;

function init() {
if (!beatsPerMinute) beatsPerMinute = 120;
}

/**
* Return the duration of a beat in milliseconds (the beat fraction).
* @param note the fraction of the current whole note, eg: BeatFraction.Half
*/
//% blockId=instrament_note block="%note|note"
//% weight=11 blockGap=8
//% group="Values"
export function noteName(note?: NoteName): string {
//To Need to fill this up
let noteName;
switch (note) {
case NoteName.C: noteName = "C"; break;
case NoteName.CSharp: noteName = "CSharp"; break;
case NoteName.E: noteName = "E"; break;
case NoteName.A: noteName = "A"; break;
case NoteName.G: noteName = "G"; break;
case NoteName.D: noteName = "D"; break;

}
return noteName;
}

/**
* Runs code each time the next note of a melody needs to be played.
* TODO: Not sure what reporter is, need to create the correct UI
*
*/
//% blockId=pitchdetection_onNoteUpdate
//% block="play melody %sound=music_sounds|until done at %value=bpm|"
//% weight=20
//% group="Events"

export function onNoteUpdate(note?: NoteName, noteB?: NoteName) {
// QUESTIONS?
// what does the ? on the param mean?
// how do i turn this in a container?
// How should I create a dataprovider for this? Thinking datalogger preset and load your own.
// create a BPM timer https://gist.github.com/Digikid13/79d250918b2973bb8f68
// read TabDown (markdown for music) https://github.com/ultimate-guitar/Tabdown
}

}

14 changes: 14 additions & 0 deletions libs/pitch-detection/pxt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "Pitch Detection",
"description": "Detect pitch from external microphone",
"files": [
"README.md",
"shims.d.ts",
"pitch-detection.ts"

],
"public": true,
"dependencies": {
"core": "file:../core"
}
}
44 changes: 44 additions & 0 deletions libs/pitch-detection/shims.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//% icon="\uf075"
//% color="#f59c16"
//% block="Pitch Detection"
declare namespace pitchdetection {

/**
* Setup environment for pitch detection.
*/
//% shim=pitchdetection::_initializePitchDetection
//% blockId=init_pitch_detection
//% block="setup detector"
//% weight=90
function initializePitchDetection(): void;

/**
* Start listening on microphone.
*/
//% shim=pitchdetection::_startLiveInput
//% blockId=start_live_input
//% block="start listening to microphone"
//% weight=91
function startLiveInput(): void;

//TODO:
//function pauseLiveInput():void;

/**
* Reads the current note detected
*/
//% blockId=note_value block="note value"
//% group="Values" weight=49 blockGap=8
//% shim=pitchdetection::_getNote
function getNote():string;

/**
* Reads the current pitch detected
*/
//% blockId=pitch_value block="pitch value"
//% group="Values" weight=50 blockGap=8
//% shim=pitchdetection::_getPitch
function getPitch():number;


}
Loading