Omnimaga
Calculator Community => TI Calculators => Lua => Topic started by: Jim Bauwens on July 25, 2011, 09:09:47 am
-
Here is a little piece of code to play Happy birthday with Lua from your nspire:
TONE_LOW = "\001"
TONE_HIGH = "\255"
happy_birthday = {262,0.5,262,0.5,294,1,262,1,349,1,330,2,262,0.5,262,0.5,294,1,262,1,392,1,349,2,262,0.5,262,0.5,523,1,440,1,349,1,330,1,294,3,466,1,466,1,440,1,349,1,392,1,349,2}
function freq_key(key)
--return 440 * math.pow(2, (key-49)/12)
return key
end
function keytone(key)
local am = 57600/freq_key(key)
return TONE_HIGH:rep(am) .. TONE_LOW:rep(am)
end
function key_am(key, ammount)
return keytone(key):rep(ammount)
end
function key_time(key, time)
return key_am(key, freq_key(key)*time)
end
function play()
local out = ""
for i=1,#happy_birthday,2 do
out = out..key_time(happy_birthday[i]*5, happy_birthday[i+1]/10)
end
print(out)
end
function on.timer()
play()
timer.stop()
end
function on.paint(gc)
timer.start(1)
end
However, the quality is not so good, because I can't send a byte that contains only 0's, and there is a stop bit at the end of each byte I send (I think). But its ok, you can have fun with it :p
I currently connect a directly a little speaker between the TX and the GND pins of my nspire, but I do not know if this is so safe. Should I add an diode in between?
-
Do I need headphones to listen to this? If so how do I plug them in?
-
You need to connect the speaker (or headphone) between the TX and the GND pins on the dock port.
This picture shows how to connect the speaker (Credit goes to TI BANK):
(http://i43.servimg.com/u/f43/13/23/13/53/nspire10.png)
Edit: better picture, thanks Critor!
-
So do I just tape it down or what?
EDIT:could you post a download of a tns file with this in it, please?
-
Well, you might be able to squeeze 2 little cables between the connector strip and the thing that covers it. Tape will also work, but you need to be sure that its on the right spot.
-
Eh, looks like there is a bug in the first top post that make the quality worse ...
* jimbauwens goes change the top post
Edit, sorry for the double post!
Edit2: video:
-
Wow this is awesome. Could you create some kind of a library so we could use a function that lets us put in a note length and hertz amount to play music?
-
Its already a little library :p
Its reads the Freq and time from the array happy_birthday and plays it :)
-
I'll have to try and see If I could write a song with this. thanks for your great work
-
No problem :)
Also, beware of long stuff music, the nspire doesn't like it and gives a memory error.
Btw, if you change
function freq_key(key)
--return 440 * math.pow(2, (key-49)/12)
return key
end
to
function freq_key(key)
return 440 * math.pow(2, (key-49)/12)
--return key
end
It will read the first values as key notes (like 1-88), so that might be more easy. Just experiment a bit, its fun :p
-
Does the sound work like on 8x calcs, where you only have a low and a high state? And if so, are you changing the state on a per bit or per byte resolution?
-
Wow, I am impressed. Very good job on this. I was really curious to see how you managed to output sound, and it is extremely hacky, I love it.
-
t0xic, the problem is that I don't have full controlling of what I send. I'm using the print() function to send the data to the serial port (so at byte level). 001 is low, and 255 is high. So my wave form isn't perfect at all, but at least you can hear the tones :)
@thepenguin77, thanks!
-
by the end of the next year it should be possible to play gameboy color games with color and sound on the CX :P
-
t0xic, the problem is that I don't have full controlling of what I send. I'm using the print() function to send the data to the serial port (so at byte level). 001 is low, and 255 is high. So my wave form isn't perfect at all, but at least you can hear the tones :)
@thepenguin77, thanks!
The quality would improve if you change state on the bit level, by also sending numbers between 1 and 255. So then the highest frequency tone you could play would be represented by 10101010 = 170.
-
I can do that, but that would be no use it it only will work for very high tones. I don't have full control over the port, so its not easy. I can't even send one empty byte (000).
-
Congrats, this is awesome :)
Now I should port my Music Composer (http://ti.bank.free.fr/index.php?mod=archives&ac=voir&id=986) (for 83+/84) to Nspire Lua :P
-
Thanks adriweb!
I just realized I have a little timing issue in my code. It's not big though, but ill fix it as soon as I get on a good comp :)
-
I have to try that as soon as possible ! :D
I imagine I could edit melodies on calc with oclua ? :)
-
I seriously think this deserves a News post ;)
-
Loulou, only if you type the whole thing on your calculator :)
-
That's why I'm planning to make a Music composer :)
it will be basic (about the features, the difficulty :P, 'coz it will obviously be Lua) but will probably be able to have some rhythm, support the whole range possible, able to save/load to external strings (var.store) etc.
Also, I'll probably (as soon as I have everything I need) be soon making (more like building) some hardware piece fir the Nspire that will be über-cool, but I can't tell you more about that, it's a secret :P
-
Whooo, you are making my curious :p
Well, good luck with it :)
-
Thanks, but it might take some time :P
-
I have just tried on my CX. But I Have only a very high tone.. (I launch the script with oclua)
Has anyone tried with a CX? :)
-
Are you sure everything is correct? Also, you can try if changing \255 in the beginning to \127 helps.
-
I'm wondering too.. But I've already used this speaker to make sound with my TI 89. So I don't think that the problem is here.
Thank you I'll see if that change anything. :)
-
Oh, and congratulations with your new CX :)
Edit: or wait, I'm confusing you with someone else
-
ok ^^
I have changed the value to 127 and it works very good now ! :D
That sounds very good finally. :)
I think I've understood how you code your melody, so I'll try to create a little music ! ;)
-
Well, I still need to fix the timing part, as I make a wrong calculation there. But most of the things work, so its not really a problem. Anyway, I'm glad it works :)
Have fun!
-
I have played "Come as you are" of Nirvana. ;) I'll post the list here soon.
And I'm playing with a code to generate random frequency list. That's very amusing. ^^
-
Very nice!
I'm starting to think of another method, one that allows me to update the screen sometimes.
That way I might be able to make a little music player.
-
Ok, my Music Composer is in very good shape, I made it during my Salt-Lake-City -> Miami travel :P
it's 501 lines of code right now and there's still a lot to be done.
The main stuff work but need to be greatly improved for a release.
So, I just wanted to know that I export the "music" with my own format, and then I'd have to write a little converter (which will be included in the program, don't worry to convert to jimbauwens' format.
Here's my syntax :
-- final string syntax : table of "[Note][Octave][Length][Alteration]", for example : "C342"
-- with C the note, 3 for the 3rd octave (middle of a regular piano keyboard I think), 4 for the eight-note length
-- (whole note is 1, half is 2, fourth is 3, sixteenth is 5), 2 for sharp (1 is flat, 0 is natural)
Screenshots :
(http://i.imgur.com/IfB8l.jpg)
(http://i.imgur.com/Rt7Ra.jpg)
(http://i.imgur.com/t2wUd.jpg)
btw, quick bonus for you guys, just so you're scared :
here's a non-optimized-beacause-i'm-lazy-right-now thing I have in the code :
if notelen > 3 then
gc:drawArc(x-8*scale+test(y>90)*12*scale,y-test(y>90)*30*scale+test(y<90)*10*scale,scale*18,scale*20,0,90-180*test(y<90))
if notelen == 5 then
gc:drawArc(x-8*scale+test(y>90)*12*scale,y-test(y>90)*20*scale,scale*18,scale*20,0,90-180*test(y<90))
end
end
%)
-
Wow, nice!
And please don't make it convert to my format,as it was just a basic example and needs lots of work if you want to use it in real life applications.
-
Well my Music Composer would be used so games programmers for example can quickly generate music for their game and include the final "encoded music string" in their application and use your play() routine to play sounds in their apps.
Maybe you can yourself adapt your program/routine to understand my format ?
(just asking :P)
-
Sure, but I don't think this would be useful in games, as you can't do anything while its playing.
I could try to implant it a bit different, but I don't know if the way I have in my mind will work good.
-
Sure, but I don't think this would be useful in games, as you can't do anything while its playing.
I could try to implant it a bit different, but I don't know if the way I have in my mind will work good.
even with a coroutine task ? (I dont know, just asking)
I'm not sure but I'll ask TI directly about that, as it could be very intersting.... but shhh I can't talk about all that (or Can I ?, since I've never talked about sound with Lua to TI lol)
-
No, not even with that :/
It would be cool if TI added something specially for sound, but I doubt that will happen.
But you can always try :D
-
Well, you know, it's not necessarily a problem if the sound has to be played on itself and nothing else can be done at the same time, if it's a short sound, it's ok for fights starts/noises/endings, and for Credits, game over FX etc.
-
Well, we have a little problem.
print adds \r\n, and that itself makes a little weird noise.
Ah, but I'm sure it will be usefull for something.
-
Wow ! Looks very nice !! :D
-
Those screenshots look quite excellent! Amazing work! :)
-
Thanks ;)
BTW, does somebody hère wanna be an alpha/bera tester ? I Will need some :P
-
I think I could do that.. :)
-
All right, I think I'll just put my stuff on GitHub in a few days so it'll be easier to manage
-
Seems nice, I wonder how does sound on the Nspire sounds like...
Also does it supports multiple sound channels at once?
-
not for now, until I (or anybody else actually) find how to get better sound off the nspire port...
-
I'd love to test it out, but I can't do it with sound. D:
-
well for now Jimbauwens' sound routines are not in my program... what's in right now is the editor and the "encryption" of the notes.
And the editor is not finished at all, even if it can produces some basic things...
Once I get all Jim's routines etc. (I'll try the hardware modifications on my calc soon), I'll post an beta version.
But before, an alpha version will be uploaded to github so people can try the editor and the encoded output it gives.
-
DJ, you can hear a bit sound in the video I posted :)
Sadly though its only one channel.
Adriweb, I'd like to be a beta tester :)
BTW, I have ideas to improve sound, just need to try it.
-
sounds great ^^
-
Adriweb, I'd like to alpha/beta test too :)
-
All right, as promised, here's the GitHub repository.
It's all open, so please do whatever you want to do with this (comments (+ or -), improvements etc. :) )
-
Can we news/publish on TI-Bank, or do you want us to wait?