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

Love music but not too good with code...

Started by Huufalem, 02 June 2012 - 02:49 AM
Huufalem #1
Posted 02 June 2012 - 04: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!
zapwow #2
Posted 02 June 2012 - 05: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.
kazagistar #3
Posted 02 June 2012 - 08: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.
Huufalem #4
Posted 02 June 2012 - 08: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?
kazagistar #5
Posted 03 June 2012 - 08: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.info/wiki/index.php?title=Redstone_%28API%29 http://computercraft.info/wiki/index.php?title=Color_%28API%29
Huufalem #6
Posted 07 June 2012 - 04: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.
kazagistar #7
Posted 07 June 2012 - 06: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.
Huufalem #8
Posted 07 June 2012 - 09:56 PM
That is perfect, thanks bud.
Bossman201 #9
Posted 08 June 2012 - 02: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).
Huufalem #10
Posted 23 June 2012 - 10:29 PM
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/>/>
kazagistar #11
Posted 23 June 2012 - 11:44 PM
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.
Huufalem #12
Posted 24 June 2012 - 05: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?
kazagistar #13
Posted 24 June 2012 - 02:50 PM

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.
tfoote #14
Posted 24 June 2012 - 05:03 PM
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…
Huufalem #15
Posted 24 June 2012 - 06: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?
MysticT #16
Posted 24 June 2012 - 07: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.

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.
Huufalem #17
Posted 24 June 2012 - 07:23 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.

Ah okay, What do you mean your almost done with the programs? Are you releasing something easy to use :P/>/>
MysticT #18
Posted 24 June 2012 - 07:45 PM
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/>/>
kazagistar #19
Posted 24 June 2012 - 07:53 PM
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. :)/>/>
Bossman201 #20
Posted 25 June 2012 - 01: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.
kazagistar #21
Posted 25 June 2012 - 01:34 AM
I was just trying to keep it simple I guess, but everyone has their own definition of what that means. :P/>/>
MysticT #22
Posted 25 June 2012 - 01:44 AM

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.
???
Seting a value to nil in a table is like removing it, so it's better to do it, since it would free the space.
Bossman201 #23
Posted 25 June 2012 - 01:55 AM

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.
???
Seting a value to nil in a table is like removing it, so it's better to do it, since it would free the space.
The guess the one coding it poorly was me lol. Anyway that is just a block of example code I wrote, in a real music editor there would always be a BPM or else the song could not play.

EDIT: To all, if you plan on using that code I posted for the love of god message me first so I can spend some actual time writing functional code. I'm pretty sure there are plenty of logic errors in that code, as I rushed writing it because I get excited.
Huufalem #24
Posted 25 June 2012 - 05:12 PM
Okay so whats wrong here it keeps saying I need end but I have them &amp;gt;.&amp;lt;

Error:

