online加声音目录
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
export const sound_play = function(_block, _generator) {
|
||||
if (!_generator.definitions_['import_sound']) {
|
||||
_generator.definitions_['import_sound'] = 'import sound';
|
||||
}
|
||||
|
||||
const sound = _block.getFieldValue("SOUND");
|
||||
if (sound === "record") {
|
||||
return `sound.record()\n`;
|
||||
}
|
||||
return `sound.play("${sound}")\n`;
|
||||
};
|
||||
@@ -0,0 +1,75 @@
|
||||
function hasPlayWaitBefore(block) {
|
||||
let currentBlock = block.getPreviousBlock();
|
||||
while (currentBlock) {
|
||||
if (currentBlock.type === 'sound_play_wait') {
|
||||
return true;
|
||||
}
|
||||
currentBlock = currentBlock.getPreviousBlock();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export const sound_play_frequency = function(_block, generator) {
|
||||
if (!generator.definitions_['import_sound']) {
|
||||
generator.definitions_['import_sound'] = 'import sound';
|
||||
}
|
||||
|
||||
const frequencyInput = _block.getInputTargetBlock("FREQUENCY");
|
||||
const durationInput = _block.getInputTargetBlock("DURATION");
|
||||
let frequencyCode, durationCode;
|
||||
|
||||
if (frequencyInput) {
|
||||
try {
|
||||
if (frequencyInput.type === "sound_note") {
|
||||
const note = frequencyInput.getFieldValue("NOTE") || "NOTE_A4";
|
||||
const noteFrequencies = {
|
||||
"NOTE_B3": 247,
|
||||
"NOTE_C4": 262,
|
||||
"NOTE_D4": 294,
|
||||
"NOTE_E4": 330,
|
||||
"NOTE_F4": 349,
|
||||
"NOTE_G4": 392,
|
||||
"NOTE_A4": 440,
|
||||
"NOTE_B4": 494,
|
||||
"NOTE_C5": 523,
|
||||
"NOTE_D5": 587,
|
||||
"NOTE_E5": 659,
|
||||
"NOTE_F5": 698,
|
||||
"NOTE_G5": 784
|
||||
};
|
||||
frequencyCode = noteFrequencies[note] || 440;
|
||||
} else if (frequencyInput.type === "math_number") {
|
||||
const numValue = frequencyInput.getFieldValue("NUM");
|
||||
frequencyCode = numValue || "440";
|
||||
} else {
|
||||
frequencyCode = generator.valueToCode(frequencyInput, "FREQUENCY", generator.ORDER_ATOMIC);
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("生成频率代码时出错:", error);
|
||||
frequencyCode = "440";
|
||||
}
|
||||
} else {
|
||||
frequencyCode = "440";
|
||||
}
|
||||
|
||||
if (durationInput) {
|
||||
try {
|
||||
if (durationInput.type === "math_number") {
|
||||
const numValue = durationInput.getFieldValue("NUM");
|
||||
durationCode = numValue || "1000";
|
||||
} else {
|
||||
durationCode = generator.valueToCode(durationInput, "DURATION", generator.ORDER_ATOMIC);
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("生成持续时间代码时出错:", error);
|
||||
durationCode = "1000";
|
||||
}
|
||||
} else {
|
||||
durationCode = "1000";
|
||||
}
|
||||
|
||||
const useBlocking = hasPlayWaitBefore(_block);
|
||||
const methodName = useBlocking ? 'play_frequency_blocking' : 'play_frequency';
|
||||
|
||||
return `sound.${methodName}(${frequencyCode}, ${durationCode})\n`;
|
||||
};
|
||||
@@ -0,0 +1,58 @@
|
||||
function hasPlayWaitBefore(block) {
|
||||
let currentBlock = block.getPreviousBlock();
|
||||
while (currentBlock) {
|
||||
if (currentBlock.type === 'sound_play_wait') {
|
||||
return true;
|
||||
}
|
||||
currentBlock = currentBlock.getPreviousBlock();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export const sound_play_frequency_no_duration = function(_block, generator) {
|
||||
if (!generator.definitions_['import_sound']) {
|
||||
generator.definitions_['import_sound'] = 'import sound';
|
||||
}
|
||||
|
||||
const frequencyInput = _block.getInputTargetBlock("FREQUENCY");
|
||||
let frequencyCode;
|
||||
|
||||
if (frequencyInput) {
|
||||
try {
|
||||
if (frequencyInput.type === "sound_note") {
|
||||
const note = frequencyInput.getFieldValue("NOTE") || "NOTE_A4";
|
||||
const noteFrequencies = {
|
||||
"NOTE_B3": 247,
|
||||
"NOTE_C4": 262,
|
||||
"NOTE_D4": 294,
|
||||
"NOTE_E4": 330,
|
||||
"NOTE_F4": 349,
|
||||
"NOTE_G4": 392,
|
||||
"NOTE_A4": 440,
|
||||
"NOTE_B4": 494,
|
||||
"NOTE_C5": 523,
|
||||
"NOTE_D5": 587,
|
||||
"NOTE_E5": 659,
|
||||
"NOTE_F5": 698,
|
||||
"NOTE_G5": 784
|
||||
};
|
||||
frequencyCode = noteFrequencies[note] || 440;
|
||||
} else if (frequencyInput.type === "math_number") {
|
||||
const numValue = frequencyInput.getFieldValue("NUM");
|
||||
frequencyCode = numValue || "440";
|
||||
} else {
|
||||
frequencyCode = generator.valueToCode(frequencyInput, "FREQUENCY", generator.ORDER_ATOMIC);
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("生成频率代码时出错:", error);
|
||||
frequencyCode = "440";
|
||||
}
|
||||
} else {
|
||||
frequencyCode = "440";
|
||||
}
|
||||
|
||||
const useBlocking = hasPlayWaitBefore(_block);
|
||||
const methodName = useBlocking ? 'play_frequency_blocking' : 'play_frequency';
|
||||
|
||||
return `sound.${methodName}(${frequencyCode}, 0)\n`;
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
function hasPlayWaitBefore(block) {
|
||||
let currentBlock = block.getPreviousBlock();
|
||||
while (currentBlock) {
|
||||
if (currentBlock.type === 'sound_play_wait') {
|
||||
return true;
|
||||
}
|
||||
currentBlock = currentBlock.getPreviousBlock();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export const sound_play_note_list = function(_block, _generator) {
|
||||
if (!_generator.definitions_['import_sound']) {
|
||||
_generator.definitions_['import_sound'] = 'import sound';
|
||||
}
|
||||
|
||||
const noteList = _block.getFieldValue("NOTE_LIST") || "DADADADUM";
|
||||
|
||||
const useBlocking = hasPlayWaitBefore(_block);
|
||||
const methodName = useBlocking ? 'play_note_list_blocking' : 'play_note_list';
|
||||
|
||||
return `sound.${methodName}("${noteList}")\n`;
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
export const sound_play_wait = function(_block, _generator) {
|
||||
if (!_generator.definitions_['import_sound']) {
|
||||
_generator.definitions_['import_sound'] = 'import sound';
|
||||
}
|
||||
|
||||
const sound = _block.getFieldValue("SOUND");
|
||||
if (sound === "record") {
|
||||
return `sound.record()\n`;
|
||||
}
|
||||
|
||||
return `sound.play_blocking("${sound}")\n`;
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
export const sound_record = function(_block, _generator) {
|
||||
if (!_generator.definitions_['import_sound']) {
|
||||
_generator.definitions_['import_sound'] = 'import sound';
|
||||
}
|
||||
|
||||
return `sound.record()\n`;
|
||||
};
|
||||
@@ -0,0 +1,6 @@
|
||||
export const sound_note = function(_block, generator) {
|
||||
const note = _block.getFieldValue("NOTE") || "NOTE_A4";
|
||||
return [`"${note}"`, generator.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
export const sound_stop_all = function(_block, _generator) {
|
||||
if (!_generator.definitions_['import_sound']) {
|
||||
_generator.definitions_['import_sound'] = 'import sound';
|
||||
}
|
||||
|
||||
return "sound.stop_all()\n";
|
||||
};
|
||||
Reference in New Issue
Block a user