Registering notes

What attachment to the arduino can I use that can register specific notes? (Like in terms of piano notes)

Sorry, what do you mean buy "register"?

If you want to generate a square wave tone at a known pitch/note, that's easy. Look at the [u]Play Melody Example[/u]. (There are charts on the Internet for converting notes to frequency.)

If you want to analyze a sound or signal and determine the note, that's tricky because of harmonics & overtones, and if there are chords or multiple sounds/instruments it gets even more tricky. If you want to try that search for 'FFT' (Fast Fourier Transform) or 'Autocorrelation'.

Recognising the pitch of a real instrument like a piano is almost impossible to do with 100% accuracy. What exactly are you trying to do that requires this?

I want to recognize the frequency of what note is being played. It doesn't have to be super accurate, but in the general area. Is there any way to do that, without the harmonics and stuff?

So what are your sound source(s)? Can you guarantee it will always be single notes? What range of frequencies (piano fundamentals are roughly 30Hz to 4000Hz)? How quickly do the notes change? And if you discover a frequency/note e.g. 440Hz/A4 what are you going to do with the information?

Steve

jacobdensityxvolume:
I want to recognize the frequency of what note is being played. It doesn't have to be super accurate, but in the general area. Is there any way to do that, without the harmonics and stuff?

That is the problem, the harmonics. What tends to happen is that you get the octave wrong. This is because the fundamental is often not the strongest harmonic at any one time.

Again I ask what you are trying to do, it will answer so many questions, like how long is this note and how quickly after one not has been recognised will another one be along.

I want to recognize the frequency of what note is being played... Is there any way to do that, without the harmonics and stuff?

All natural/real-world sounds have harmonics & overtones. Harmonics & overtones are what makes a guitar sound different from a piano and it's what makes Lady Gaga sound different from Elton John when they are singing the same song and the exact same notes.

If it's an artificially-generated pure-tone tone (or even a square/rectangle wave, which does have harmonics) you can count the zero-crossings or rising-edges for a period of time and calculate the frequency. Otherwise, you'll have to use FFT or autocorrelation (and those may, or may-not, work for your requirements).

It doesn't have to be super accurate, but in the general area.

Musical notes are about 6% apart.* For example, A = 440Hz and A-Sharp is 466Hz. A C is about 6% higher than a B, etc. But, that's not the issue... The problem is identifing the perceived fundamental pitch out of all the simultaneous frequencies... If you are off, you probably won't be off by a few-percent and if you are you just pick the closest note. If you are off, you'll probably be of by a lot.

*That's on the traditional western music scale.

People who have [u]perfect pitch[/u] (AKA absolute pitch) can identify the pitch within 3% or better, and they can name the note (without a reference). That's not good enough to tune a guitar, it's just good enough to name the closest note... You need a reference-note or a gadget to tune a guitar.

Thanks for your help so far everyone. Basically what I'm trying to do is recognize the notes from a tuba mouthpiece. This may work despite the harmonics problem. My machine will also incorporate a three-button system (the valves), so for each valve arrangement pressed, there are only a few certain notes that can be played. If an attachment can just find the general area of where the main note is, the machine wil be able to figure out what note is trying to be played. I know things like tuners can find the note being played, so I'm hoping that along with a valve system I will be able to tell what is being played.

Ok so we have got that it is a tuba but little else. Are you trying to turn the notes into MIDI? What is the output of recognising a note going to be?

Things like tuners do not get the octiave correct, will that matter to you?

I have no idea what sort of sound you get from a tuba mouthpiece with no tuba but how are you planning to capture this sound. Some sort of microphone? What sort?

It sounds like you're planning to make an electronic tuba. Possibly so you can play "silently" through headphones (am I anywhere near?) or maybe to use with a synthesiser. But a tuba plays across at least a 2 octave range so harmonics are definitely going to play a part.

It's a good guessing game isn't it?

Steve

You'll just have to try a couple of the methods/algorithms to see if you get useful data/numbers. In addition to FFT & autocorrelation there is something called FHT. From what I've read autocorrelation works 'best' for guitar tuners, although I haven't read about lots of success making guitar tuners with the Arduino... As you can see, nobody can predict if you'll be successful or not.

The good news is, a tube can't play chords and it's only one instrument so there's only one note at a time.

