Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: Michael_Lee on November 01, 2010, 11:05:07 pm

Title: Music using Freq(
Post by: Michael_Lee on November 01, 2010, 11:05:07 pm
Hello: Which frequencies using the Freq( command correspond to which notes?
Like which frequency would give a middle C, or what would give an A sharp, etc...

Also, for time, how much equals a second, and how much equals half a second, etc...

Also, is it possible to play chords and hold notes longer then what I get when I input 65535 for time?

Title: Re: Music using Freq(
Post by: FloppusMaximus on November 02, 2010, 12:35:56 am
Untested, but based on the source code provided, it looks like it's 47·n + 59 clock cycles per iteration (or on the 84+, 47·n + 61 in RAM, 55·n + 70 in a Flash app.)  Each iteration is only half a cycle of your square wave, though.  So if you wanted to play a tone of 440 Hz, you'd want the frequency parameter to be ((6,000,000 / 880) - 59) / 47 = 144.

For the time parameter, figure each tick is about 47/6 = 7.8333 microseconds, so a second would be 128,000 ticks.  This will vary depending on the frequency parameter, though.  (If you want to play notes longer than 65536, I think it would work so long as you ensure that the time parameter is a multiple of twice the frequency parameter.  Or maybe 1 less.)

Since it's all based on the CPU clock, your mileage will vary - you won't get perfectly precise hardware-independent tones in any case.  Still, you might want to experiment a little.

No, you can't play chords with that routine.
Title: Re: Music using Freq(
Post by: DJ Omnimaga on November 02, 2010, 12:43:42 am
Something I noticed is that when alternating between some notes extremly fast, back and forth, it sounded a bit different from regular square waves. More like Final Fantasy 1 NES style than SMB1.
Title: Re: Music using Freq(
Post by: Michael_Lee on November 02, 2010, 06:06:34 pm
Untested, but based on the source code provided, it looks like it's 47·n + 59 clock cycles per iteration (or on the 84+, 47·n + 61 in RAM, 55·n + 70 in a Flash app.)  Each iteration is only half a cycle of your square wave, though.  So if you wanted to play a tone of 440 Hz, you'd want the frequency parameter to be ((6,000,000 / 880) - 59) / 47 = 144.

For the time parameter, figure each tick is about 47/6 = 7.8333 microseconds, so a second would be 128,000 ticks.  This will vary depending on the frequency parameter, though.  (If you want to play notes longer than 65536, I think it would work so long as you ensure that the time parameter is a multiple of twice the frequency parameter.  Or maybe 1 less.)

?
I'm sorry - I barely understood that - it was too complex for me.
Do you think it's possible for you to churn out a few nice and simple formulas where I can plug in whatever hertz I want to output the frequency value I should put in?
Title: Re: Music using Freq(
Post by: Quigibo on November 02, 2010, 08:30:16 pm
I'm not sure what the actual tones correspond to, but I have been using the wavelength column of this table (http://www.phy.mtu.edu/~suits/notefreqs.html) which seems to give pretty good results.
Title: Re: Music using Freq(
Post by: FloppusMaximus on November 02, 2010, 10:00:17 pm
Funny, you're right, the wavelengths in that table are close to the values you want (though unless I've made a mistake somewhere, they're actually off by about an octave.)

If f is your frequency in hertz, and t is your duration in seconds,

frequency parameter (or I guess wavelength parameter would be a better name), k = ((3000000 / f) − 59) / 47
or k ≈ (63830 / f) − 1.3

duration parameter, d = t · 6000000 · k / (47k + 59)
or dt · 127660 · (k / (k + 1.3))
(or for low frequencies, dt · 127660)

and if you want to play a continuous tone using two 'Freq' commands, make sure that for the first command, d is a multiple of 2k.
Title: Re: Music using Freq(
Post by: DJ Omnimaga on November 02, 2010, 10:02:51 pm
I bookmarked that page. It might be useful when I start messing with Freq(.
Title: Re: Music using Freq(
Post by: LordConiupiter on November 03, 2010, 11:17:58 am
perhaps frequency could be overloaded with another parameter type? because it we have not so many tones we can access right now, and i think we just only use the Freq( command to play music with normal tones.
Example:
Code: (Axe) [Select]
.for a low A sharp
Freq(^A,1
.for a high B flat
Freq(vB,5
.for a central C
Freq(-C,3
Title: Re: Music using Freq(
Post by: Michael_Lee on November 03, 2010, 03:12:59 pm
perhaps frequency could be overloaded with another parameter type? because it we have not so many tones we can access right now, and i think we just only use the Freq( command to play music with normal tones.

I'm not sure if this'll be a good idea - you can use Freq( for sound effects, which requires more granular control, plus I think that might be a bit difficult to parse.

So question: How does my computer play chords when I'm listening to music?  Is it possible to do something similar on-calc?
Another question: Is it possible to control volume on the calculator?
Title: Re: Music using Freq(
Post by: squidgetx on November 03, 2010, 03:29:41 pm
So question: How does my computer play chords when I'm listening to music?  Is it possible to do something similar on-calc?
Another question: Is it possible to control volume on the calculator?

You can use Axe to play fake chords: play the notes of a chord in succession quickly with no breaks in between.

There is sadly no volume control on calc, afaik (even with assembly)
Title: Re: Music using Freq(
Post by: ASHBAD_ALVIN on November 03, 2010, 03:31:36 pm
Yeah, with assembly all we are doing is sending value through the link port in a certain way -- the port isn't built with a way to recieve or handle volume requests :P

EDIT: oh and the chords sound decent, but not perfect -- kinda spasmism if you listen closely, but otherwise acceptable
Title: Re: Music using Freq(
Post by: Quigibo on November 03, 2010, 08:12:15 pm
Actually, volume can be controlled (to a point) by altering the duty cycle.  The duty cycle is how long the pulse lasts relative to the period of the pulses.  If anyone remembers the old Freq() command when it first came out in Axe, it was much softer because it had a much smaller duty cycle.  I do not have a control for the volume in the Freq() command because it would make the routine much larger.  Some sound players even have a separate routine for every possible volume in the duty cycle to speed it up for some decent quality audio.
Title: Re: Music using Freq(
Post by: Deep Toaster on November 03, 2010, 08:57:14 pm
I'm not sure what the actual tones correspond to, but I have been using the wavelength column of this table (http://www.phy.mtu.edu/~suits/notefreqs.html) which seems to give pretty good results.

Nice, a coincidence? And so that's why the docu says Freq(wavelength,time).
Title: Re: Music using Freq(
Post by: DJ Omnimaga on November 03, 2010, 11:56:55 pm
Yeah, with assembly all we are doing is sending value through the link port in a certain way -- the port isn't built with a way to recieve or handle volume requests :P

EDIT: oh and the chords sound decent, but not perfect -- kinda spasmism if you listen closely, but otherwise acceptable
Really? I migth have done something wrong, or maybe it was Wabbitemu, because in my case it sounded like static or something metallic when alternating between notes too fast :(. It has been ages since I last tried, though.
Title: Re: Music using Freq(
Post by: Michael_Lee on November 04, 2010, 07:30:33 pm
Tangentially related to this, I made a sort of piano/wind instrument sort of thing.

Turn the calc sideways, and press any of the keys from [2ND] to [STO>]
They're both C's, so [ALPHA] would be D, [MATH] would be E, etc...
If you press the left and right arrows (well, actually up and down from the sideways perspective), the range goes up and down by an octave (indicated by the number on the screen.
Pressing the other two buttons moves the range by a whole note (the letter on the screen indicates which note [2ND] is on.
For sharps, press the key over the appropiate one (C sharp is [MODE], the key above [2ND], which is C)
It works well in the middle region, but sounds weirder if you go too high or low.

(Wow - that was a long and complicated explanation.  But it's intuitive - just turn the calc on the side, try pressing the closest 2 row of keys and the arrows keys and experiment)

I compiled w/ Axe 0.4.5 - the file is source code.
Title: Re: Music using Freq(
Post by: Deep Toaster on November 04, 2010, 08:06:35 pm
Nice! I can't try it now, unfortunately, but it sounds great! As a suggestion, maybe making keys on other rows play other octaves?
Title: Re: Music using Freq(
Post by: DJ Omnimaga on November 04, 2010, 11:06:06 pm
That's cool Michael_Lee. Some more programs to demonstrate the sound in Axe language. :)
Title: Re: Music using Freq(
Post by: Broseph Radson on November 04, 2010, 11:18:16 pm
Another way to have multiple octaves would be to use the arrow keys to go up or down and use the same note keys to play the notes. That way you aren't as limited to how high or low you can go, and (I'm assuming) you can mathematically change the octave.
Title: Re: Music using Freq(
Post by: Deep Toaster on November 04, 2010, 11:48:34 pm
Another way to have multiple octaves would be to use the arrow keys to go up or down and use the same note keys to play the notes. That way you aren't as limited to how high or low you can go, and (I'm assuming) you can mathematically change the octave.

I think that's what he's doing right now:

Turn the calc sideways, and press any of the keys from [2ND] to [STO>]
They're both C's, so [ALPHA] would be D, [MATH] would be E, etc...
If you press the left and right arrows (well, actually up and down from the sideways perspective), the range goes up and down by an octave (indicated by the number on the screen.
Title: Re: Music using Freq(
Post by: Broseph Radson on November 05, 2010, 08:37:49 am
Oh wow /embarrassed
Title: Re: Music using Freq(
Post by: DJ Omnimaga on November 05, 2010, 06:50:07 pm
Don't worry about it :D
Title: Re: Music using Freq(
Post by: Keoni29 on March 15, 2011, 10:28:58 am
I'm experimenting with freq too, but it gives me a low quality sinesquarewave with this code:
.CHIPSND
Repeat getKey(15)
  For(A,1,12)
    Repeat A=50
      Freq(i*100,2000)
      A+1 > A
    End
    0 > A
  End
End
Title: Re: Music using Freq(
Post by: aeTIos on March 15, 2011, 11:45:10 am
try a longer lenght in freq(, like 3600