Shop OBEX P1 Docs P2 Docs Learn Events
Musical Note Detection Possibilities? — Parallax Forums

Musical Note Detection Possibilities?

ercoerco Posts: 20,256
edited 2015-02-25 18:22 in General Discussion
I'm working on a music project for the twins, who are learning to play recorders (little flutes). It's simple enough to detect sound versus silence, I'm wondering if there is a simple microphone+audio amp+filter/clip circuit that might condition the recorder sound so a Stamp could tell the difference between 3 or 4 different tones using COUNT and light different LEDs. The notes can be spaced apart as necessary, don't need to be adjacent.

No doubt, it will come down to the purity of the sound produced (harmonics+other noise) by the recorder. An electronic square wave generator could make audio frequency tones that would be easier to detect, but it would be more magical to use their recorders. My backup plan is using more hardware like several 567 tone decoders, but a software solution would be much simpler.

Has anyone tried anything along these lines? Those silly little whistle keychain beepers that are nothing but a piezo sensor/speaker and epoxy blob make it look easy.

Comments

  • ercoerco Posts: 20,256
    edited 2015-01-19 20:09
    After doing a little research, I think I'll try a kazoo instead. That buzzy sound has got to be more digital/square wavy than a recorder.

    I found the attached "record/playback" circuit at http://www.engineersgarage.com which is a fun site to poke around. It's got a 1-transistor amp input to a PIC, may be a good place to start.
    800 x 754 - 89K
  • kwinnkwinn Posts: 8,697
    edited 2015-01-19 20:38
    If you use a comparator, or op amp/transistor to square up any single note you can use a counter to determine the frequency. In a lot of cases no amplifier is needed. Use a voltage divider to bias the signal input pin to 1/2 Vcc, a capacitor to block any DC in the input signal, and count away.
  • CuriousOneCuriousOne Posts: 931
    edited 2015-01-20 00:40
    Can't say for stamp, but I have done such on the ZX Spectrum about 30 years ago, it was quite primitive FFT, but it was working in realtime - 3mhz cpu, 48k ram.
  • GordonMcCombGordonMcComb Posts: 3,366
    edited 2015-01-20 08:52
    There are some uC-based guitar tuner projects out there (try Hackaday, etc.), and whether it would be easily reproducible would depend on the chip they used. Obviously, find one where all the filtering is done in software, simplifying the input circuit. I would imagine it would be a little much for a Stamp or PICAXE.
  • Heater.Heater. Posts: 21,230
    edited 2015-01-20 09:21
    Here is an answer to a similar question I posted a while back:

    Probably possible on the Prop. Depending on what kind of accuracy you want.

    What you need is a Fast Fourier Transform to tell what frequencies are present in your signal.
    There is one here : http://propeller.wikispaces.com/FFT

    Or my FFT is here : http://forums.parallax.com/showthrea...ier-Transform.

    Or there are a few similar transforms you can use. Check out this nice Propeller Sound analyser demo video: http://www.youtube.com/watch?v=qvjG2j6uh94

    You may need a steep low pass filter to get this working nicely.


    Here is Pete Hemery detecting notes played on a Kalima with a Propeller: https://www.youtube.com/watch?v=xQUO_jfl8SE
  • PublisonPublison Posts: 12,366
    edited 2015-01-20 10:09
    Here's Pete's submission:

    http://petehemery.github.io/Final-Year-Project/

    It's pretty impressive.
  • kwinnkwinn Posts: 8,697
    edited 2015-01-20 10:24
    An FFT seems to be a bit of overkill when all you need is the fundamental frequency. Counting the number of high/low transitions for a period of time will do it.
  • LeonLeon Posts: 7,620
    edited 2015-01-20 12:32
    If you know the actual frequencies in advance, the Goertzel algorithm is ideal for identifying a single frequency.
  • Heater.Heater. Posts: 21,230
    edited 2015-01-20 14:14
    kwinn,
    Counting the number of high/low transitions for a period of time will do it.
    Might do. If your input only contains a single tone. And if the harmonics of that are not to big.

    Decades ago I remember some fellow students in my physics department trying to measure the frequency of a big old brass bell using that technique and a frequency counter.

    Their output was pretty much random numbers.
  • GordonMcCombGordonMcComb Posts: 3,366
    edited 2015-01-20 15:58
    Heater. wrote: »
    Might do. If your input only contains a single tone. And if the harmonics of that are not to big.

    I think a kazoo is all harmonics. The sound is made by humming into it, which is itself already a complex waveform, made even more complex by adding timbral effects to it. For detecting raw tones, the recorder is much better, I'd think.
  • ercoerco Posts: 20,256
    edited 2015-01-20 19:10
    If only there was some type of a device to graphically view a waveform in order to determine harmonic content.

    Hmmm. I wonder if I could modify the CRT from an old TV into something along those lines. But where might I possibly find a discarded old CRT TV?
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2015-01-20 19:39
    Okay, erco, it really is time for you to embrace the Propeller and the power that it brings to the table. I know, I know. Like you, I'm a minimalist at heart. The BASIC Stamp was, and shall ever remain, a tour de force in the 8-bit processor world. But sometimes you just need bigger guns. And this is one of those times. I agree with Leon that the Goertzel algo is the way to go. You can find my Goertzel object embeddid in the zip here:

    -Phil
  • GordonMcCombGordonMcComb Posts: 3,366
    edited 2015-01-20 20:18
    erco wrote: »
    If only there was some type of a device to graphically view a waveform in order to determine harmonic content.

    There are probably plans somewhere for building an audio spectrum analyzer. Somewhere. But it might be easier to find some freeware or demo software that does it. Alls you'd need is a microphone connected to your sound card. Save your old CRT for the Ercovision for your Corvair.

    And yes, listen to Phil. Just get a damn Propeller already! Geez!!
  • RS_JimRS_Jim Posts: 1,765
    edited 2015-01-21 05:23
    At one time there was a thread about an audio spectrum analyzer for the demo board.

    Try this http://forums.parallax.com/attachment.php?attachmentid=48409
    Jim
  • kwinnkwinn Posts: 8,697
    edited 2015-01-21 09:41
    erco wrote: »
    If only there was some type of a device to graphically view a waveform in order to determine harmonic content.

    Hmmm. I wonder if I could modify the CRT from an old TV into something along those lines. But where might I possibly find a discarded old CRT TV?

    Why don't you plug a microphone in to your PC and record one of your kids playing the flute. If you record it as a .wav file you could use Audacity or a similar program to look at the waveforms. I'm pretty sure the output from any flute like instrument produces a fairly simple to square up wave.
  • jmgjmg Posts: 15,173
    edited 2015-01-22 14:03
    I agree, start with a sound card, and PC analysis software while you work out what you actually have, in a waveform and spectral content manner.
    There are a lot of SoundCard package out there, and some commercial scopes even include a sound-card option (makes a nice way to test/learn the software, and is good in class).
    eg Cleverscope claims this, but I have not tried it.
  • ercoerco Posts: 20,256
    edited 2015-02-16 16:12
    Turns out a Stamp does just fine with a variety of toy flutes and recorders, and there is plenty of potential to do more, like auto-harmony.
    '{$STAMP BS2}
    ' {$PBASIC 2.5}
    
    ' sample twice, discard if vastly different
    
    PAUSE 100
    
    Main:
        '
        COUNT 15,50,W1
        COUNT 15,50,W2
        IF W2<W1 AND W1-W2>3 THEN main ' discard if 2 samples are more than 3 apart
        IF W1<W2 AND W2-W1>3 THEN main ' discard if 2 samples are more than 3 apart
        W1=W1+W2 'update W1, now total ~similar to COUNT 100
        IF W1<30 THEN main   ' ignore LF noise & pauses
        W2=W1*10
    
        FREQOUT 9,500,W2
    
        IF W1>49 AND W1<55 THEN DEBUG "CCC",CR:GOTO main    ' 52-53
        IF W1>56 AND W1<62 THEN DEBUG "DDD",CR:GOTO main    ' 59
        IF W1>65 AND W1<68 THEN DEBUG "EEE",CR:GOTO main  ' 66-67
        IF W1>67 AND W1<75 THEN DEBUG "FFF",CR:GOTO main  ' 70
        IF W1>77 AND W1<82 THEN DEBUG "GGG",CR:GOTO main  '79-80
        IF W1>86 AND W1<93 THEN DEBUG "AAA",CR:GOTO main  '89-91
        IF W1>96 AND W1<103 THEN DEBUG "BBB",CR:GOTO main  ' 98-99
        IF W1>102 AND W1<109 THEN DEBUG " HIGH CCC",CR:GOTO main  '106-107
    
    GOTO main
    
  • ercoerco Posts: 20,256
    edited 2015-02-16 17:05
    Does OK on a piano, too. I think the FFT is a money laundering scam. :)
  • Bob Lawrence (VE1RLL)Bob Lawrence (VE1RLL) Posts: 1,720
    edited 2015-02-16 17:19
    re: An electronic square wave generator could make audio frequency tones that would be easier to detect,

    You can also use a Schmitt trigger http://en.wikipedia.org/wiki/Schmitt_trigger

    In electronics, a Schmitt trigger is a comparator circuit with hysteresis, implemented by applying positive feedback to the noninverting input of a comparator or differential amplifier. ** It is an active circuit which converts an analog input signal to a digital output** signal. The circuit is named a "trigger" because the output retains its value until the input changes sufficiently to trigger a change. In the non-inverting configuration, when the input is higher than a certain chosen threshold, the output is high. When the input is below a different (lower) chosen threshold, the output is low, and when the input is between the two levels, the output retains its value. This dual threshold action is called hysteresis and implies that the Schmitt trigger possesses memory and can act as a bistable circuit (latch or flip-flop). There is a close relation between the two kinds of circuits: a Schmitt trigger can be converted into a latch and a latch can be converted into a Schmitt trigger.

    Schmitt trigger devices are typically used in signal conditioning applications to remove noise from signals used in digital circuits, particularly mechanical switch bounce. They are also used in closed loop negative feedback configurations to implement relaxation oscillators, used in function generators and switching power supplies.


    Also by using a low pass filter you can set a range of frequencies that you want to detect.

    (Diagram on page 4)
    https://www.fairchildsemi.com/application-notes/AN/AN-140.pdf
  • ercoerco Posts: 20,256
    edited 2015-02-16 17:33
    An electronic square wave generator could make audio frequency tones that would be easier to detect,

    Yes, the backup plan was to use a 555 oscillator, but this is already working better than expected with the recorder flute, so I'm good to go. :)
  • kwinnkwinn Posts: 8,697
    edited 2015-02-16 19:00
    See, good to try the simple approach first. Sometimes it works.
  • ercoerco Posts: 20,256
    edited 2015-02-16 20:01
    Amen Brother Kwinn! It helps that I'm a lazy simpleton... :)
  • ercoerco Posts: 20,256
    edited 2015-02-25 18:22
    I may the only one who finds this thread interesting, but I'll keep sharing my experiments here nonetheless. :)
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' sample twice, discard if vastly different
    
    PAUSE 100
    
    Main:
        COUNT 15,50,W1
        COUNT 15,50,W2
        IF W2<W1 AND W1-W2>3 THEN main ' discard if 2 samples are more than 3 apart
        IF W1<W2 AND W2-W1>3 THEN main ' discard if 2 samples are more than 3 apart
        W1=W1+W2 'update W1, now total ~similar to COUNT 100
        IF W1<30 THEN main   ' ignore LF noise & pauses
        W2=W1*10
        FREQOUT 9,500,W2
        DEBUG DEC W2,CR
        GOTO main
    
Sign In or Register to comment.