# "Just Music" justly tuned music library

I’m looking into creating this library. I don’t really have much of a clue about programming in anything but python and lua besides a general idea of programming, but will be taking a c++ class next semester. I don’t anticipate much difficulty in leaning the required programming knowledge, and I’m almost certain that I understand the physics and music theory as much is as neccesary. I’d appreciate some help with understanding the hardware limitations though; I need to know what the formula is for determining the minumum frequency difference between pitches is and what the relevant constants are.

Might start by reading the existing audio code.

As i said, I don’t know anything besides lua and python well enough to read it, I don’t mean to be a PITA, but after reading through it as much as I could, along with a lot of other documentation, I was hoping somebody else could tell me, so I could start designing the program. After reading through what I could, it seems like it should be pretty quick and easy to figure out if you know how to. I’m just looking for the formula for the minimum difference in hz I’ll be able to generate between two tones based on programming.

Howdy, just thought I’d post this chart on the notes and their corresponding frequencies.
http://www.phy.mtu.edu/~suits/notefreqs.html

And then from the Arduino library, the function for generating tones!
http://www.arduino.cc/en/Reference/Tone

Hopefully that will be enough to get you started, and show it’s actually not all that darn complicated to get the sounds you would like as it relates to a musical note.

And then…
If you would like to try out some simple sound generation that is nearly identical to what occurs on the Arduboy, except done with python, check out this link.

And this one too!
https://wiki.python.org/moin/Audio

The first link, in this context, is completely (edit: irrelevant, not incorrect), except for, possibly, the octaves of one note, and I’ve already gone through the second and through that to brett’s notes. I’m not asking all that much, and have already expended a large number of hours researching this.

I’m not sure what you are asking then. Looking at the `tone()` documentation you can see it simply takes a frequency. The first link is a list of frequencies for notes. Can you please rephrase the question in terms of what you would like to accomplish?

##Tone Example
tone(pin, frequency)
tone(pin, frequency, duration)

To play a middle C note:

``````tone(a_pin, 261, 1);
``````

## Sequencing

If you are asking about sequencing then you are going to be playing notes ones after the other for a specific duration within a time segment delineated by divisions, or measures.

If you are asking about electronic music creation as it relates to an ADSR, (attack, decay, sustain, and release) in order to craft a more realistic or instrumental note, that will require an amplitude envelope. Is this what you are asking about?

## Polyphonics

Although some of these approaches may not be possible on the Arduboy because of the pin configuration to the piezo, which, someone please refresh me, will not allow the PWM to be sent to the piezo — which is a pretty interesting design choice. Something worth crying about even.

You can take a look into Fourier synthesis for a bit more information on how you combine the frequencies of multiple single notes to create a single waveform.

Not hardware PWM, but you can do PWM with software, which is what we do now. Hardware PWM would be nicer though. @bateske is aware of the fact that we’d like PWM to the speaker in the final revision if possible.

I’d love to know how this relates to PWMing a digital output. I know the timing gives you the frequency… but right now attack and release are instance… and sustain is obviously the length we hold the note.

Yeah check out the pic again, its a relationship of time to amplitude. In order to mimic the dynamic nature of notes on instruments you use an envelope to alter the amplitude of the waveform over time. These are really quick periods of time, milliseconds to seconds, like hitting a drum or plucking a string.

As it relates to the PWM, the PWM, is the generator and is creating the tone that is going to have the envelope applied to it. A high attack would be an initial high volume for the tone, then time moves into the decay portion which talks about the volume of the tone after the attack and has another volume setting, and into sustain and release. That relationship is how you get interesting sounds.

I honestly don’t know if the amplitude of the piezo signal can be diminished in the current setup.

Here is a circuit for the method I am aware of, which is to alter the duty cycle.

Ha, i just saw it was for a parallax, which was the first brand of microcontroller we all ever owned I’m sure!

Yeah, I dunno about amplitude (which is really volume, right)… I know duty cycle changes the sound “effect”, but I dunno if 50% duty cycle is anything like 50% volume.

Duty cycle on a piezo will alter the volume, and yeah volume = amplitude. Here is a sin wave breakdown, where A is the amplitude and simply controls the height of the waveform. Only look at the y(x,t) function, and not the differentials below.
Sorry this is probably a terrible image in retrospect.

@dualweilder is asking about Just Intonation which is basically perceived as a more ‘natural’ and mathematical way of constructing a musical scale, as opposed to Equal Temperament tuning which is the standardised system used on the keys of a modern piano and most ‘western’ instruments that followed.

The thing with the Arduboy is that the frequencies used to produce musical notes are derived from a division of the 16MHz (or 8MHz?) clock - so they are never going to be completely accurately tuned even in relation to Equal Temperament.

So good luck generating frequencies accurate enough to satisfy Just Intonation!

1 Like

Define “accurately”… 16Mhz is fast enough to make a pretty close approximation of say 25khz (one of the highest notes in our music library). In fact 25Khz should be exact (as a multiple of 16Mhz…)… while you might not be able to accurate do 25,001Hz, can anyone tell the difference? How much off does a frequency need to be for it to sound “out of tune”. We could definitely play the low notes a LOT more accurately than we do now. We should be storing their frequencies with a lot better accuracy than we are… it’s not 16… it’s 16.xxx… and that would easily fit in the 16 bit values we’re using for tone.

