Skip to content
Merged
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
169 changes: 146 additions & 23 deletions pxtblocks/fields/field_melodySandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,35 @@ export class FieldCustomMelody<U extends FieldCustomOptions> extends FieldMatrix
let oldValue: string = value;
try {
value = value.slice(1, -1); // remove the boundary quotes
value = value.trim(); // remove boundary white space
this.createMelodyIfDoesntExist();
let notes: string[] = value.split(" ");
const reader = new MelodyStringReader(value, true);

const notes: string[] = [];
const validNotes = [48, 50, 52, 53, 55, 57, 59, 60];

while (reader.hasNextNote()) {
reader.readNote();
if (reader.currentDuration !== 4) throw new Error(lf("Only quarter notes are supported"));

if (reader.currentNote === REST) {
notes.push("-");
continue;
}

const absoluteNote = reader.currentOctave * 12 + reader.currentNote;

notes.forEach(n => {
if (!this.isValidNote(n)) throw new Error(lf("Invalid note '{0}'. Notes can be C D E F G A B C5", n));
});
if (!validNotes.includes(absoluteNote)) {
throw new Error(lf("Invalid note '{0}'. Notes can be C D E F G A B C5", absoluteNote));
}

const noteName = pxtmelody.rowToNote(7 - validNotes.indexOf(absoluteNote));
notes.push(noteName);
}

if (notes.length > this.numMatrixCols) {
throw new Error(lf("Too many notes. Maximum is {0}", this.numMatrixCols));
}

this.createMelodyIfDoesntExist();
this.melody.resetMelody();

for (let j = 0; j < notes.length; j++) {
Expand All @@ -314,27 +335,13 @@ export class FieldCustomMelody<U extends FieldCustomOptions> extends FieldMatrix
}
}
this.updateFieldLabel();
} catch (e) {
}
catch (e) {
pxt.log(e)
this.invalidString = oldValue;
}
}

private isValidNote(note: string): boolean {
switch (note) {
case "C":
case "D":
case "E":
case "F":
case "G":
case "A":
case "B":
case "C5":
case "-": return true;
}
return false;
}

// The width of the preview on the block itself
protected getPreviewWidth(): number {
this.updateSize_();
Expand Down Expand Up @@ -569,7 +576,7 @@ export class FieldCustomMelody<U extends FieldCustomOptions> extends FieldMatrix
if (pxt.BrowserUtils.isEdge()) FieldCustomMelody.VIEWBOX_WIDTH += 37;
FieldCustomMelody.VIEWBOX_HEIGHT = (FieldCustomMelody.CELL_WIDTH + FieldCustomMelody.CELL_VERTICAL_MARGIN) * this.numMatrixRows + FieldCustomMelody.CELL_VERTICAL_MARGIN;
this.matrixSvg = pxsim.svg.parseString(`<svg xmlns="http://www.w3.org/2000/svg" class="melody-grid-div blocklyMatrix" role="grid" viewBox="0 0 ${FieldCustomMelody.VIEWBOX_WIDTH} ${FieldCustomMelody.VIEWBOX_HEIGHT}" tabindex="0" />`);
this.matrixSvg.ariaLabel = lf("Melody grid");
this.matrixSvg.ariaLabel = lf("Melody grid");

this.createMatrixDisplay({
cellWidth: FieldCustomMelody.CELL_WIDTH,
Expand Down Expand Up @@ -944,3 +951,119 @@ function getPlaceholderColor(row: number): string {
}
return "#DCDCDC";
}

const offsetLookup = [0x09, 0x0b, 0x00, 0x02, 0x04, 0x05, 0x07];
const REST = -0xffff;

// copied from pxt-microbit/libs/core/music.ts
class MelodyStringReader {
currentOctave: number;
currentNote: number;
currentDuration: number;
melodyStringIndex: number;

constructor(public melody: string, public resetOctave: boolean) {
this.melodyStringIndex = 0;
this.currentOctave = 4;
this.currentDuration = 4;
this.currentNote = REST;
}

readNote() {
this.eatWhitespace();
if (this.resetOctave) {
this.currentOctave = 4;
}
let note: number = undefined;
let modifier = 0;

while (this.melodyStringIndex < this.melody.length) {
const c = this.melody.charCodeAt(this.melodyStringIndex++);
if (c == 32 /* space */) {
break;
}

else if (c === 35 /* # */) {
modifier++;
}
else if (c === 45 /* - */ || c === 114 /* r */ || c === 82 /* R */) {
if (note !== undefined) {
this.melodyStringIndex--;
break;
}
note = -1;
}
else if (c === 98 /* b */) {
if (note === undefined) {
note = 11;
}
else {
modifier--;
}
}
else if (c >= 48 && c <= 57) {
// number
if (note === undefined) {
// invalid if we haven't seen a note yet, ignore the number
continue;
}
else {
this.melodyStringIndex--;
this.currentOctave = this.readNumber();
}
}
else if (c >= 65 && c <= 71) {
// A-G
if (note !== undefined) {
this.melodyStringIndex--;
break;
}
note = offsetLookup[c - 65];
}
else if (c >= 97 && c <= 103) {
// a-g
if (note !== undefined) {
this.melodyStringIndex--;
break;
}
note = offsetLookup[c - 97];
}
else if (c === 58 /* : */) {
this.currentDuration = Math.max(1, this.readNumber());
break;
}
}

if (note === undefined || note < 0) {
// invalid note, treat as rest
this.currentNote = REST;
}
else {
this.currentNote = note + modifier;
}

this.eatWhitespace();
}

readNumber() {
let result = 0;
while (this.melodyStringIndex < this.melody.length) {
const c = this.melody.charCodeAt(this.melodyStringIndex);
if (c < 48 || c > 57) break;
result = result * 10 + (c - 48);
this.melodyStringIndex++;
}
return result;
}

eatWhitespace() {
while (this.melodyStringIndex < this.melody.length && this.melody.charAt(this.melodyStringIndex) == " ") {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question in the other PR about if this should be a === or not.

this.melodyStringIndex++;
}
}

hasNextNote(): boolean {
this.eatWhitespace();
return this.melodyStringIndex < this.melody.length;
}
}
2 changes: 1 addition & 1 deletion pxtlib/melody-editor/melodyArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ namespace pxtmelody {

export function noteToRow(note: string): number {
let rowNum: number = -1;
switch (note) {
switch (note.toUpperCase()) {
case "C5": rowNum = 0; break;
case "B": rowNum = 1; break;
case "A": rowNum = 2; break;
Expand Down
Loading