Omnimaga

Calculator Community => TI Calculators => Lua => Topic started by: Jim Bauwens on July 25, 2011, 09:09:47 am

Title: Sound with nspire Lua
Post 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:

Code: [Select]
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?
Title: Re: Sound with nspire Lua
Post by: ruler501 on July 25, 2011, 09:12:24 am
Do I need headphones to listen to this? If so how do I plug them in?
Title: Re: Sound with nspire Lua
Post by: Jim Bauwens on July 25, 2011, 09:15:36 am
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!
Title: Re: Sound with nspire Lua
Post by: ruler501 on July 25, 2011, 09:18:48 am
So do I just tape it down or what?

EDIT:could you post a download of a tns file with this in it, please?
Title: Re: Sound with nspire Lua
Post by: Jim Bauwens on July 25, 2011, 09:21:36 am
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.
Title: Re: Sound with nspire Lua
Post by: Jim Bauwens on July 25, 2011, 09:40:42 am
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:
Title: Re: Sound with nspire Lua
Post by: ruler501 on July 25, 2011, 04:03:00 pm
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?
Title: Re: Sound with nspire Lua
Post by: Jim Bauwens on July 25, 2011, 04:05:44 pm
Its already a little library :p

Its reads the Freq and time from the array happy_birthday and plays it :)
Title: Re: Sound with nspire Lua
Post by: ruler501 on July 25, 2011, 04:06:56 pm
I'll have to try and see If I could write a song with this. thanks for your great work
Title: Re: Sound with nspire Lua
Post by: Jim Bauwens on July 25, 2011, 04:10:39 pm
No problem :)
Also, beware of long stuff music, the nspire doesn't like it and gives a memory error.

Btw, if you change
Code: [Select]
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
Title: Re: Sound with nspire Lua
Post by: fb39ca4 on July 26, 2011, 12:06:24 am
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?
Title: Re: Sound with nspire Lua
Post by: thepenguin77 on July 26, 2011, 12:09:37 am
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.
Title: Re: Sound with nspire Lua
Post by: Jim Bauwens on July 26, 2011, 01:33:09 am
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!
Title: Re: Sound with nspire Lua
Post by: renatose on July 26, 2011, 11:57:21 am
by the end of the next  year it should be possible to play gameboy color games with color and sound on the CX :P
Title: Re: Sound with nspire Lua
Post by: fb39ca4 on July 26, 2011, 12:04:00 pm
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.
Title: Re: Sound with nspire Lua
Post by: Jim Bauwens on July 26, 2011, 12:09:34 pm
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).
Title: Re: Sound with nspire Lua
Post by: Adriweb on July 26, 2011, 12:14:11 pm
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
Title: Re: Sound with nspire Lua
Post by: Jim Bauwens on July 26, 2011, 12:21:48 pm
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 :)
Title: Re: Sound with nspire Lua
Post by: Loulou 54 on July 27, 2011, 06:27:19 am
I have to try that as soon as possible ! :D
I imagine I could edit melodies on calc with oclua ? :)
Title: Re: Sound with nspire Lua
Post by: Munchor on July 27, 2011, 06:35:38 am
I seriously think this deserves a News post ;)
Title: Re: Sound with nspire Lua
Post by: Jim Bauwens on July 27, 2011, 09:51:33 am
Loulou, only if you type the whole thing on your calculator :)
Title: Re: Sound with nspire Lua
Post by: Adriweb on July 27, 2011, 11:50:26 am
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
Title: Re: Sound with nspire Lua
Post by: Jim Bauwens on July 27, 2011, 03:26:35 pm
Whooo, you are making my curious :p
Well, good luck with it :)
Title: Re: Sound with nspire Lua
Post by: Adriweb on July 28, 2011, 01:11:30 am
Thanks, but it might take some time :P
Title: Re: Sound with nspire Lua
Post by: Loulou 54 on July 29, 2011, 09:26:46 am
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?  :)
Title: Re: Sound with nspire Lua
Post by: Jim Bauwens on July 29, 2011, 09:29:41 am
Are you sure everything is correct? Also, you can try if changing \255 in the beginning to \127 helps.
Title: Re: Sound with nspire Lua
Post by: Loulou 54 on July 29, 2011, 09:40:50 am
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. :)
Title: Re: Sound with nspire Lua
Post by: Jim Bauwens on July 29, 2011, 09:46:44 am
Oh, and congratulations with your new CX :)

