Jump to content




Love music but not too good with code...


  • You cannot reply to this topic
40 replies to this topic

#1 Huufalem

  • New Members
  • 15 posts

Posted 02 June 2012 - 03:49 AM

Hello guys! Sorry to ask this but Im not to great with Lua coding, But I really love music, and after seeing a few videos on YouTube involving computrecraft and the ability to get rid of a lot of red stone and save a lot of space I decided to give it ago but My coding skills are lacking. Basically what I am looking to do is line up tuned note blocks and run cables to them then be able to have the computer play music. If someone could point me in the right direction as to achieving this goal that would be great! Thank you so much for your time and help!

#2 zapwow

  • New Members
  • 4 posts

Posted 02 June 2012 - 04:01 AM

One thing you could try is using bundled cables. Connect a bundled cable to a computer, and connect it to up to 16 different note blocks using the appropriately colored insulated wire.
Then use redstone.setBundledOutput to activate different note blocks.
You could even connect one color to multiple note blocks, so that activating a color would play a chord.

#3 kazagistar

  • Members
  • 365 posts

Posted 02 June 2012 - 07:14 PM

rs.setBundledOutput("back", colors.white+colors.red)
os.sleep(0.2)
rs.setBundledOutput("back", 0)
os.sleep(0.2)
rs.setBundledOutput("back", colors.red+colors.blue+colors.green)
os.sleep(0.2)
rs.setBundledOutput("back", 0)
os.sleep(0.2)

Of course, this is dependent on having red power wiring installed. If you do not, you are going to have to create a binary selector using different directions of redstone cabling, which requires some serious work.

Edited for correctness.

#4 Huufalem

  • New Members
  • 15 posts

Posted 02 June 2012 - 07:32 PM

Thanks guys! Ill will have to post a video once I get it all done :)/>

Can I combine a color from the left and right side?

#5 kazagistar

  • Members
  • 365 posts

Posted 03 June 2012 - 07:34 AM

I'm not sure what you mean, could you clarify? You mean sending stuff to different sides? I just noticed my code has a retarded bug (guess I should try it before I run it) and didn't have the directions, which would make it more clear (and actually work). You specify the direction you want to set, and what colors to set, with 0 being no signal. Check it out... http://computercraft...stone_%28API%29 http://computercraft...Color_%28API%29

#6 Huufalem

  • New Members
  • 15 posts

Posted 07 June 2012 - 03:05 AM

Okay well lets say I want to make a song with the noteblocks. One side can output up to 14 different notes.the song I wish to make uses 23 different noteblocks. Now what I wish to understand is if its possible to have the right side and left side play different notblocks at the same time to make a cord. Like to say if I set the piano on left side output and drums or bass on the right side if I could get both to hit notes at the same time.

#7 kazagistar

  • Members
  • 365 posts

Posted 07 June 2012 - 05:59 PM

Clearly my code example was not clear enough, so let me explain it in a bit more detail.
rs.setBundledOutput("back", colors.white+colors.red)
This is the command to set the output in a specific direction. "rs.setBundledOutput" is the name of the function, and what is between the brackets ( ) is the parameters, which determines what it does.

The "back" part (before the first comma) is the DIRECTION you want the computer to set output in. You only can control one direction at a time. For example, if I want to output to the left, I put in "left", right is "right", and same for down and up. When this command runs, the output signal in that direction will be the same until you call a different command, so you can do left and then right, and they will both turn on essentially instantly until you turn them off.

The colors.white+colors.red determines which colored outputs should be set to on. All the other ones on that side will be set to off. So, if you want to play 2 notes on the left, and 2 on the right, at the same time, you would go
rs.setBundledOutput("left", colors.white+colors.red)
rs.setBundledOutput("right", colors.red)
If you wish to turn off all the notes, instead of putting in a color, you type in 0 instead of numbers.

Now you cant just turn a wire on and off right after each other, or it wont work. You have to wait between commands for at least the length of a tick (which can vary). In this case, you can use the sleep command and wait for that amount of time in seconds before doing the next command.

Finally, if you do this
rs.setBundledOutput("back", colors.green)
os.sleep(1.5)
rs.setBundledOutput("back", colors.green)
it the note will have been on the whole time. You should shut them off between notes.