@uXe, Thanks a billion. I have never heard of this. I really appreciate your reply. A google search of “justly tuned” turned up this thread as the top google search. I felt like I had offended dualwielder with my answer! I see what is going on now. I think if this was stack overflow you would have the accepted answer by leaps and bounds. Thanks again!

Define “accurately”? I’ll try…

“The unit most commonly used to measure intervals is called a cent, from Latin centum, meaning ‘one hundred’. It stands for one hundredth of an equal-tempered semitone. In other words, one octave consists of 1200 cents.”

“Cent is a logarithmic unit of measure of an interval, and that is a dimensionless ‘frequency ratio’ of f2 / f1.”

So, using your example of 25kHz:

``````25000 Hz plus one 'cent' = 25014.44473766387 Hz
``````

That is what ‘accurately’ means in the world of Just Intonation!

1 Like

Interesting. I just did the math for the highest piano note at 4186.01Hz… The closest we could get at 8Mhz would be 4,186.289901Hz… so we’re playing a note that’s actually 0.00668% too high. Is that off enough to be considered out of tune? I wouldn’t think so.

1 Like

I agree. Subjectively, it doesn’t make any difference, especially when we are talking about an 8-bit video games platform, you almost need the tuning to be a little wonky - that is part of the charm!

But in a discussion of Just Intonation, tuning is an objective ideal - talked about in terms of ‘perfect’, ‘natural’ and ‘pure’ harmonics. Which might not be achievable on the Arduboy, that is all I am trying to say.

1 Like

Sorry about disappearing for so long. I had some fairly major personal issues to deal with. uXe is correct about my intention of using just intonation. I shouldn’t have taken offence, but as I said, I had to deal with some personal issues. Sorry for being such an a**. I’m planning on using the arduboy itself, and not some external source, to come up with the tuning. Basically, anything involving logarithms, cents, or equal temperament is out. Everything will be based on rational numbers from the arduboy’s point of view. Given that I need more than octaves, fourths and fifths, this means I’ll need to multiply the smallest available exact frequency by some powers of some prime numbers other than two, most likely only 3 and 5, to come up with a base frequency. Wikipedia has a good article on the possible tunings like this, and I intend to support them all. I intend to make a library exclusively for producing these sounds,similar to tone(), but with tuning system choice and musical note specification commands instead of frequency commands. After that, if possible, I’ll make an abc notation library or application, and an effects library or application to go with it. Someone else is almost certainly going to come up with a better tracker/sequencer style app than I’m willing to put effort into, so I’m most likely not even going to try.

Given my intent, just intonation is not an ideal at all, except, if I’m not mistaken, for the fact that the clock speed is not exactly constant due to the temperature, and maybe some other similar factors. While logarithms are irrational, making equal temperament not programmable to a turing machine, (not sure about the more exotic types of computers) just intonation is entirely possible in a subset of the frequencies available. I have absolutely no intention of sticking with an arbitrary standard like A=440 hz. The standard will be based entirely on the arduboy itself.

Duty cycle might alter the the volume, but likely not in our case.

If the rate is far above the audio fequency - as in hundreds of times higher, AND if there is some low-pass filtering either explicitly before the peizo or just implicitly because it only handles audio frequencies, then you will get averaging of the PWM duty cycle to result in the equivalent of a voltage level. If you run +V to 0 at 50% duty cycle, the average will come out to +V1/2. A duty cycle of 25% will give you +V1/4, and 75% will give you +V*3/4. At a more complex level, you are running a 1 bit D/A converter - http://www.cs.tut.fi/sgn/arg/rosti/1-bit/ . But read those specs carefully. You need to be sampling at least 32 times higher than your maximum audio frequency! This method will also likely demand considerable CPU involvement in any useful sound generation.

If the fundamental frequency of the PWM is also the intended audio frequency, then the duty cycle instead will alter the harmonic content of the sound. You can look here http://hephaestusaudio.com/delphi/2009/06/24/class-d-pwm-harmonics/ for a summary, but what it amounts to is this - changing the duty cycle alters the character of the sound, while keeping the fundamental frequency constant.

I doubt very much that we are using the first condition. The PWM frequency would most likely be the fundamental audio frequency. Altering the duty cycle would alter the type of sound you hear. From the users point of view in playback, the fundamental frequency is the note, and the duty cycle is a simple control for type of instrument.

Note that this also means that without some additional involvement, there will be no volume control. Everything I’ve seen about the PWM system is that you get fine control over frequency and duty cycle, but none over amplitude. A second analog pin could be used with a transistor to control amplitude, or a pair of pins and a resistor ladder would give a 2 bit D-A converter. But without additional hardware, there is no volume control.

This is likely why classic games didn’t have digital volume control, and just used a pot to attenuate the signal.

1 Like

We’d have 59 cycles to decide whether to flip the bit or not (at the highest piano note, at 32 times frequency). IO Load, IO Push, some math for modulus… seems you need a counter… ADD, AND, LD, ST, CMP, JMP… It might not be impossible, but yeah we’re burning thru a few CPU cycles pretty fast if 32 times is really the requirement.