online加声音目录

This commit is contained in:
whm1216
2025-12-02 00:57:34 +08:00
parent 7835a77ae2
commit 3ecb7d8cfb
37 changed files with 2444 additions and 2 deletions

View File

@@ -0,0 +1,21 @@
export const sound_effect_add = function(_block, _generator) {
if (!_generator.definitions_['import_sound']) {
_generator.definitions_['import_sound'] = 'import sound';
}
const effect = _block.getFieldValue("EFFECT");
const valueInput = _block.getInputTargetBlock("VALUE");
let val;
if (valueInput) {
if (valueInput.type === "math_number") {
val = valueInput.getFieldValue("NUM") || "10";
} else {
val = _generator.valueToCode(valueInput, "VALUE", _generator.ORDER_NONE) || "10";
}
} else {
val = "10";
}
return `sound.adjust_effect("${effect}", ${val})\n`;
};

View File

@@ -0,0 +1,7 @@
export const sound_effect_clear = function(_block, _generator) {
if (!_generator.definitions_['import_sound']) {
_generator.definitions_['import_sound'] = 'import sound';
}
return "sound.clear_effects()\n";
};

View File

@@ -0,0 +1,21 @@
export const sound_effect_set = function(_block, _generator) {
if (!_generator.definitions_['import_sound']) {
_generator.definitions_['import_sound'] = 'import sound';
}
const effect = _block.getFieldValue("EFFECT");
const valueInput = _block.getInputTargetBlock("VALUE");
let val;
if (valueInput) {
if (valueInput.type === "math_number") {
val = valueInput.getFieldValue("NUM") || "100";
} else {
val = _generator.valueToCode(valueInput, "VALUE", _generator.ORDER_NONE) || "100";
}
} else {
val = "100";
}
return `sound.set_effect("${effect}", ${val})\n`;
};

View File

@@ -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`;
};

View File

@@ -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`;
};

View File

@@ -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`;
};

View File

@@ -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`;
};

View File

@@ -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`;
};

View File

@@ -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`;
};

View File

@@ -0,0 +1,6 @@
export const sound_note = function(_block, generator) {
const note = _block.getFieldValue("NOTE") || "NOTE_A4";
return [`"${note}"`, generator.ORDER_ATOMIC];
};

View File

@@ -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";
};

View File

@@ -0,0 +1,8 @@
export const sound_volume_add = function(_block, generator) {
if (!generator.definitions_['import_sound']) {
generator.definitions_['import_sound'] = 'import sound';
}
const val = generator.valueToCode(_block, "VALUE", generator.ORDER_NONE) || "0";
return `sound.adjust_volume(${val})\n`;
};

View File

@@ -0,0 +1,7 @@
export const sound_volume_get = function(_block, generator) {
if (!generator.definitions_['import_sound']) {
generator.definitions_['import_sound'] = 'import sound';
}
return ['sound.get_volume()', generator.ORDER_ATOMIC];
};

View File

@@ -0,0 +1,8 @@
export const sound_volume_set = function(_block, generator) {
if (!generator.definitions_['import_sound']) {
generator.definitions_['import_sound'] = 'import sound';
}
const val = generator.valueToCode(_block, "VALUE", generator.ORDER_NONE) || "100";
return `sound.set_volume(${val})\n`;
};