I hope that is comprehensive enough, I dont know if I can explain it any more simply.

#8 Huufalem

  • New Members
  • 15 posts

Posted 07 June 2012 - 08:56 PM

That is perfect, thanks bud.

#9 Bossman201

  • New Members
  • 92 posts

Posted 08 June 2012 - 01:01 AM

I think it would be a better idea to use wireless modems attached to computers, if you are using all pitches of noteblocks. It has scale-ability, the materials are cheap, and the computer doesn't have to be anywhere near the noteblocks (which would be expensive and time-consuming to do in Redpower).

#10 Huufalem

  • New Members
  • 15 posts

Posted 23 June 2012 - 09:29 PM

View PostBossman201, on 08 June 2012 - 01:01 AM, said:

I think it would be a better idea to use wireless modems attached to computers, if you are using all pitches of noteblocks. It has scale-ability, the materials are cheap, and the computer doesn't have to be anywhere near the noteblocks (which would be expensive and time-consuming to do in Redpower).

Care to explain? If there is a better or simpler way to im all ears :P/>

#11 kazagistar

  • Members
  • 365 posts

Posted 23 June 2012 - 10:44 PM

View PostHuufalem, on 23 June 2012 - 09:29 PM, said:

View PostBossman201, on 08 June 2012 - 01:01 AM, said:

I think it would be a better idea to use wireless modems attached to computers, if you are using all pitches of noteblocks. It has scale-ability, the materials are cheap, and the computer doesn't have to be anywhere near the noteblocks (which would be expensive and time-consuming to do in Redpower).

Care to explain? If there is a better or simpler way to im all ears :P/>

Imagine you have a number of computers. Some are "clients" and one is the "server". They are all hooked up to wireless routers. You can have all listening for messages from the server, and if the message they receive is for one of the notes they are connected to, they play it. So, a program like this:
rednet.open("top")
while true do
  senderID, message, distance = rednet.receive()
  if message == "on 1" then
    rs.setOutput("back", true)
  elseif message == "off 1" then
    rs.setOutput("back", false)
  elseif message == "on 2" then
    rs.setOutput("left", true)
  elseif message == "off 2 then
    rs.setOutput("left", false)
  end
end
In the above case, the computer has a wireless router on top of it, and a note block behind it and a different one to the left.

Now, you can have the server, or you can think of it as the conductor, broadcast messages, and the other computer will obey, as long as they are within wireless range.
-- assuming the router is on top, connect to it
rednet.open("top")
-- first, start by turning on power to a single note block
rednet.broadcast("on 1")
-- wait half a second
sleep(0.5)
-- now turn power off
rednet.broadcast("off 1")
-- and start playing another not at the same time
rednet.broadcast("on 2")
-- wait a little while for it to play
sleep(0.5)
-- and shut it off, and wait a while again
rednet.broadcast("off 2")
-- we can also play notes on other computers (assuming they are set up properly)
-- you can also play multiple at the same time
rednet.broadcast("on 3")
rednet.broadcast("on 5")
sleep(0.5)
rednet.broadcast("off 3")
rednet.broadcast("off 5")

This might not be the most efficient way of doing this or whatever, but it should be easy enough to get you started. Maybe I will cook something up tonight. If you have ever worked with midi, it is somewhat similar to that.

#12 Huufalem

  • New Members
  • 15 posts

Posted 24 June 2012 - 04:55 AM

Okay, You explained that perfectly. The only thing I fail to understand is how does the server send a message to different clients? Say I have a computer that plays G note for something then have another computer that plays F note.Could it be easier to set up a client with bundled cable and just have it play all the piano notes. then set anouther client the same way and have it play all the bass notes so the server tells the clients to send power to say red cable on server one looking something like

rednet.open("top")
while true do
  senderID, message, distance = rednet.receive()
  if message == "on 1" then
	rs.setBundledOutput(("back"), colors.red)

That could be wrong but that is what I am guessing it should look like. But I still fail to understand how I would get different clients. Would I just place the modem on a different side of the computer?

#13 kazagistar

  • Members
  • 365 posts

Posted 24 June 2012 - 01:50 PM

View PostHuufalem, on 24 June 2012 - 04:55 AM, said:

