Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. friend: frequency to MIDI should be easy right?

friend: frequency to MIDI should be easy right?

Scheduled Pinned Locked Moved The Weird and The Wonderful
tutorialcomhardwareiothelp
23 Posts 9 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Rick York

    FFT is a good technique to use but I am not sure how well it will work in this situation - using a Pi. You can probably get by with testing for fundamentals up to about 4K. The reasoning is the low E on a 4-string bass is ~40Hz, 80 for a guitar. Five octaves above that would be 2560Hz so, according to Mr. Nyquist, you need to sample at 5.1KHz minimum. Probably 6KHz or 8KHz would be better. If you do FFTs on 256 samples you would need to do 32 of them per second to keep up and that would mean bursts of data would come every 30mS. You will need to use the fastest Pi available to keep up with that. I might be thinking about this wrongly though. There are a few free programs you can play around with to get an understanding of this. Here's one : Audio Spectrum Analyser[^] and here is another : Sound Frequency Analyzer[^]. I think there is source code for them available too.

    R Offline
    R Offline
    raddevus
    wrote on last edited by
    #12

    That's really great info in your post. I'm going to read it again even more slowly. The interesting thing is that this is not even running on an RPi. This is a MKR Zero which has a SAMD21 Cortex-M0+ 32bit low power ARM MCU (compared to RPi 3+ Quadcore ARM Cortex-A53, 64Bit)

    R 1 Reply Last reply
    0
    • R raddevus

      That's really great info in your post. I'm going to read it again even more slowly. The interesting thing is that this is not even running on an RPi. This is a MKR Zero which has a SAMD21 Cortex-M0+ 32bit low power ARM MCU (compared to RPi 3+ Quadcore ARM Cortex-A53, 64Bit)

      R Offline
      R Offline
      Rick York
      wrote on last edited by
      #13

      The main things in there are the frequency range of interest which is summarized well on this page: Table of Musical Notes and Their Frequencies and Wavelengths[^] and the Nyquist sampling theorem which basically means you have to sample at a minimum of twice the frequency of interest. This is why the sample rate of CD audio is 44.1KHz in order to reproduce frequencies of up to 20KHz. That sample rate caused some other major issues but it was the best technology could do in the early 1980s.

      J 1 Reply Last reply
      0
      • M msk4711

        The bit shifting is a tool from older times, when bit shifting was faster than a division by 32. The value you get is the arithmetic mean, but not the moving average.

        B Offline
        B Offline
        Bernhard Hiller
        wrote on last edited by
        #14

        msk4711 wrote:

        arithmetic mean

        That is, the variable sum has an appropriate name.

        Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

        1 Reply Last reply
        0
        • R Rick York

          The main things in there are the frequency range of interest which is summarized well on this page: Table of Musical Notes and Their Frequencies and Wavelengths[^] and the Nyquist sampling theorem which basically means you have to sample at a minimum of twice the frequency of interest. This is why the sample rate of CD audio is 44.1KHz in order to reproduce frequencies of up to 20KHz. That sample rate caused some other major issues but it was the best technology could do in the early 1980s.

          J Offline
          J Offline
          Jan Holst Jensen2
          wrote on last edited by
          #15

          Exactly. The code shown will give a readout of the DC (static) input voltage, it will not tell you anything about the tone signal. To sample the input at a decent frequency, forget about using analogRead(). It can't sample faster than about 9000 Hz. You will need to set the ADC in free-running mode and after the necessary code changes to support that, you then need to see how much RAM you have availble for sampling, and how fine-grained an FFT you can do on that given the limited processing power you have. Let's assume that you can get a fine-grained FFT. If you play a single string on the guitar it will be fairly simple to determine the base frequency and convert that to MIDI. Now strike a chord. Now you have six different strings that all produce base frequencies and overtones and you need to deconvolute them. To produce correct MIDI you have to do all this in real-time. The delay from string-played to MIDI-produced has to be below 10 ms if you want to use the MIDI signal to trigger a guitar synth or another instrument without noticeable delay. If you "only" want to record and can live with a fixed larger latency, fine, but you still have to be able to process all input in real-time. One of my friends has a home studio and he uses a piece of software that can analyze all kinds of instruments and e.g. split a guitar voice into the respective strings and display what is played as finger settings. Really awesome, but of course, it costs :-). You can buy dedicated hardware to plug into your guitar that outputs MIDI for around $100. Just saying :-)...

          1 Reply Last reply
          0
          • R raddevus

            It's a long convoluted story about how I finally wound up on the following code (below). It all started with a request from a friend:

            naive friend

            "Can't you make a device for me that analyzes tones (frequencies) and turns them into MIDI and outputs them to my computer so I can play the guitar and have it turned into MIDI? Should be easy right?"

            Me:

            Everything should be easy.

            Well, there was a official arduino example using a MKR 1000 that has an incorrect schematic[^]*. And from there, I was down the rabbit-hole. Okay, so now I was just trying to create a quick sound generator that I can use to input into the Analog 0 pin (A0) of my MKR Zero so I can test how the code works. I have a Grove Seeed Sound Sensor laying around so I find some sample code. However, there is no explanation of this odd piece of code and I have to look at it closely. Yes, it's a simple bitshift, but I had to think about what it does. Do you see what it does?**

            const int pinAdc = A0;

            void setup()
            {
            Serial.begin(115200);
            //Serial.println("Grove - Sound Sensor Test...");
            }

            void loop()
            {
            long sum = 0;
            for(int i=0; i<32; i++)
            {
            sum += analogRead(pinAdc);
            }

            sum >>= 5;
            
            Serial.println(sum);
            delay(10);
            

            }

            So it looks like it takes 32 samples each time through the loop and then it does that bit shift. Interesting. The code is from Grove - Sound Sensor[^] *Bonus Material The error in the schematic that is not in the breadboard example is that the schematic uses Amp2 OUT to go into the A0 pin on the Arduino even though there is no input on Amp2. The output should be from Amp1 to A0 on the schematic. See schematic at : https://www.arduino.cc/en/uploads/Tutorial/ArduinoMKR1000AudioInput_schem.png[^] ** spoiler Yep, it just clips off the

            S Offline
            S Offline
            StatementTerminator
            wrote on last edited by
            #16

            So have you realized yet that musical notes on the Western scale don't really correspond to any absolute frequencies? They're relatively tuned, to each other on the scale by ratio, not to a fixed frequency. Even if you assume that A=440, what temperament are you going to use? Just intonation won't work out right on a fretted instrument, you know. It's a complicated problem, aside from the programming. There are commercial products that do this, but they all involve compromises. Also, latency is a big problem with these things, so there's that too.

            R 1 Reply Last reply
            0
            • R raddevus

              It's a long convoluted story about how I finally wound up on the following code (below). It all started with a request from a friend:

              naive friend

              "Can't you make a device for me that analyzes tones (frequencies) and turns them into MIDI and outputs them to my computer so I can play the guitar and have it turned into MIDI? Should be easy right?"

              Me:

              Everything should be easy.

              Well, there was a official arduino example using a MKR 1000 that has an incorrect schematic[^]*. And from there, I was down the rabbit-hole. Okay, so now I was just trying to create a quick sound generator that I can use to input into the Analog 0 pin (A0) of my MKR Zero so I can test how the code works. I have a Grove Seeed Sound Sensor laying around so I find some sample code. However, there is no explanation of this odd piece of code and I have to look at it closely. Yes, it's a simple bitshift, but I had to think about what it does. Do you see what it does?**

              const int pinAdc = A0;

              void setup()
              {
              Serial.begin(115200);
              //Serial.println("Grove - Sound Sensor Test...");
              }

              void loop()
              {
              long sum = 0;
              for(int i=0; i<32; i++)
              {
              sum += analogRead(pinAdc);
              }

              sum >>= 5;
              
              Serial.println(sum);
              delay(10);
              

              }

              So it looks like it takes 32 samples each time through the loop and then it does that bit shift. Interesting. The code is from Grove - Sound Sensor[^] *Bonus Material The error in the schematic that is not in the breadboard example is that the schematic uses Amp2 OUT to go into the A0 pin on the Arduino even though there is no input on Amp2. The output should be from Amp1 to A0 on the schematic. See schematic at : https://www.arduino.cc/en/uploads/Tutorial/ArduinoMKR1000AudioInput_schem.png[^] ** spoiler Yep, it just clips off the

              E Offline
              E Offline
              englebart
              wrote on last edited by
              #17

              Bit shift 5 is the same as divide by 32. (only faster) It is taking 32 samples, and calculating the average.

              R 1 Reply Last reply
              0
              • S StatementTerminator

                So have you realized yet that musical notes on the Western scale don't really correspond to any absolute frequencies? They're relatively tuned, to each other on the scale by ratio, not to a fixed frequency. Even if you assume that A=440, what temperament are you going to use? Just intonation won't work out right on a fretted instrument, you know. It's a complicated problem, aside from the programming. There are commercial products that do this, but they all involve compromises. Also, latency is a big problem with these things, so there's that too.

                R Offline
                R Offline
                raddevus
                wrote on last edited by
                #18

                Right, I know there basically is a solid way to say XXX frequency is a particular note. I know that A=440Hz but then where do you go from there? And yes, you are also correct about the latency issue because you have to do so much sampling and there is noise to deal with and all the rest. It is an interesting experiment to try and to think about though.

                S 1 Reply Last reply
                0
                • E englebart

                  Bit shift 5 is the same as divide by 32. (only faster) It is taking 32 samples, and calculating the average.

                  R Offline
                  R Offline
                  raddevus
                  wrote on last edited by
                  #19

                  englebart wrote:

                  Bit shift 5 is the same as divide by 32. (only faster) It is taking 32 samples, and calculating the average.

                  Yes!! Nailed it on both accounts. It's a nice little piece of code from the sample. :thumbsup:

                  1 Reply Last reply
                  0
                  • R raddevus

                    Right, I know there basically is a solid way to say XXX frequency is a particular note. I know that A=440Hz but then where do you go from there? And yes, you are also correct about the latency issue because you have to do so much sampling and there is noise to deal with and all the rest. It is an interesting experiment to try and to think about though.

                    S Offline
                    S Offline
                    StatementTerminator
                    wrote on last edited by
                    #20

                    raddevus wrote:

                    I know there basically is a solid way to say XXX frequency is a particular note

                    Not really, or at least only if you make certain assumptions. From the perspective of music theory that isn't correct at all. Warning: below is a little clarification about technicalities and theory, but for the practical purposes of what you're doing you don't really need to take this into account for most cases. I think it's worth mentioning though while we're discussing notes and frequencies, because there are some misunderstandings here. And, yeah, I have a bit of a thing about this because it's a common misconception that bugs me. You can easily find tables of frequencies matched to notes on the scale, but they are lies told for practical purposes and convenience. In reality, there is no defined frequency for any note, there are just arbitrary standards that can be--and often are--ignored by musicians in practice. The A=440 thing is a relatively recent convention designed to make it easier for musicians to play together while being in tune with each other. It's just a convention, and it's far from universal. For the purposes of a MIDI pickup, though, it's safe to assume that A=440 unless the guitarist is into archaic or oddball tunings. That's the easy part, though. The real problem is that the Western scale is based on ratio intervals between notes rather than any absolute pitches (frequencies), and the intervals change with each key, which can require the notes themselves to change frequency to be perfectly in tune for a particular key. That is, E-flat in one key may be a slightly different frequency than E-flat in another key, if you want perfect consonance with the other notes on the scale. You might think that's crazy, but it's true, and it's the reason for things like blue notes in jazz. Microtonal instruments like voice, violin, trombone, etc. automatically adjust pitch for each key, because intonation is done by ear and your ear will tell you to adjust the intonation for each key, musicians do this automatically. But fretted and keyboard instruments are another story, they have discrete pitches that they can play with little wiggle room, and so the musician can't adjust on the fly and it's impossible to tune them so that they are in tune in every key. This is why every piano in the world is out of tune, always, and if you don't believe me ask a piano tuner. The only way to make a piano truly in tune is to re

                    R 1 Reply Last reply
                    0
                    • S StatementTerminator

                      raddevus wrote:

                      I know there basically is a solid way to say XXX frequency is a particular note

                      Not really, or at least only if you make certain assumptions. From the perspective of music theory that isn't correct at all. Warning: below is a little clarification about technicalities and theory, but for the practical purposes of what you're doing you don't really need to take this into account for most cases. I think it's worth mentioning though while we're discussing notes and frequencies, because there are some misunderstandings here. And, yeah, I have a bit of a thing about this because it's a common misconception that bugs me. You can easily find tables of frequencies matched to notes on the scale, but they are lies told for practical purposes and convenience. In reality, there is no defined frequency for any note, there are just arbitrary standards that can be--and often are--ignored by musicians in practice. The A=440 thing is a relatively recent convention designed to make it easier for musicians to play together while being in tune with each other. It's just a convention, and it's far from universal. For the purposes of a MIDI pickup, though, it's safe to assume that A=440 unless the guitarist is into archaic or oddball tunings. That's the easy part, though. The real problem is that the Western scale is based on ratio intervals between notes rather than any absolute pitches (frequencies), and the intervals change with each key, which can require the notes themselves to change frequency to be perfectly in tune for a particular key. That is, E-flat in one key may be a slightly different frequency than E-flat in another key, if you want perfect consonance with the other notes on the scale. You might think that's crazy, but it's true, and it's the reason for things like blue notes in jazz. Microtonal instruments like voice, violin, trombone, etc. automatically adjust pitch for each key, because intonation is done by ear and your ear will tell you to adjust the intonation for each key, musicians do this automatically. But fretted and keyboard instruments are another story, they have discrete pitches that they can play with little wiggle room, and so the musician can't adjust on the fly and it's impossible to tune them so that they are in tune in every key. This is why every piano in the world is out of tune, always, and if you don't believe me ask a piano tuner. The only way to make a piano truly in tune is to re

                      R Offline
                      R Offline
                      raddevus
                      wrote on last edited by
                      #21

                      That's a great read but my original statement was a mistake:

                      raddevus wrote:

                      I know there basically is NOT a solid way to say XXX frequency is a particular note

                      I left out the word not. Oops. :-D I learned a lot about pitches, instruments, tambre from my time at Berklee College of Music and from David L Burge (Perfect Pitch Ear Training SuperCourse: Name EXACT Notes by Ear.[^]).

                      S 1 Reply Last reply
                      0
                      • R raddevus

                        That's a great read but my original statement was a mistake:

                        raddevus wrote:

                        I know there basically is NOT a solid way to say XXX frequency is a particular note

                        I left out the word not. Oops. :-D I learned a lot about pitches, instruments, tambre from my time at Berklee College of Music and from David L Burge (Perfect Pitch Ear Training SuperCourse: Name EXACT Notes by Ear.[^]).

                        S Offline
                        S Offline
                        StatementTerminator
                        wrote on last edited by
                        #22

                        Haha! Well, you gave me an excuse to go on a rant about even temperament, so that was fun at least :)

                        R 1 Reply Last reply
                        0
                        • S StatementTerminator

                          Haha! Well, you gave me an excuse to go on a rant about even temperament, so that was fun at least :)

                          R Offline
                          R Offline
                          raddevus
                          wrote on last edited by
                          #23

                          StatementTerminator wrote:

                          Well, you gave me an excuse to go on a rant about even temperament,

                          Yes and it is a great read. Thanks again for writing it up. :thumbsup::thumbsup::thumbsup:

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups