Jump to content




[Lua] Noteblock Player


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

#1 TristanTroll

  • New Members
  • 15 posts

Posted 05 September 2012 - 09:50 PM

I want a program that when I type say "1" into the terminal, it will send a signal to the white wire through bundled cables. I recently asked for help on a song, but this guy basically told me nothing useful and said go script yourself an entire noteblock player with no additional help or thought that maybe I can't code to save my life. I tried MysticT's noteblock stuff and that didn't work, considering I am actually a multiplayer player, not a weird singleplayer guy who can access his rom/lua files and whatnot. I just need a noteblock player that works on a piano that goes lowest(left) to highest(right). I don't understand most of what this forum says, the tutorials on youtube are just "Hey guys look at what I made on minecraft today" which isn't helpful to people starting computercraft like me at all. And if I seem angry it's probably because I've spent the last week, hours a day looking for help and have received none that is useful...

#2 OmegaVest

  • Members
  • 436 posts

Posted 05 September 2012 - 10:09 PM

So, wait, you're just looking for a numerical keyboard player? Well, that's actually quite simple if you don't mind a little roundabouts.

For starters, every wire in a bundled cable has a numerical value to it. White is 1, Orange 2, Magenta 4, and so on up the binary ladder. (I think that's the right order). And by combining these numbers, we can get different ones. Like white+orange is 3, magenta and orange is 5, etc. By the way, using 'help colors' should give you the list of colors, and in the correct order. Just, it helps to know binary counting.

So, if you want to play note 1, you can type in '1', and then pass the input to a bundled output, like so.

input = tonumber(read())
rs.setBundledOutput(side, input)

Now it's really just a matter of understanding how to make each wire activate. The simplest way is to make an array that is nothing but the colors.

myColors = {}
myColors[1] = 1
myColors[2] = 2
myColors[3] = 4
myColors[4] = 8
myColors[5] = 16
myColors[6] = 32
myColors[7] = 64
myColors[8] = 128
myColors[9] = 256
myColors[10] = 512
myColors[11] = 1024
myColors[12] = 2048
myColors[13] = 4096
myColors[14] = 8192
myColors[15] = 16384
myColors[16] = 32768

And then apply what we have already learned to the array.

rs.setBundledOutput(side, myColors[input])


And that should get you started, if nothing else presently.

#3 TristanTroll

  • New Members
  • 15 posts

Posted 05 September 2012 - 10:32 PM

I'm not going to type rs.setBundledOutput(side, myColors[input]) For each and every note. What I need is a Noteblock Player I can use in multiplayer and I'm not the owner. Can you make one of those with a test song and a picture of your piano setup?

#4 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 05 September 2012 - 10:39 PM

Nobody wants to write a program for someone who won't code some of it themselves. I know I wouldn't want to do that, because you would never learn how to do it. OmegaVest already did some of the hard work for you.

#5 NIN3

  • New Members
  • 57 posts
  • LocationEverywhere.

Posted 05 September 2012 - 10:42 PM

View PostOmegaVest, on 05 September 2012 - 10:09 PM, said:



Now it's really just a matter of understanding how to make each wire activate. The simplest way is to make an array that is nothing but the colors.

myColors = {}
myColors[1] = 1
myColors[2] = 2
myColors[3] = 4
myColors[4] = 8
myColors[5] = 16
myColors[6] = 32
myColors[7] = 64
myColors[8] = 128
myColors[9] = 256
myColors[10] = 512
myColors[11] = 1024
myColors[12] = 2048
myColors[13] = 4096
myColors[14] = 8192
myColors[15] = 16384
myColors[16] = 32768

And then apply what we have already learned to the array.

rs.setBundledOutput(side, myColors[input])


And that should get you started, if nothing else presently.

Um, what about colors.(color)? Last time I checked, that worked just fine....

#6 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 05 September 2012 - 10:45 PM

View PostNIN3, on 05 September 2012 - 10:42 PM, said:

View PostOmegaVest, on 05 September 2012 - 10:09 PM, said:



Now it's really just a matter of understanding how to make each wire activate. The simplest way is to make an array that is nothing but the colors.

myColors = {}
myColors[1] = 1
myColors[2] = 2
myColors[3] = 4
myColors[4] = 8
myColors[5] = 16
myColors[6] = 32
myColors[7] = 64
myColors[8] = 128
myColors[9] = 256
myColors[10] = 512
myColors[11] = 1024
myColors[12] = 2048
myColors[13] = 4096
myColors[14] = 8192
myColors[15] = 16384
myColors[16] = 32768

And then apply what we have already learned to the array.

rs.setBundledOutput(side, myColors[input])


And that should get you started, if nothing else presently.

Um, what about colors.(color)? Last time I checked, that worked just fine....
Either works interchangeably. Numbers are just easier to work with, in my opinion.

#7 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 05 September 2012 - 10:49 PM

View PostNIN3, on 05 September 2012 - 10:42 PM, said:

Um, what about colors.(color)? Last time I checked, that worked just fine....

As well as colors[color].

I actually wrote a noteblock player a while ago that would read a list of outputs to induce. I had it play gangnam style but it didn't work out that well because of inaccurate timing.

However this would be much easier to do:
local tCommands = {
  [keys.q] = 'white';
  [keys.w] = 'red';
  [keys.e] = 'blue';
  --continue this with all of your colors, but in the order of how you would want your keys mapped in correspondence to the order of redstone outputs to the noteblocks.
}

while true do
  local _, k = os.pullEvent('key')

  if tCommands[k] then
    rs.setBundledOutput('back', 0) --replacing 'back' with whatever side you want, of course.
    rs.setBundledOutput('back', colors[tCommands[k]])
  end
end


#8 TristanTroll

  • New Members
  • 15 posts

Posted 05 September 2012 - 11:11 PM

Well maybe, just maybe cranium kid I said that I can't code to save my life, just maybe, and this is the ask a pro section and I needed help, so maybe I was just doing what I was supposed to, no reason to be mean to me. I don't understand what anyone here is doing because I see a crap ton of scattered scripts.

#9 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 05 September 2012 - 11:22 PM

You should probably look up a tutorial on CC lua first :D/>

http://computercraft...title=Tutorials

#10 TristanTroll

  • New Members
  • 15 posts

Posted 05 September 2012 - 11:40 PM

Nah I'd rather leave this to the Lua pros... besides making this doesn't just help me it helps everyone.

#11 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 06 September 2012 - 12:02 AM

I'm not trying to be mean. I just want to let you learn how to do it instead of demanding that someone do it for you. This is just a generally accepted "rule". See here that it's one of the stickies. We have tutorials in the forums for a reason. I'm sorry if I sound like I'm speaking for the entire community, but as a whole, I think everybody would like you to learn, instead of being spoonfed lines of code. We're not here to generate code FOR you, just to help you with your problems.

#12 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 06 September 2012 - 01:56 AM

View PostTristanTroll, on 05 September 2012 - 11:11 PM, said:

Well maybe, just maybe cranium kid I said that I can't code to save my life, just maybe, and this is the ask a pro section and I needed help, so maybe I was just doing what I was supposed to, no reason to be mean to me. I don't understand what anyone here is doing because I see a crap ton of scattered scripts.

View PostTristanTroll, on 05 September 2012 - 11:40 PM, said:

Nah I'd rather leave this to the Lua pros... besides making this doesn't just help me it helps everyone.

Lua is an easy language, trust me. (I know Lua, VB, C#, bits of perl and bits of C++, and lua is by far the easiest to get to grips with).
There's not much point trying to use computercraft unless you're willing to learn a bit about how to program. Part of the whole reason the mod was created was so people had a reason to learn how to program (As well as make something current programmers could use in minecraft to help simplify things).
Programming as a concept isn't hard, the hard bit is knowing what processes you have to perform to get your program to do what you want it to. For example you might know that you want to get a noteblock to play when someone presses a button while using the computer, but there are so many different ways you could go about it, it's sometimes hard to clear your head enough to think how you would go about doing it.


Also, it's pretty much an unwritten rule around here that you should never expect to come to the pros section and expect someone to provide you with a full running program, you should always assume they are just going to tell you where you are going wrong or suggest ways of doing things or things you may have missed (like the existence of a function that would help),which is one of the reasons Cranium was annoyed (also the fact that he's put a lot of effort into learning how to program in lua and had to overcome a lot of obstacles. Any programmer worth their salt has been through this struggle and gets annoyed when beginners want to take short cuts or people expect the code monkeys to deliver any program they ask for on a plate).

regardless, here is some code that will output data to a bundled cable at the back of a computer so you can play it like a keyboard(providing noteblocks are attached):
local side = "back" -- variable holding the side to send the data
term.clear() -- clear screen
term.setCursorPos(1, 1) --reset cursor
while true do
local e, k = os.pullEvent("key")
--you can choose the keys you want to set corresponding code to by using the keys constant (keys.keyname)
--k represents the key pressed. Test what key it is by using a key constant.
--here I demonstrate
--the w key sending output to the red wire,
--the r key sending output to the blue wire and
--the e key sending output to the white wire
--these can be changed by changing the key constant, the colour and
--more options can be added by adding more "else if k == keys.keyname then" blocks
--this is not the most efficient method, but if you are a beginner you dont need to worry about efficiency,
--just understanding what is going on

if k == keys.w then
redstone.setBundledOutput(side, colours.red) -- sets output to red
elseif k == keys.r then
redstone.setBundledOutput(side, colours.blue) -- sets output to blue
elseif k == keys.e then
redstone.setBundledOutput(side, colours.white) -- sets output to white
end
sleep()
redstone.setBundledOutput(side, 0) --cuts all output
end


#13 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 06 September 2012 - 02:16 AM

@Pharap, a long list of elseifs is a very unprofessional way of writing programs. Also, if the signal is cut off instantly after being sent, the note blocks won't play. They need to have the signal on for a certain amount of time, therefore they should be cut either before sending input, or have a sleep() after sending input.

#14 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 06 September 2012 - 02:38 AM

View PostKingdaro, on 06 September 2012 - 02:16 AM, said:

@Pharap, a long list of elseifs is a very unprofessional way of writing programs. Also, if the signal is cut off instantly after being sent, the note blocks won't play. They need to have the signal on for a certain amount of time, therefore they should be cut either before sending input, or have a sleep() after sending input.
I considered a sleep, but then forgot it at the last minute, that much I will admit to.
No it isn't brilliant programming but firstly, lua doesn't have a switch statement and secondly, it's no good bombarding a beginner with advanced efficient techniques if they aren't going to understand what it does. Showing things clearly is how people learn, not by showing them stuff that they need to understand a load of other junk first to understand.

Also I'd like to point out that quotes are much better than @ symbols, this isn't twitter or Java.

#15 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 06 September 2012 - 02:58 AM

View PostPharap, on 06 September 2012 - 02:38 AM, said:

No it isn't brilliant programming but firstly, lua doesn't have a switch statement and secondly, it's no good bombarding a beginner with advanced efficient techniques if they aren't going to understand what it does. Showing things clearly is how people learn, not by showing them stuff that they need to understand a load of other junk first to understand.
I read over what I said and I understand why you wrote your program the way you did.

View PostPharap, on 06 September 2012 - 02:38 AM, said:

Also I'd like to point out that quotes are much better than @ symbols, this isn't twitter or Java.
I used "@" because I didn't feel like cutting out parts of your huge post, haha.

#16 TristanTroll

  • New Members
  • 15 posts

Posted 06 September 2012 - 03:14 AM

Thanks Pharap, this isn't what I first asked for but I think it's way cooler. Thanks for not being a post farmer and taking time to give useful code for a super beginner like me :D/> The instructions were understandable for me.

#17 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 06 September 2012 - 03:15 AM

View PostKingdaro, on 06 September 2012 - 02:58 AM, said:

View PostPharap, on 06 September 2012 - 02:38 AM, said:

No it isn't brilliant programming but firstly, lua doesn't have a switch statement and secondly, it's no good bombarding a beginner with advanced efficient techniques if they aren't going to understand what it does. Showing things clearly is how people learn, not by showing them stuff that they need to understand a load of other junk first to understand.
I read over what I said and I understand why you wrote your program the way you did.

View PostPharap, on 06 September 2012 - 02:38 AM, said:

Also I'd like to point out that quotes are much better than @ symbols, this isn't twitter or Java.
I used "@" because I didn't feel like cutting out parts of your huge post, haha.
Cutting is fine, it's preferable to the @ thing, because if I'm only watching quotes, I won't know you've replied, or if I am watching both, I might just think your reply was a general one and not aimed at me (luckily my instincts told me otherwise).

I'm glad you understand why I did it though, I feel that a lot of experienced programmers start to forget what it was like when they were first starting out and tend to give people code that is wonderfully efficient but really hard for beginners, hence why I always try to explain code I give to people who are just starting out as well as making it require as least knowledge as possible. That way people learn whether they want to or not lol.

As for any mistakes I made, I put it up to tiredness (it's 04:14 where I am, UTC-0 is a late place to be right now)

#18 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 06 September 2012 - 03:25 AM

View PostTristanTroll, on 06 September 2012 - 03:14 AM, said:

Thanks Pharap, this isn't what I first asked for but I think it's way cooler. Thanks for not being a post farmer and taking time to give useful code for a super beginner like me :D/> The instructions were understandable for me.

You're welcome. I mostly saw 'noteblock player' and skimmed the rest, since it's a commonly asked for program.
As long as you tune it correctly, it should be fine. I'm not sure how many wire colours there are, but hopefully enough to match the 24 notes that noteblocks can produce, if not, just rig it up so there's a separate bundled cable on each side of the computer.

I'm glad you understood it, I've had an arduous journey in programming to get to where I am now, and since I only started a year ago, I still remember my first few weeks learning to program, so when I hear that someone is still new, I look back and say 'how would I explain this to myself back then' and thus try to make my code as simple as possible. Also I have a 'functionality over all' attitude, which basically means I worry more about if code works than if it's efficient.

Regardless of my ramblings, I'm glad you understand now, hopefully you'll get your noteblock keyboard working and soon move onto bigger and better things :P/>

#19 TristanTroll

  • New Members
  • 15 posts

Posted 06 September 2012 - 11:25 AM

My idea is to have a town bell tune from animal crossing with noteblocks that plays every hour if that's possible, so my idea is to type like sleep(3600) with a world anchor from tekkit by it so it always loads, but how do I make a program loop?

#20 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 06 September 2012 - 12:36 PM

View PostTristanTroll, on 06 September 2012 - 11:25 AM, said:

My idea is to have a town bell tune from animal crossing with noteblocks that plays every hour if that's possible, so my idea is to type like sleep(3600) with a world anchor from tekkit by it so it always loads, but how do I make a program loop?
To sleep every hour and then play the tune every hour, you would make a while loop.
while true do
  playSong() -- replace this with your song playing code.
  sleep(3600)
end

However if you wanted the song to sound at all like a real song, you would have a waiting period in the song that would offset the hour-long cycle. Another way to do this would be to make a while loop using the parallel API to play both the timer and the song at the same time.
local function songTimer()
  sleep(3600)
end

while true do
  parallel.waitForAll(songTimer, playSong)
end

The parallel API waits for both the song to complete, and for the timer to cycle 3600 seconds. Hopefully this makes sense.

View PostPharap, on 06 September 2012 - 03:25 AM, said:

Also I have a 'functionality over all' attitude, which basically means I worry more about if code works than if it's efficient.
Aha, you and I are on two completely different spectrums; I can't leave the edit program unless my code is the shortest it can be while being readable XD





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users