Note Block
Minecraft's note block, driven by a computer: read and tune its note, read its instrument, play it, against the computer or anywhere on its cable.
The note block of Minecraft is a peripheral: a computer tunes it and plays it, without clicks and without redstone. Unlike the sound library, it needs no Speaker, and from the Minicomputer on it can stand anywhere along the computer's Data Cable: a carillon on top of a tower, a band of note blocks around a hall, each with its own instrument.
local nb = peripheral.find("note_block")
nb.set_note(12)
nb.play()nb.note() | The note it plays: how many times it was clicked, from 0 (an F#) to 24 (the F# two octaves higher). The notes are the same as those of sound.note. |
nb.instrument() | The instrument it plays. It follows the blocks around it as they change: put a block of gold under it, and it reads "bell" at once. |
nb.set_note(note) | Tunes it, as clicks would, without playing it. The change is applied at the end of the program's tick, once, with the last note asked: note() still reads the old note in the same tick, and sleep(0.05) waits a tick. |
nb.play() | Plays its note, as a redstone pulse would: the sound, the note flying out of it, and a vibration for the Sculk Sensors around. Like set_note, it happens at the end of the tick, once: calling it ten times in a tick plays one note. The changes are applied in the order the program asked them, so set_note then play in the same tick plays the new note. |
Setting up
Reaching it from a computer. Every computer drives it, the Tube Computer included. Put it against a face of the computer and name it by that face, or, from the Minicomputer on, against a Data Cable of the computer's network: it is then named like note_block@120,64,-35. peripheral.find("note_block") takes the first one and peripheral.list("note_block") gives the names of all of them. See Peripherals and Every device.
Its instrument comes from the block under it, as in the game: a block of gold makes a bell, wood a bass, glowstone a pling (the whole list is under sound.instruments). A mob head on top gives it the voice of that mob instead, and then the note does not change its pitch.
Muffled. Any other block on top keeps it silent: play then returns false, as a redstone pulse would play nothing. A note block also still plays when it gets redstone or a click: the computer does not take it over.
When the note block is gone (broken, replaced), every method stops the program with note block removed; when its chunk is not loaded, with peripheral is not loaded.
Reading
nb.note()
The note it plays: how many times it was clicked, from 0 (an F#) to 24 (the F# two octaves higher). The notes are the same as those of sound.note.
- number
- its note, from 0 to 24
local nb = peripheral.find("note_block")
print("tuned to " .. nb.note())See also note_block.set_note() sound.note()
nb.instrument()
The instrument it plays. It follows the blocks around it as they change: put a block of gold under it, and it reads "bell" at once.
- string
- the instrument the block under it gives:
"harp","bass","bell"..., or the mob of a head on top:"zombie","skeleton","creeper","dragon","wither_skeleton","piglin","custom_head"
local nb = peripheral.find("note_block")
if nb.instrument() == "bell" then
print("a bell, there is gold underneath")
endSee also sound.instruments()
Tuning and playing
nb.set_note(note)
Tunes it, as clicks would, without playing it. The change is applied at the end of the program's tick, once, with the last note asked: note() still reads the old note in the same tick, and sleep(0.05) waits a tick.
notenumber- a whole number from 0 to 24; a smaller one becomes 0, a bigger one 24
local nb = peripheral.find("note_block")
nb.set_note(18)
print(nb.note()) -- still the old note
sleep(0.05)
print(nb.note()) -- 18A note must be a whole number: nb.set_note(12.5) stops the program with bad argument #1 to 'set_note' (number has no integer representation). For quarter tones, use sound.note.
See also note_block.note() note_block.play()
nb.play()
Plays its note, as a redstone pulse would: the sound, the note flying out of it, and a vibration for the Sculk Sensors around. Like set_note, it happens at the end of the tick, once: calling it ten times in a tick plays one note. The changes are applied in the order the program asked them, so set_note then play in the same tick plays the new note.
- boolean
truewhen it will sound,falsewhen a block on top muffles it
A carillon: every note block on the cable plays in turn, a scale climbing from block to block.
local bells = peripheral.list("note_block")
local scale = {6, 8, 10, 11, 13, 15, 17, 18}
for i, note in ipairs(scale) do
local nb = peripheral.wrap(bells[(i - 1) % #bells + 1])
nb.set_note(note)
nb.play()
sleep(0.25)
endTo play a chord on one block, or more than one note a tick, use a Speaker and sound.note: a note block plays one note a tick, as in the game.
See also note_block.set_note() sound.note()