rednet.open("top")
while true do
  senderID, message, distance = rednet.receive()
  if message == "on 1" then
	rs.setBundledOutput(("back"), colors.red)

That could be wrong but that is what I am guessing it should look like. But I still fail to understand how I would get different clients. Would I just place the modem on a different side of the computer?

Close. First of all, some small syntax mistakes: whenever you indent, you need to end the block afterwards like this:
rednet.open("top")
while true do
  senderID, message, distance = rednet.receive()
  if message == "on 1" then
	rs.setBundledOutput("back", colors.red)
  end
end

Next, for the communication, I was using "rednet.broadcast(message)". This sends the same message to every single computer nearby. So, if you have 4 computers, ALL of them will get it. However, only one of the computers will have (if message == "on 1"), and so only one will do anything. The rest will get the message, see if it matches any of their if-statements (which it won't, because you will have set it to listen for different messages, like (if message == "on 6")), and then end up not doing anything.

The different sides for the note blocks are just so you can fit up to 4 note blocks per computer WITHOUT using redpower wire (because one side has to have the wireless and you cant attach to the front afaik). If you use the bundled cables, then you can get 16*5=80 notes on one computer, and two of which can play simultaneously, but with the disadvantage that controlling them is a little bit more confusing, because it involves flag-setting and binary. Plus, computercraft materials is actually really cheap, and you don't have to run wires everywhere. That is what bossman was saying. If you really want to use redpower, however, this was already covered at some length earlier in the thread, and the documentation might help too.

#14 tfoote

  • New Members
  • 134 posts
  • LocationSalt Lake, UT

Posted 24 June 2012 - 04:03 PM

View PostHuufalem, on 02 June 2012 - 03:49 AM, said:

Hello guys! Sorry to ask this but Im not to great with Lua coding, But I really love music, and after seeing a few videos on YouTube involving computrecraft and the ability to get rid of a lot of red stone and save a lot of space I decided to give it ago but My coding skills are lacking. Basically what I am looking to do is line up tuned note blocks and run cables to them then be able to have the computer play music. If someone could point me in the right direction as to achieving this goal that would be great! Thank you so much for your time and help!

The wiki will be your friend for eternity...

#15 Huufalem

  • New Members
  • 15 posts

Posted 24 June 2012 - 05:59 PM

SO to be sure, There are 115 different notes in minecraft.
115/4=29 computers (Clients)
+ 1 computer (server)

And in order to set up the computers Say computer one plays notes 1-4 so it would be on 1 back on 2 left on 3 right on 4 front. Then computer two would be on 5 back on 6 left on 7 right on 8 front? Is this correct?

#16 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 24 June 2012 - 06:18 PM

If you use redpower, you can do it just with 3 computers (2 clients, 1 server). And there's 125 notes (5 instruments, 25 notes each, 25 * 5 = 125).
The setup is pretty easy, and I'm almost done with the programs.

View Postkazagistar, on 24 June 2012 - 01:50 PM, said:

The different sides for the note blocks are just so you can fit up to 4 note blocks per computer WITHOUT using redpower wire (because one side has to have the wireless and you cant attach to the front afaik).
You can place the modem on the front, wich gives you another side for cable.

#17 Huufalem

  • New Members
  • 15 posts

Posted 24 June 2012 - 06:23 PM

View PostMysticT, on 24 June 2012 - 06:18 PM, said:

If you use redpower, you can do it just with 3 computers (2 clients, 1 server). And there's 125 notes (5 instruments, 25 notes each, 25 * 5 = 125).
The setup is pretty easy, and I'm almost done with the programs.

Ah okay, What do you mean your almost done with the programs? Are you releasing something easy to use :P/>

#18 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 24 June 2012 - 06:45 PM

View PostHuufalem, on 24 June 2012 - 06:23 PM, said:

Ah okay, What do you mean your almost done with the programs? Are you releasing something easy to use :)/>
Well, not sure if it's easy to use. It requires some configuration (setting the computer ids, the sides and colors of cables, etc.), but then you can just do something like:
nb.play(1, 10) -- play E on bass guitar
nb.play(5, 5) -- play B on piano
nb.play(4, { 0, 1, 2, 3, 4, 5 }) -- play some notes on bass drum
nb.play({ [2] = 2, [3] = { 6, 7, 8 } }) -- play notes on some instruments ([instrument] = <notes>)
And maybe I'll add some functions to play songs or something. But first I have to make it work :P/>