bios:206: [string "music01":56: 'end' expected (to close 'if' at line 46)

This is what I have for each client but thier number is changed to be 1-25 dirt 101-125 is wood. so on and so on. Hopefully I have it somewhat right lol.

rednet.open("top")
while true do
  senderID, message, distance = rednet.receive()
  if message == "on 1" then
	rs.setBundledOutput(("left"), colors.white)
  if message == "on 2" then
	rs.setBundledOutput(("left"), colors.orange)
  if message == "on 3" then
	rs.setBundledOutput(("left"), colors.magenta)
  if message == "on 4" then
	rs.setBundledOutput(("left"), colors.lightBlue)
  if message == "on 5" then
	rs.setBundledOutput(("left"), colors.yellow)
  if message == "on 6" then
	rs.setBundledOutput(("left"), colors.lime)
  if message == "on 7" then
	rs.setBundledOutput(("left"), colors.pink)
  if message == "on 8" then
	rs.setBundledOutput(("left"), colors.gray)
  if message == "on 9" then
	rs.setBundledOutput(("left"), colors.lightGray)
  if message == "on 10" then
	rs.setBundledOutput(("left"), colors.cyan)
  if message == "on 11" then
	rs.setBundledOutput(("left"), colors.purple)
  if message == "on 12" then
	rs.setBundledOutput(("left"), colors.blue)
  if message == "on 13" then
	rs.setBundledOutput(("left"), colors.brown)
  if message == "on 14" then
	rs.setBundledOutput(("left"), colors.green)
  if message == "on 15" then
	rs.setBundledOutput(("left"), colors.red)
  if message == "on 16" then
	rs.setBundledOutput(("left"), colors.black)
  if message == "on 17" then
	rs.setBundledOutput(("right"), colors.white)
  if message == "on 18" then
	rs.setBundledOutput(("right"), colors.orange)
  if message == "on 19" then
	rs.setBundledOutput(("right"), colors.magenta)
  if message == "on 20" then
	rs.setBundledOutput(("right"), colors.lightBlue)
  if message == "on 21" then
	rs.setBundledOutput(("right"), colors.yellow)
  if message == "on 22" then
	rs.setBundledOutput(("right"), colors.lime)
  if message == "on 23" then
	rs.setBundledOutput(("right"), colors.pink)
  if message == "on 24" then
	rs.setBundledOutput(("right"), colors.gray)
  if message == "on 25" then
	rs.setBundledOutput(("right"), colors.lightGray)
    end
  end
end

kazagistar #25
Posted 25 June 2012 - 06:03 PM
All the ifs after the first should be elseifs, and delete one of the end statements.
Huufalem #26
Posted 25 June 2012 - 06:14 PM
Do I need to set the elseif to off for everything?


rednet.open("top")
while true do
senderID, message, distance = rednet.receive()
if message == "on 1" then
  rs.setoutput(("left"), colors.white,true)
elseif message == "off 1" then
  rs.setoutput(("left"), colors.white,false)
if message == "on 2" then
  rs.setoutput(("left"), colors.orange,true)
elseif message == "off 2" then
  rs.setoutput(("left"), colors.orange,false)
   end
end
Lyqyd #27
Posted 25 June 2012 - 07:10 PM
Do I need to set the elseif to off for everything?


rednet.open("top")
while true do
senderID, message, distance = rednet.receive()
if message == "on 1" then
  rs.setoutput(("left"), colors.white,true)
elseif message == "off 1" then
  rs.setoutput(("left"), colors.white,false)
if message == "on 2" then
  rs.setoutput(("left"), colors.orange,true)
elseif message == "off 2" then
  rs.setoutput(("left"), colors.orange,false)
   end
end

No. He literally told you everything you had to do to the code you had previously posted:

Change all but the first if to elseif.
Remove one of the three end statements.

This includes handling the off messages too. One if, everything else an elseif. One end to match the if, one end to match the while.
kazagistar #28
Posted 25 June 2012 - 07:13 PM
No, EVERY if. Only the first one should be an actual if. There is nothing magical about the off vs the on statements.


if <condition> then <stuff> end
if <condition> then <stuff> else <stuff> end
if <condition> then <stuff> elseif <condition> then <stuff> end
if <condition> then <stuff> elseif <condition> then <stuff> else <stuff> end
if <condition> then <stuff> elseif <condition> then <stuff> elseif <condition> then <stuff> elseif <condition> then <stuff> else <stuff> end

Lua does not actually care about or read your indentation; that is only done for human readers. What matters is that the order follows the correct pattern.

In theory, you could just do something like this, which would be the same thing, but would be WAY more annoying to write and debug, so we have the compound elseif statement so we don't have to write a billion end statements. I hope that helps.

if <condition> then
  <stuff>
else
  if <condition> then
    <stuff>
  else
    if <condition> then
	  <stuff>
    else
	  if <condition> then
	    <stuff>
	  else
	    <stuff>
	  end
    end
  end
end
Huufalem #29
Posted 25 June 2012 - 08:01 PM
Would it being typed like this stop it from playing multiple notes at the same time?

Client:

rednet.open("top")
while true do
senderID, message, distance = rednet.receive()
if message == "on 1" then rs.setBundledOutput(("left"), colors.white)
elseif message == "on 2" then rs.setBundledOutput(("left"), colors.orange)
elseif message == "on 3" then rs.setBundledOutput(("left"), colors.magenta)
elseif message == "on 4" then rs.setBundledOutput(("left"), colors.lightBlue)
elseif message == "on 5" then rs.setBundledOutput(("left"), colors.yellow)
elseif message == "on 6" then rs.setBundledOutput(("left"), colors.lime)
elseif message == "on 7" then rs.setBundledOutput(("left"), colors.pink)
elseif message == "on 8" then rs.setBundledOutput(("left"), colors.gray)
elseif message == "on 9" then rs.setBundledOutput(("left"), colors.lightGray)
elseif message == "on 10" then rs.setBundledOutput(("left"), colors.cyan)
elseif message == "on 11" then rs.setBundledOutput(("left"), colors.purple)
elseif message == "on 12" then rs.setBundledOutput(("left"), colors.blue)
elseif message == "on 13" then rs.setBundledOutput(("left"), colors.brown)
elseif message == "on 14" then rs.setBundledOutput(("left"), colors.green)
elseif message == "on 15" then rs.setBundledOutput(("left"), colors.red)
elseif message == "on 16" then rs.setBundledOutput(("left"), colors.black)
elseif message == "on 17" then rs.setBundledOutput(("right"), colors.white)
elseif message == "on 18" then rs.setBundledOutput(("right"), colors.orange)
elseif message == "on 19" then rs.setBundledOutput(("right"), colors.magenta)
elseif message == "on 20" then rs.setBundledOutput(("right"), colors.lightBlue)
elseif message == "on 21" then rs.setBundledOutput(("right"), colors.yellow)
elseif message == "on 22" then rs.setBundledOutput(("right"), colors.lime)
elseif message == "on 23" then rs.setBundledOutput(("right"), colors.pink)
elseif message == "on 24" then rs.setBundledOutput(("right"), colors.gray)
elseif message == "on 25" then rs.setBundledOutput(("right"), colors.lightGray)
end
end

It works flawlessly just can not get it to play two notes at the same time when I use this to test

Server:

rednet.open("top")
rednet.broadcast ("on 1")
rednet.broadcast ("on 2")
rednet.broadcast ("on 3")
rednet.broadcast ("on 4")
rednet.broadcast ("on 5")
rednet.broadcast ("on 6")
rednet.broadcast ("on 7")
rednet.broadcast ("on 8")
rednet.broadcast ("on 9")
rednet.broadcast ("on 10")
rednet.broadcast ("on 11")
rednet.broadcast ("on 12")
rednet.broadcast ("on 13")
rednet.broadcast ("on 14")
rednet.broadcast ("on 15")
rednet.broadcast ("on 16")
rednet.broadcast ("on 17")
rednet.broadcast ("on 18")
rednet.broadcast ("on 19")
rednet.broadcast ("on 20")
rednet.broadcast ("on 21")
rednet.broadcast ("on 22")
rednet.broadcast ("on 23")
rednet.broadcast ("on 24")
rednet.broadcast ("on 25")
sleep(0.5)
rednet.broadcast ("on 25")
sleep(0.5)
rednet.broadcast ("on 24")
sleep(0.5)
rednet.broadcast ("on 25")
sleep(0.5)
rednet.broadcast ("on 24")
sleep(0.5)
rednet.broadcast ("on 12")
rednet.broadcast ("on 13")
sleep(0.5)
rednet.broadcast ("on 24")
rednet.broadcast ("on 25")
Bossman201 #30
Posted 26 June 2012 - 02:48 AM
Yes, that code will only play one note at a time.

If you wanted to play multiple notes, you should use this code:
paralell.waitForAll(rednet.broadcast(note1), rednet.broadcast(note2))
You must change your code so that the server broadcasts the note you want played instead of just changing state on some redstone. The client can then manage changing redstone states itself. No reason to add overhead.

Also, your current client code doesn't turn off any redstone states. Meaning you can only play each note once unless the system is reset.
Huufalem #31
Posted 26 June 2012 - 04:31 AM
Yes, that code will only play one note at a time.

If you wanted to play multiple notes, you should use this code:
paralell.waitForAll(rednet.broadcast(note1), rednet.broadcast(note2))
You must change your code so that the server broadcasts the note you want played instead of just changing state on some redstone. The client can then manage changing redstone states itself. No reason to add overhead.

Also, your current client code doesn't turn off any redstone states. Meaning you can only play each note once unless the system is reset.

What would I need to add to my client to have it clear the red stone states after use?

I see your

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

but that confuses me to no end as to how it runs. Sorry you really have to spoon feed me here lol.
Bossman201 #32
Posted 26 June 2012 - 08:49 PM
I haven't used the Redstone API at all in lua before, but looking at the methods posted here, it looks like this code should be sufficient for redstone wiring or single red alloy wiring.

local note = "whatever note you want to play" --ex. E F G A B C D including sharps and flats.
while true do --Loop runs forever.
id, message = os.pullEvent("rednet_message") --os.pullEvent "pulls" every event that happens during program execution; with the filter "rednet_message", only rednet message events will be pulled.
 if message == note then --Checks to see if the broadcasted note is the note this computer is responsible for.
  rs.setOutput("back", true) --Turns on redstone.
  sleep(0.1) --Might be necessary, might not.
  rs.setOutput("back", false) --Turns off redstone.
 end
end

Test the programs without sleep() but they might be necessary.
Huufalem #33
Posted 26 June 2012 - 09:11 PM
Thanks Bossman, Ill give it a go and see what I run into.
Huufalem #34
Posted 27 June 2012 - 01:25 AM

rednet.open("top")
paralell.waitForAll(rednet.broadcast(note1))
sleep(0.05)
paralell.waitForAll(rednet.broadcast(note2))
sleep(0.05)
paralell.waitForAll(rednet.broadcast(note1), rednet.broadcast(note2))

Gives me

testing:2: attempt to index ? (a nil value)
Lyqyd #35
Posted 27 June 2012 - 01:29 AM
Parallel should probably be spelled correctly. That may help.
MysticT #36
Posted 27 June 2012 - 01:34 AM
Yes, but that's not the way to use parallel. And parallel won't help with this.
To be able to play 2 notes at the same time you need to set both colors on, like:

local c = colors.combine(colors.white, colors.black)
rs.setBundledOutput("back", c)
Lyqyd #37
Posted 27 June 2012 - 01:52 AM
And a better way to receive multiple-note information via rednet would be to format a string with as many notes as you like in it:


<left:white><right:orange>

And then when the note controller receives that, it runs it through something a bit like:


for side, note in string.gmatch(packet, "<(%a+):P/>/>%a+)>") do
    noteTable[side] = colors.combine(noteTable[side], colors[note])
end
for side, value in pairs(noteTable) do
    redstone.setBundledOutput(side, value)
end
sleep(0.2)
for _,side in pairs(redstone.getSides()) do
    redstone.setBundledOutput(side, 0) --not sure about this value here.  Maybe false instead?  Haven't worked with bundled cable.
    noteTable[side] = nil
end
MysticT #38
Posted 27 June 2012 - 01:57 AM
Or even better would be if the server knows the colors to use for each note, it sends the combined colors directly:
Server:

local c = colors.combine(colors.white, colors.blue, colors.red)
rednet.send(id, tostring(c))
Client:

local id, msg = rednet.receive()
if id == serverID then
  local c = tonumber(msg)
  if c then
    rs.setBundledOutput("back", c)
  end
end
Lyqyd #39
Posted 27 June 2012 - 04:39 AM
Or even better would be if the server knows the colors to use for each note, it sends the combined colors directly:
Server:

local c = colors.combine(colors.white, colors.blue, colors.red)
rednet.send(id, tostring(c))
Client:

local id, msg = rednet.receive()
if id == serverID then
  local c = tonumber(msg)
  if c then
	rs.setBundledOutput("back", c)
  end
end

True enough, color combination could be done at either end. Distinguishing between different sides is still necessary for the system they laid out, though. I guess I was envisioning a system where lines of simultaneous notes and a delay were read out of a file and sent to the note controllers, such that multiple songs could be written and played. Combining the colors makes as much sense at one end as it does at the other, to me.
Huufalem #40
Posted 27 June 2012 - 06:09 AM
Or even better would be if the server knows the colors to use for each note, it sends the combined colors directly:
Server:

local c = colors.combine(colors.white, colors.blue, colors.red)
rednet.send(id, tostring(c))
Client:

local id, msg = rednet.receive()
if id == serverID then
  local c = tonumber(msg)
  if c then
	rs.setBundledOutput("back", c)
  end
end

True enough, color combination could be done at either end. Distinguishing between different sides is still necessary for the system they laid out, though. I guess I was envisioning a system where lines of simultaneous notes and a delay were read out of a file and sent to the note controllers, such that multiple songs could be written and played. Combining the colors makes as much sense at one end as it does at the other, to me.

But in that method you will have to write a lot to get the combined colors working. Let me explain my set up and code can be done based on it because it seems as though that code requires a lot of work but still with little payout.
and if I use the wireless computer method with 1 computer and 1 noteblock I still need to go and turn on every computer to have them listen.

Basally I have
5 computers; 1 for Dirt, Wood, Sand, Cobblestone, Glass.
and 1 Server to control them.
Now on the left side of the client computers is note blocks 1-16 then on the right I have note blocks 17-25.
My main goal is to have a system that I can play multiple songs on with out having an issue

My goal is very similar to http://www.youtube.com/watch?v=0fi4gvDXBX4&amp;feature=related
MysticT #41
Posted 03 July 2012 - 07:01 PM
Ok, just finished the api and programs, got get them on my post.