改声音目录

This commit is contained in:
whm1216
2025-12-02 19:11:04 +08:00
parent 3ecb7d8cfb
commit 69484c9c6b
11 changed files with 241 additions and 218 deletions

View File

@@ -1,75 +1,10 @@
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) {
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;
const frequencyInput = generator.valueToCode(this, "FREQUENCY", generator.ORDER_ATOMIC);
const durationInput = generator.valueToCode(this, "DURATION", generator.ORDER_ATOMIC);
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`;
return `sound.play_frequency(${frequencyInput}, ${durationInput})\n`;
};

View File

@@ -1,58 +1,9 @@
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) {
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;
const frequencyInput = generator.valueToCode(this, "FREQUENCY", generator.ORDER_ATOMIC);
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`;
return `sound.play_frequency_no_duration(${frequencyInput})\n`;
};

View File

@@ -1,23 +1,9 @@
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 noteList = this.getFieldValue("NOTE_LIST");
const useBlocking = hasPlayWaitBefore(_block);
const methodName = useBlocking ? 'play_note_list_blocking' : 'play_note_list';
return `sound.${methodName}("${noteList}")\n`;
return `sound.play_note_list("${noteList}")\n`;
};

View File

@@ -1,6 +1,7 @@
export const sound_note = function(_block, generator) {
const note = _block.getFieldValue("NOTE") || "NOTE_A4";
return [`"${note}"`, generator.ORDER_ATOMIC];
export const sound_note = function (_block, generator) {
// 获取频率值(字符串格式)
const frequency = this.getFieldValue("NOTE");
return [frequency, generator.ORDER_ATOMIC];
};