#19 kazagistar

  • Members
  • 365 posts

Posted 24 June 2012 - 06:53 PM

View PostMysticT, on 24 June 2012 - 06:45 PM, said:

View PostHuufalem, on 24 June 2012 - 06:23 PM, said:

Ah okay, What do you mean your almost done with the programs? Are you releasing something easy to use B)/>
Well, not sure if it's easy to use. It requires some configuration (setting the computer ids, the sides and colors of cables, etc.), but then you can just do something like:
nb.play(1, 10) -- play E on bass guitar
nb.play(5, 5) -- play B on piano
nb.play(4, { 0, 1, 2, 3, 4, 5 }) -- play some notes on bass drum
nb.play({ [2] = 2, [3] = { 6, 7, 8 } }) -- play notes on some instruments ([instrument] = <notes>)
And maybe I'll add some functions to play songs or something. But first I have to make it work :P/>
If I may toss out an idea, the easiest way to set this up would probably be something like midi. Assign each instrument a midi value, then have the main computer read a file, where the first line is how long each beat should be, and each line after is just a space deliminated list of which values to play at that beat. Setting it up might take a while, but writing the music would be a breeze.

Or just release your code, and I will just mod it in. :)/>

#20 Bossman201

  • New Members
  • 92 posts

Posted 25 June 2012 - 12:28 AM

Not sure why you would have him broadcast a 'note on' and 'note off' message. I would use this for the computers behind the noteblocks:
local note = "whatever note you want to play" --ex. E F G A B C D including sharps and flats
while true do
 id, message = os.pullEvent("rednet_message")
 if message == note then
  --handle redstone turn-on state
  --turn off redstone state
 end
end
Make the clients turn off redstone themselves, no reason to add extra network overhead. Note: This loop will run forever, that was intended.

As for the server, I would create a program that loads a table from a file into memory and then will broadcast each value in the table, using a timer to regulate your time BPM.
local iBPM = nil
if tSong[1] ~= "nil" then --Yes, this is a string because leaving a nil key in a table is poor programming etiquette, if you ask me.
 iBPM = 60 / tSong[1]
else
 print("Enter BPM: ")
 iBPM = 60 / tonumber(io.read())
end
tTimer = os.startTimer(iBPM)
A value could be added in the beginning of the table(example above) to indicate the BPM(beats per minute) of a song. My example breaks down BPM into BPS as timers use seconds, not minutes; Which is how I'd keep track of BPM.

Ideally you would want your timer to cycle at least 64(must be base 2, as that's how music works) times per measure, but that could cause too much overhead/exponential increases in memory required by the table as most entries will be 0 or "nil". Then I'd use a code like this to read/broadcast:
local terminate = false
local iBPM --previously declared, just here so you do not forget
local i = 3
local iEnd = tSong[2] --need some internal way to tell the end of the song, table length stored in entry 2. Just thought of a better way using #tSong but I'm not going to change the code a third time.
while terminate == false do
 evt, p1 = os.pullEvent("timer")
 if p1 == "tTimer" then
  tTimer = os.startTimer(iBPM)
 end
 rednet.broadcast(tSong[i])
 i = i + 1
 if i == iEnd + 1 then
  terminate = true
end
end

One problem with this is that your song can only play one note at a time so your solution is to use matrices or do some fancy parallel API shit. Which I don't know.

I planned to create a music editor, music player, and a turtle program to set up your player, based on what I've described above. But I couldn't find a good spot for it, so the project was scrapped (biggest reason why my projects fail is that they're never even started).

Sources: I know a bit about music too

EDIT: After editing for the 10999th time, and reformatting my code each time, I decided that someone needs to fix that about the forums. EDIT OF EDIT: There was something important I wanted to add to this post code-wise, but I can't remember what it was... Oh I remember now.

Since there are multiple instruments, it would be wise to add that into the entry before what actual note is played, here's an example table.
local tSong = {1 C, 2 E, 5 F, 2 G, 3 A, 4 B}
You won't have to change the client code that I posted in the beginning of the post, you must merely change the contents of variable 'note' to include which instrument it is.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users