Skip to content

Commit 3a772e3

Browse files
authored
Add frequency (Hz) display to Sampler Widget (sugarlabs#4616)
* Add frequency display to Sampler Widget * Fix pitch button * Use platformColor.textColor instead of hardcoded color --------- Co-authored-by: Anvita Prasad <cs23b1059.iiitdm.ac.in>
1 parent 16edd6c commit 3a772e3

File tree

1 file changed

+56
-4
lines changed

1 file changed

+56
-4
lines changed

js/widgets/sampler.js

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,32 @@ function SampleWidget() {
434434
window.scroll(0, 0);
435435
};
436436

437-
this.pitchBtn = widgetWindow.addInputButton("C4", "");
438-
this.pitchBtn.onclick = () => {
437+
// Create a container for the pitch button and frequency display
438+
this.pitchBtnContainer = document.createElement("div");
439+
this.pitchBtnContainer.className = "wfbtItem";
440+
this.pitchBtnContainer.style.display = "flex";
441+
this.pitchBtnContainer.style.flexDirection = "column";
442+
this.pitchBtnContainer.style.alignItems = "center";
443+
this.pitchBtnContainer.style.cursor = "pointer"; // Add pointer cursor to indicate clickable
444+
445+
// Add the container to the toolbar
446+
widgetWindow._toolbar.appendChild(this.pitchBtnContainer);
447+
448+
// Create the pitch button
449+
this.pitchBtn = document.createElement("input");
450+
this.pitchBtn.value = "C4";
451+
this.pitchBtnContainer.appendChild(this.pitchBtn);
452+
453+
// Create the frequency display
454+
this.frequencyDisplay = document.createElement("div");
455+
this.frequencyDisplay.style.fontSize = "smaller";
456+
this.frequencyDisplay.style.textAlign = "center";
457+
this.frequencyDisplay.style.color = platformColor.textColor;
458+
this.frequencyDisplay.textContent = "261 Hz";
459+
this.pitchBtnContainer.appendChild(this.frequencyDisplay);
460+
461+
// Add click event to the container (includes both the button and frequency display)
462+
this.pitchBtnContainer.onclick = () => {
439463
this._createPieMenu();
440464
};
441465

@@ -564,6 +588,24 @@ function SampleWidget() {
564588
this.octaveCenter = this.sampleOctave;
565589
};
566590

591+
/**
592+
* Calculates the frequency in Hz for the current pitch.
593+
* @returns {number} The frequency in Hz
594+
*/
595+
this._calculateFrequency = function () {
596+
let semitones = 0;
597+
598+
semitones += isNaN(this.octaveCenter) ? 0 : this.octaveCenter * 12;
599+
semitones += isNaN(this.pitchCenter) ? 0 : MAJORSCALE[this.pitchCenter];
600+
semitones += isNaN(this.accidentalCenter) ? 0 : this.accidentalCenter - 2;
601+
602+
// A4 = 440Hz at semitone position 57
603+
const netChange = semitones - 57;
604+
const frequency = Math.floor(440 * Math.pow(2, netChange / 12));
605+
606+
return frequency;
607+
};
608+
567609
/**
568610
* Updates the sample pitch value based on the pitch, accidental, and octave centers.
569611
* @returns {void}
@@ -879,8 +921,9 @@ function SampleWidget() {
879921
};
880922

881923
/**
882-
* Retrieves the name of the current pitch.
883-
* @returns {void}
924+
* Gets the pitch name based on the current pitch, accidental, and octave.
925+
* Also updates the pitch button display.
926+
* @returns {string} The pitch name.
884927
*/
885928
this.getPitchName = function () {
886929
let name = "";
@@ -889,7 +932,16 @@ function SampleWidget() {
889932
name += this.octaveCenter.toString();
890933
this.pitchName = name;
891934

935+
// Calculate frequency
936+
const frequency = this._calculateFrequency();
937+
938+
// Update the pitch button value
892939
this.pitchBtn.value = this.pitchName;
940+
941+
// Update the frequency display text
942+
this.frequencyDisplay.textContent = frequency + " Hz";
943+
944+
return this.pitchName;
893945
};
894946

895947
/**

0 commit comments

Comments
 (0)