Edit: or wait, I'm confusing you with someone else
Title: Re: Sound with nspire Lua
Post by: Loulou 54 on July 29, 2011, 10:12:32 am
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 ! ;)
Title: Re: Sound with nspire Lua
Post by: Jim Bauwens on July 29, 2011, 10:32:46 am
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!
Title: Re: Sound with nspire Lua
Post by: Loulou 54 on July 29, 2011, 11:32:50 am
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. ^^
Title: Re: Sound with nspire Lua
Post by: Jim Bauwens on July 29, 2011, 12:34:56 pm
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.
Title: Re: Sound with nspire Lua
Post by: Adriweb on July 30, 2011, 01:14:34 am
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 :

Quote
-- 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 :
Code: [Select]
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

%)
Title: Re: Sound with nspire Lua
Post by: Jim Bauwens on July 30, 2011, 01:23:17 am
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.
Title: Re: Sound with nspire Lua
Post by: Adriweb on July 30, 2011, 01:27:05 am
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)
Title: Re: Sound with nspire Lua
Post by: Jim Bauwens on July 30, 2011, 01:30:19 am
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.
Title: Re: Sound with nspire Lua
Post by: Adriweb on July 30, 2011, 01:32:19 am
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)
Title: Re: Sound with nspire Lua
Post by: Jim Bauwens on July 30, 2011, 01:36:12 am
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
Title: Re: Sound with nspire Lua
Post by: Adriweb on July 30, 2011, 01:48:42 am
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.
Title: Re: Sound with nspire Lua
Post by: Jim Bauwens on July 30, 2011, 01:51:55 am
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.
Title: Re: Sound with nspire Lua
Post by: Loulou 54 on July 30, 2011, 07:10:53 am
Wow ! Looks very nice !! :D
Title: Re: Sound with nspire Lua
Post by: apcalc on July 31, 2011, 03:08:59 pm
Those screenshots look quite excellent!  Amazing work! :)
Title: Re: Sound with nspire Lua
Post by: Adriweb on July 31, 2011, 09:17:08 pm
Thanks ;)


BTW, does somebody hère wanna be an alpha/bera tester ? I Will need some :P
Title: Re: Sound with nspire Lua
Post by: Loulou 54 on August 01, 2011, 10:22:54 am
I think I could do that.. :)
Title: Re: Sound with nspire Lua
Post by: Adriweb on August 01, 2011, 01:42:58 pm
All right, I think I'll just put my stuff on GitHub in a few days so it'll be easier to manage
Title: Re: Sound with nspire Lua
Post by: DJ Omnimaga on August 01, 2011, 02:09:22 pm
Seems nice, I wonder how does sound on the Nspire sounds like...

Also does it supports multiple sound channels at once?
Title: Re: Sound with nspire Lua
Post by: Adriweb on August 01, 2011, 02:10:33 pm
not for now, until I (or anybody else actually) find how to get better sound off the nspire port...
Title: Re: Sound with nspire Lua
Post by: pianoman on August 01, 2011, 02:10:58 pm
I'd love to test it out, but I can't do it with sound. D:
Title: Re: Sound with nspire Lua
Post by: Adriweb on August 01, 2011, 02:13:36 pm
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.
Title: Re: Sound with nspire Lua
Post by: Jim Bauwens on August 02, 2011, 01:46:45 am
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.
Title: Re: Sound with nspire Lua
Post by: cypressx1 on August 02, 2011, 07:59:38 am
sounds great ^^
Title: Re: Sound with nspire Lua
Post by: critor on August 02, 2011, 10:43:41 am
Adriweb, I'd like to alpha/beta test too :)
Title: Re: Sound with nspire Lua
Post by: Adriweb on August 04, 2011, 08:50:03 pm
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. :) )
Title: Re: Sound with nspire Lua
Post by: critor on August 05, 2011, 06:12:22 pm
Can we news/publish on TI-Bank, or do you want us to wait?