This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
edza101's profile picture

Chime - Music thing for immibis speakers

Started by edza101, 22 February 2014 - 06:49 AM
edza101 #1
Posted 22 February 2014 - 07:49 AM
Hello! Was experimenting with some modded peripherals and ran across immibis's speakers. I decided to transcribe c4 major into its Hz forms and make a program that read and played them!


The Code:


--Configuration
local speakerSide = "top"
local playSpeed = 0.2
local playVolume = 20
--Argument Definition
local filePath, playNotes = ...
--Note Dictionary
local note = {
["a"] = 220,
["b"] = 248.942,
["c"] = 261.626,
["d"] = 293.665,
["e"] = 329.628,
["f"] = 349.228,
["g"] = 391.995
}
--Usage Label
local function usage()
print("This program is designed to play music in notation 'aabbcc'.")
print("Usage: chime [File Path] [true to show notes, or false to not]")
end
--File Loader and Parser
local function fileParse()
return fs.open(filePath,"r").readLine()
end
--Player
local function play()
peripheral.wrap(speakerSide).setAttenuation(playVolume)
if playNotes == "false" then
   if c ~= " " then
	peripheral.wrap(speakerSide).start(1,note[c])
	sleep(playSpeed)
	peripheral.wrap(speakerSide).stop(1)
   else
	sleep(playSpeed)
   end
elseif playNotes == "true" then
  for c in fileParse():gmatch"." do
   if c ~= " " then
	peripheral.wrap(speakerSide).start(1,note[c])
	sleep(playSpeed)
	peripheral.wrap(speakerSide).stop(1)
	print(c)
   else
	sleep(playSpeed)
   end
  end
else
  usage()
end
end
--Main
local function main()
term.clear()
term.setCursorPos(1,1)
play()
print("Song Finished")
end
main()


Sample Music File (mary had a little lamb :D/>):


edcdeee ddd eee edcdeeeeddedc

Usage:

chime [file path] [true for show notes, false for not]

Download:

Pastebin adress: q4mgL5YB or hyperlinked

I know it isnt perfect but I think its a pretty cool idea.
Edited on 23 February 2014 - 05:26 AM
willwac #2
Posted 26 February 2014 - 01:29 AM
how do you make a song file?