The complete tuba would be easier because it's constrained (by physics & acoustics) to certain notes... Like a multiple-choice test, it's easier to get the right answer. (I would imagine it's also more difficult to play in-tune without the resonant tube/pipe attached?)


On the hardware side, of course you'll need a microphone. You'll also need a preamp to bring-up the millivolt signal to around 1V, and since the Arduino can't read negative voltages the signal needs to be biased.

If you already have a working microphone & preamp (or mixer) the bias circuit only needs 2 resistors and a capacitor. If you don't have any hardware, a [u]microphone board[/u], with a built-in preamp is the easiest way to go. At least start with something like that so you can try-out the software before you spend time building hardware.

Thank you for all your responses. I am working sort of on making an electronic tuba like you said. I think using only the mouthpiece should be fine because it still produces the same note. With the valve system it will be able to tell the exact note you're going for. I suppose for now even if it gets the octave wrong that's alright, I just want to get close enough. Mostly I need help with knowing how to program a microphone board like that, like "if the frequency is between this and this and these valves are pressed then it is this note." How would you do this sort of thing?

Mostly I need help with knowing how to program a microphone board like that, like "if the frequency is between this and this and these valves are pressed then it is this note." How would you do this sort of thing?

The GOOD NEWS is, that part is easy! It's just [u]if-statements[/u] and some simple [u]Boolean logic[/u].


The two most important concepts in programming are conditional execution (if statements, etc.) and loops (doing something over-and-over, usually until some condition is reached).

I think the first thing I'd try is amplifying the sound, filtering it to limit it roughly to the range of fundamental frequencies you want then hard clipping it to approximate a square/rectangular wave. Get that shifted to 0 to 5V and it may be possible to get to an approximate frequency just by measuring and averaging pulse lengths. Crude I admit but it might work.

If you can get a reasonably accurate base frequency the rest is involved but reasonably straightforward programming. Is your output going to be MIDI notes or have you planned some other way of communicating with the rest of your "machine".

Steve

I don't know how FFT or autocorrelation works, but 'i've successfully found the frequency of a waveform with this:

Find a zero cross point
Find the next zero cross point with same direction (- to + or + to -)
Check if you found a complete wavelength. Do this by comparing subsequent samples from both zero crossings. Calculate the difference of two samples. Square it. Add it to a sum. Continue with following pair. After adding the squared difference of a fair number of pairs, save the sum.
Advance to the following zero cross point and repeat the action ending in saving the sum. After a while you have some candidates for the wavelength. The one with the least sum is your wave length. From that you can calculate the frequency.

If you play say two octaves, you search for say 60 Hz to 240 Hz. That narrows down things. This method should find the basic frequency, no matter if the harmonics are stronger than the basic frequency, as long as the basic frequency is there.

Johan -- I'm not really sure what a zero cross point is, could you explain to me more? Could you maybe message me a step by step of what hardware to get and how to implement all that stuff you explained into it?

Steve -- I was planning on using a MIDI track. I was thinking of importing the notes as different short "songs" into an arduino mp3 player and coding, "when this frequency is registered, play this song on repeat" or something like that. Any ideas? is this sort of importing possible?

MIDI suggests that you have another MIDI capable synthesiser or something playing the notes. If that is going to be an Arduino playing sounds via an mp3 player you probably don't need to convert the notes to MIDI at all.

But I'm not sure it will work very well. In my experience an mp3 player finding a "file" on an SD card and starting to play it takes quite a long time, maybe several seconds. There would be quite a delay between working out the note and hearing the sound which is probably not what you want for an instrument.

You might have more luck using something like Fluxamasynth Shield v.3 – Modern Device or one of the other Arduino synthesisers to create the sounds.

Steve

Johan -- I'm not really sure what a zero cross point is, could you explain to me more?

Do you know what a waveform is A [u]sine wave[/u] is positive half the time and negative half the time. It crosses through zero twice per cycle... A 100Hz sine wave crosses-through zero 200 times per second, once going-up and once going-down.

But as we've discussed, real-world sounds are not sine waves. If you what to see some real-world sound waves, open an audio file in [u]Audacity[/u] (or any other audio editor). You'll have to zoom way-way in to see the individual zero crossings.

Acoustic sound waves in the air don't go negative... They are centered around atmospheric pressure and the air pressure increases & decreases around the ambient pressure... If you are playing sound through a speaker the pressure goes up when the speaker moves-out and it goes down when the speaker moves-in. A microphone doesn't pick-up the constant atmospheric pressure, it just picks-up the changes so the electrical signal coming-out goes positive & negative.

Steve -- I was planning on using a MIDI track.

That's a lot different from a tuba mouth piece... Of course, the computer already knows all of the MIDI notes and if you're playing a MIDI file it knows the notes way in advance of when they are played.

Could you maybe message me a step by step of what hardware to get and how to implement all that stuff you explained into it?

Um, no. I don't know hardware. I've done this on a computer, fiddling around with a microphone, recording sound to Audacity, creating wav files, reading the files as binary data into a C/C++ program, which found the wave lengths using the method I described. I might be able to do the same stuff with an Arduino and some hardware, but I'd have to do a lot of research before I'd get it working. You do the research instead. :slight_smile:
But I guess the list of hardware is not that long. An Arduino, a microphone, some flash memory (a micro sd card?)
Get Audacity! Play with it until you get an idea of what waves look like. Same pitch on different instruments creates very different wave shapes, but they all have the same wave length, if the pitch is the same. When you feel confident, do the following. In Audacity, record a continuous 1 channel (mono) sound of one tone of an instrument. Duplicate the produced wave. Focus on one spot and zoom in so that you see the wave lengths (repeating, almost similar wave forms). Take the time shift tool and shift one of the tracks one whole wave length. Zoom in to really see the individual samples to get the zero crossings exactly on top of each other. Now, if you'd subtract one wave from the other, almost nothing would be left, because the consecutive wave lengths seem to be almost identical. To do the subtraction, you have to invert one of the tracks. Then mix both tracks. You don't end in a flat line, because the wave lengths are not identical, but it's not far from flat. But if you didn't shift one track a whole wave length, but only to say the next zero crossing, which wasn't a full wave length, your mixed track wouldn't be flat at all. So, perfect wave length shift -> almost flat mix after inverting one track. Not a perfect wave length shift -> not at all flat mix. This very same procedure I did as a C/C++ program. What my program did was it squared each sample of the presumably flat mix. Then it added a bunch of squared samples. The sum told whether the mix was flat or not. Without squaring, the sum would have been near zero in any case, because there would might have been equally many samples above and below zero.

was planning on using a MIDI track. I was thinking of importing the notes as different short "songs" into an arduino mp3 player and coding, "when this frequency is registered, play this song on repeat"

If you are using MIDI then their is no need to record anything.

Just send the MIDI sound generator with a note on and note off message. The message has three byte:-

  1. Note on command ( a value of 0x90 )
  2. The note number 0 to 127 - for example Middle C has a note number of 60
  3. The note velocity 0 to 127 - a sort of loudness control.