Omnimaga

Calculator Community => Other Calc-Related Projects and Ideas => TI Z80 => Topic started by: SirCmpwn on October 23, 2010, 03:07:27 pm

Title: Sound
Post by: SirCmpwn on October 23, 2010, 03:07:27 pm
Hello,
How do I output sound through the link port in pseudocode?  And a textual explanation would be nice too.
Thanks!
Title: Re: Sound
Post by: thepenguin77 on October 23, 2010, 03:22:29 pm
You are probably going to want to play 8 bit music, so I'll go with that. You need to turn the link port on an off for varying amounts for each different byte. So say you have 257 units to work with. For byte 0 it would be 1 off and 256 on. Byte 1 would be 2 off and 255 on. Byte 2 would be 3 off and 254 on. And it continues on...

But to do this accurately and very fast, (<200 t-states of switching code if you want good quality), you need to sacrifice space for speed. This means making a huge table of code for every single possible byte. The way you do this can vary, but you basically have to have dedicated code if you want quality.

Also, using this technique, you have to have music of at least 20KHz sample rate. Any lower and those with really good hearing will be in pain.


That's not really pseudo code, but that's how I do it.
Title: Re: Sound
Post by: SirCmpwn on October 23, 2010, 03:24:08 pm
Thanks, but I want to play wav files.
Title: Re: Sound
Post by: thepenguin77 on October 23, 2010, 03:30:21 pm
Those are wav files. I mean 8-bit wav files. Not midi's. I have no idea how those work.

The instructions are for what you do once you have read the header and are going to play music for real.
Title: Re: Sound
Post by: SirCmpwn on October 23, 2010, 03:40:32 pm
Oh, I see.  So each byte of the music represents the sound?  So if I open up a wav file and move past the header, and the first byte is 0x10, I'll do 0x10 "units" with both link port lines pulled high, and 0xEE "units" with both lines low?
And what "units" would be good?
Title: Re: Sound
Post by: DJ Omnimaga on October 23, 2010, 03:41:29 pm
I wonder if the source of this could be useful? http://www.ticalc.org/archives/files/fileinfo/176/17685.html
Title: Re: Sound
Post by: SirCmpwn on October 23, 2010, 03:43:58 pm
Thanks DJ!  That looks useful.
Title: Re: Sound
Post by: DJ Omnimaga on October 23, 2010, 03:48:26 pm
Yeah I wasn't sure how optimized it was, though, since it was made several years ago, before many today's tricks were discovered. I just rememebered using that back then. It did 11 KHz 8 bit music, if I remember. A 3 second audio file is 20 KB on-calc, though...
Title: Re: Sound
Post by: SirCmpwn on October 23, 2010, 03:55:22 pm
Don't worry about that ;)
Title: Re: Sound
Post by: thepenguin77 on October 23, 2010, 04:13:08 pm
For units, this is basically the most amount of t-states that you can fit within your time constraints. So a bit of math: 15,800,000 t-states / 22500 hz = 702 t-states per byte. Now, we need 257 units, but to be safe around the edges (1 unit is usually impossible) we'll call it 260 units. So your options for units are either 1 or 2 t-states. 1 t-state will give you 442 t-states for note switching but low volume. With cheap headphones, it probably won't be audible. 2 t-states is what I use and that gives you 182 t-states to switch. That's plenty of time for optimization if space is an issue, or time for compression :).

So I would recommend that you use 2 t-states as your unit of time. This is so small that you can see why you need dedicated code. And you correctly understood what I said the first time.

Oh, and I used 15.8 MHz as the calculator speed because that's the average speed that I have found in calcs. I have seen some as slow as 14.8 and as fast as 16.2.
Title: Re: Sound
Post by: SirCmpwn on October 23, 2010, 04:16:06 pm
So... if I had 90 MHz to work with...
Title: Re: Sound
Post by: DJ Omnimaga on October 23, 2010, 04:18:28 pm
You could port MTV Music Generator to the TI-Nspire. ;D
Title: Re: Sound
Post by: thepenguin77 on October 23, 2010, 04:20:50 pm
Wow, unexpected development.

You would have 4,000 t-states per byte. I would use between 10 and 12 t-states as your unit. And then use those spare 1,140 to decode MP3.

Edit:
    No. You have so much ram that that's not even necessary. Decode the MP3 beforehand and then play it as a wav.
Title: Re: Sound
Post by: SirCmpwn on October 23, 2010, 04:24:48 pm
Well, TI-Nspire plus TI-84+ keypad gives me 90 MHz and an I/O port to work with.
At 90 MHz, with a music frequency of 44100 Hz, I have 2040.8 t-states per byte to work with.  With 257 units, we have 1783 t-states at one t-state per byte.  At 4 t-states per byte, we get 1012 t-states per byte.  6 t-states per byte leaves us 498 t-states per byte.  Shouldn't be a huge problem :)
(I just guessed at your calculations.  Care to elaborate how you got these numbers?)
And as for decoding the MP3, I would just do it in real time to save the user the issue of waiting.
Title: Re: Sound
Post by: Munchor on October 23, 2010, 04:27:05 pm
http://www.ticalc.org/archives/files/fileinfo/176/17685.html (http://www.ticalc.org/archives/files/fileinfo/176/17685.html)

That looks very good!
Title: Re: Sound
Post by: DJ Omnimaga on October 23, 2010, 04:51:53 pm
SirCmpwn in 84+ mode the 84+ doesn't even simulate 90 MHz. Idk how fast it really runs but notice how the speed is roughly the same, if not slower, as a real 84+. You'll have to use TI-Nspire mode with Ndless, then switch the keypad after running a game.
Title: Re: Sound
Post by: fb39ca4 on October 23, 2010, 04:58:08 pm
You could use usb audio...
Title: Re: Sound
Post by: DJ Omnimaga on October 23, 2010, 05:00:19 pm
Yeah that can work, but those adapters are not available everywhere in retail stores, like over here. Then the person is forced to get one online, which is a problem if his parents won't allow him to buy stuff online or if he cannot get a credit card...
Title: Re: Sound
Post by: SirCmpwn on October 23, 2010, 05:24:01 pm
SirCmpwn in 84+ mode the 84+ doesn't even simulate 90 MHz. Idk how fast it really runs but notice how the speed is roughly the same, if not slower, as a real 84+. You'll have to use TI-Nspire mode with Ndless, then switch the keypad after running a game.
That's what I was planning, switch the keyboard after you start the program.
Title: Re: Sound
Post by: DJ Omnimaga on October 23, 2010, 05:48:16 pm
Ah ok. I think Willrandship or someone else also wrote a program for when you use the 84+ keypad that remaps the keyboard so keys aren,t all scrambled afterward.
Title: Re: Sound
Post by: SirCmpwn on October 23, 2010, 05:54:41 pm
Orly?  I'd like to see that.
Title: Re: Sound
Post by: DJ Omnimaga on October 23, 2010, 06:06:05 pm
I think it is in that topic: http://ourl.ca/4852