Author Topic: Sound with nspire Lua  (Read 21678 times)

0 Members and 1 Guest are viewing this topic.

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Sound with nspire Lua
« 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?
« Last Edit: November 11, 2011, 02:04:21 pm by jimbauwens »

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: Sound with nspire Lua
« Reply #1 on: July 25, 2011, 09:12:24 am »
Do I need headphones to listen to this? If so how do I plug them in?
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Sound with nspire Lua
« Reply #2 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):



Edit: better picture, thanks Critor!
« Last Edit: July 25, 2011, 05:45:09 pm by jimbauwens »

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: Sound with nspire Lua
« Reply #3 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?
« Last Edit: July 25, 2011, 09:20:17 am by ruler501 »
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Sound with nspire Lua
« Reply #4 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.

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Sound with nspire Lua
« Reply #5 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:
« Last Edit: July 25, 2011, 04:01:33 pm by jimbauwens »

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: Sound with nspire Lua
« Reply #6 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?
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Sound with nspire Lua
« Reply #7 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 :)

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: Sound with nspire Lua
« Reply #8 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
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Sound with nspire Lua
« Reply #9 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
« Last Edit: July 25, 2011, 04:11:38 pm by jimbauwens »

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: Sound with nspire Lua
« Reply #10 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?
« Last Edit: July 26, 2011, 12:10:29 am by t0xic_kitt3n »

Offline thepenguin77

  • z80 Assembly Master
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1594
  • Rating: +823/-5
  • The game in my avatar is bit.ly/p0zPWu
    • View Profile
Re: Sound with nspire Lua
« Reply #11 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.
zStart v1.3.013 9-20-2013 
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
       -Runer112

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Sound with nspire Lua
« Reply #12 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!

Offline renatose

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 209
  • Rating: +4/-0
  • watch out the power balls
    • View Profile
Re: Sound with nspire Lua
« Reply #13 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

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: Sound with nspire Lua
« Reply #14 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.