Perfect Pitch: Unveiling the Mathematical Symphony Behind a Guitar Tuner

Rate this content
Bookmark
GithubProject website

Join me on a musical journey through the world of open source software as I share the story behind crafting from scratch an online guitar tuner with Javascript.

In this talk, we'll explore the delightful intersection of mathematics and development, where a simple curiosity led to the creation of an open-source tuner using React and Next.js.

I'll share insights and challenges into the implementation of the tuner, navigating through the algorithmic landscape of autocorrelation and frequency calculations.

This talk has been presented at React Summit 2024, check out the latest edition of this React Conference.

FAQ

Omar Job is the Technical Lead at Learn, an Italian company focused on making digital competencies accessible to everyone. He is also passionate about music and enjoys playing the guitar.

The guitar tuner project aims to explore the mathematics involved in pitch estimation and detection. It was created out of pure curiosity to see if the developer could build a guitar tuner from scratch.

The guitar tuner project is built using React and Next.js. It also utilizes the Web Audio API for audio processing and the MediaStream API to access the user's microphone.

Pitch detection involves identifying the frequency of a note being played, which is measured in hertz (Hz). Notes are represented by frequencies, and an octave doubles the frequency of the same note.

Zero crossing is an algorithm that counts the number of times a signal crosses the zero line. It is straightforward on a clear signal but not effective on noisy signals like those from a guitar.

Fast Fourier Transform (FFT) is a method that converts a signal from its original time domain to the frequency domain. It helps identify the frequency of a signal but can be complicated when dealing with noisy signals.

Autocorrelation is an algorithm that compares a signal with a shifted version of itself to identify periodic patterns. It is used to clear noisy signals and make them more readable for frequency calculation.

The project uses the MediaStream API to access user media like the microphone and the Audio API for decoding and processing audio signals.

The project uses the React Go chart library for data visualization. The frequency is passed as a prop to the library, which then displays an indicator that moves based on the frequency.

Potential improvements include resetting the indicator when no note is detected, improving noise cancellation and signal pre-processing, detecting chords instead of single notes, and studying other algorithms for pitch detection.

Omar Diop
Omar Diop
22 min
18 Jun, 2024

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This Talk provides an overview of an open-source guitar tuner project, covering topics such as pitch detection, web APIs for microphone access, implementation with React and XGS, and data visualization. It explores various pitch detection algorithms, including zero crossing, fast Fourier transform, and autocorrelation. The project utilizes the MediaStream API and Audio API to access user media. The implementation is done with React and Next.js, and includes a utility library for performing pitch estimation and detection. The Talk also discusses future improvements for the project, such as noise cancellation and chord detection.

1. Introduction to the Guitar Tuner Project

Short description:

I am Omar Job, the Technical Lead at Learn, an Italian company aiming to make digital competencies accessible to everyone. Today, we will have an overview of an open-source guitar tuner project. We will explore pitch detection and estimation, web APIs for microphone access, implementation with React and XGS, data visualization, and discuss issues and improvements. This project was born out of curiosity and aims to explore new territory. It's open source, built with React and extraJS, and utilizes the web audio API.

Hi, everyone. It's a pleasure to be here and I cannot wait to share with you this topic. So, let's start with the presentations. I am Omar Job and I'm Technical Lead at Learn, which is an Italian company which aims to make digital competencies accessible to everyone. In my free time, I have a lot of passions, one of which is music, and I love to bother my neighbors playing guitar. And that's why we are here.

So, today we're going to see a lot of things. We will have an overview about the project, which is an open source guitar tuner. We will see the basics of pitch detection and pitch estimation, so how to detect the frequency of a note that's played. And then we will see the web APIs involved in the project, so how to get access to a user's microphone. And we will see even the actual implementation with React and XGS. And we will see after that the data visualization part, so how to display the information that we are retrieving. And after that, finally, we will talk about issues and possible improvements.

I would like to start with a question that is, do we need another guitar tuner? Well, there's plenty of guitar tuners online. You can find a lot of applications and download them for free. So, which leads to another question, that is, why? Why did we come up with a guitar tuner again? Well, actually, I wanted to test myself and I was curious because I wanted to see what was under the hood. I wanted to see all the mathematics involved in pitch estimation and detection. And I want to see if I could build a guitar tuner from scratch. So, this is a project made out of pure curiosity. And disclaimer, I'm not a mathematician. This is the result of my researches and my curiosity. So, this is a project that aims to explore something that I did never explore.

The project is open source. You can find it online. You can check the code and I will share it with you after the talk so you can explore it. It's made with React and extraJS. And it uses the web audio API. The interface is pretty clear. It detects the note that is being played. And as you can see, the indicator moves when I play a note.

2. Basics of Pitch Detection and Algorithms

Short description:

The basics of pitch detection involve understanding that notes are represented by frequencies in hertz. A reference frequency, A440, is used for tuning. The guitar signal, represented by a wave, is often noisy, making analysis challenging. Various algorithms exist for pitch detection, including zero crossing.

It's very accurate. It can be improved, but for this stage, it's very, very good and it works very well. So, what are the basics of pitch detection?

Well, I want to align everyone on the topic because if you play an instrument, and I noticed that a lot of programmers do play instruments, but if you're not a musician, this topic can be tricky to understand. So, I want to align everyone on the topic. So, we start with the notes.

And I tell you that notes are represented by a frequency, which is measured in hertz. As you can see, in this table, we have a lot of notes. Every note is represented by a number, which is the frequency. On the top row, you can see the note names that go from C to B. And on the left column, you can see some numbers that go from 0 to 4. These numbers are octaves. An octave is the same note repeated, but with a different frequency, and each octave doubles the frequency of the note. In other words, if you go on the A column, on the A440, which is highlighted, you can see that if you go down with the rows in the column, the frequency is doubled.

So, the A440 is the reference frequency for tuning. So, a lot of tuners use this frequency to tune all the instruments, and this is A4, so the A in the octave 4. Guitar has six strings, so every string is played as a note. So, as you can see here, the six strings is an E note in the fourth octave. The first string, for instance, is an E in the second octave, and as you notice, the frequencies are very very different. Our goal is to detect the frequency that is being played, and tell the user which note he is playing in order to tune his guitar.

So, the guitar signal can be represented with a plot. So, in sound, the signal is represented by a wave. As you can see on the left, we have a clean signal, which is an A4, so A440 Hz. As you can see, it is periodic and very clear. On the right, you can see the guitar signal, which is not so clear, but can be very noisy and tricky to perform analysis on this kind of signal. This leads to our algorithms.

We have a lot of algorithms to perform pitch estimation and pitch detection. These are three main algorithms that I've studied, and I'm going to show you every algorithm step by step. I will point out the pros, the cons, and which one I used to perform the actual pitch detection in the project. We will start with zero crossing. Zero crossing starts with this kind of plot, so the clear signal.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

A Guide to React Rendering Behavior
React Advanced Conference 2022React Advanced Conference 2022
25 min
A Guide to React Rendering Behavior
Top Content
This transcription provides a brief guide to React rendering behavior. It explains the process of rendering, comparing new and old elements, and the importance of pure rendering without side effects. It also covers topics such as batching and double rendering, optimizing rendering and using context and Redux in React. Overall, it offers valuable insights for developers looking to understand and optimize React rendering.
Building Better Websites with Remix
React Summit Remote Edition 2021React Summit Remote Edition 2021
33 min
Building Better Websites with Remix
Top Content
Remix is a web framework built on React Router that focuses on web fundamentals, accessibility, performance, and flexibility. It delivers real HTML and SEO benefits, and allows for automatic updating of meta tags and styles. It provides features like login functionality, session management, and error handling. Remix is a server-rendered framework that can enhance sites with JavaScript but doesn't require it for basic functionality. It aims to create quality HTML-driven documents and is flexible for use with different web technologies and stacks.
Don't Solve Problems, Eliminate Them
React Advanced Conference 2021React Advanced Conference 2021
39 min
Don't Solve Problems, Eliminate Them
Top Content
Kent C. Dodds discusses the concept of problem elimination rather than just problem-solving. He introduces the idea of a problem tree and the importance of avoiding creating solutions prematurely. Kent uses examples like Tesla's electric engine and Remix framework to illustrate the benefits of problem elimination. He emphasizes the value of trade-offs and taking the easier path, as well as the need to constantly re-evaluate and change approaches to eliminate problems.
React Compiler - Understanding Idiomatic React (React Forget)
React Advanced Conference 2023React Advanced Conference 2023
33 min
React Compiler - Understanding Idiomatic React (React Forget)
Top Content
Watch video: React Compiler - Understanding Idiomatic React (React Forget)
Joe Savona
Mofei Zhang
2 authors
The Talk discusses React Forget, a compiler built at Meta that aims to optimize client-side React development. It explores the use of memoization to improve performance and the vision of Forget to automatically determine dependencies at build time. Forget is named with an F-word pun and has the potential to optimize server builds and enable dead code elimination. The team plans to make Forget open-source and is focused on ensuring its quality before release.
Using useEffect Effectively
React Advanced Conference 2022React Advanced Conference 2022
30 min
Using useEffect Effectively
Top Content
Today's Talk explores the use of the useEffect hook in React development, covering topics such as fetching data, handling race conditions and cleanup, and optimizing performance. It also discusses the correct use of useEffect in React 18, the distinction between Activity Effects and Action Effects, and the potential misuse of useEffect. The Talk highlights the benefits of using useQuery or SWR for data fetching, the problems with using useEffect for initializing global singletons, and the use of state machines for handling effects. The speaker also recommends exploring the beta React docs and using tools like the stately.ai editor for visualizing state machines.
Routing in React 18 and Beyond
React Summit 2022React Summit 2022
20 min
Routing in React 18 and Beyond
Top Content
Routing in React 18 brings a native app-like user experience and allows applications to transition between different environments. React Router and Next.js have different approaches to routing, with React Router using component-based routing and Next.js using file system-based routing. React server components provide the primitives to address the disadvantages of multipage applications while maintaining the same user experience. Improving navigation and routing in React involves including loading UI, pre-rendering parts of the screen, and using server components for more performant experiences. Next.js and Remix are moving towards a converging solution by combining component-based routing with file system routing.

Workshops on related topic

React Performance Debugging Masterclass
React Summit 2023React Summit 2023
170 min
React Performance Debugging Masterclass
Top Content
Featured WorkshopFree
Ivan Akulov
Ivan Akulov
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)
Concurrent Rendering Adventures in React 18
React Advanced Conference 2021React Advanced Conference 2021
132 min
Concurrent Rendering Adventures in React 18
Top Content
Featured WorkshopFree
Maurice de Beijer
Maurice de Beijer
With the release of React 18 we finally get the long awaited concurrent rendering. But how is that going to affect your application? What are the benefits of concurrent rendering in React? What do you need to do to switch to concurrent rendering when you upgrade to React 18? And what if you don’t want or can’t use concurrent rendering yet?

There are some behavior changes you need to be aware of! In this workshop we will cover all of those subjects and more.

Join me with your laptop in this interactive workshop. You will see how easy it is to switch to concurrent rendering in your React application. You will learn all about concurrent rendering, SuspenseList, the startTransition API and more.
React Hooks Tips Only the Pros Know
React Summit Remote Edition 2021React Summit Remote Edition 2021
177 min
React Hooks Tips Only the Pros Know
Top Content
Featured Workshop
Maurice de Beijer
Maurice de Beijer
The addition of the hooks API to React was quite a major change. Before hooks most components had to be class based. Now, with hooks, these are often much simpler functional components. Hooks can be really simple to use. Almost deceptively simple. Because there are still plenty of ways you can mess up with hooks. And it often turns out there are many ways where you can improve your components a better understanding of how each React hook can be used.You will learn all about the pros and cons of the various hooks. You will learn when to use useState() versus useReducer(). We will look at using useContext() efficiently. You will see when to use useLayoutEffect() and when useEffect() is better.
React, TypeScript, and TDD
React Advanced Conference 2021React Advanced Conference 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.
Web3 Workshop - Building Your First Dapp
React Advanced Conference 2021React Advanced Conference 2021
145 min
Web3 Workshop - Building Your First Dapp
Top Content
Featured WorkshopFree
Nader Dabit
Nader Dabit
In this workshop, you'll learn how to build your first full stack dapp on the Ethereum blockchain, reading and writing data to the network, and connecting a front end application to the contract you've deployed. By the end of the workshop, you'll understand how to set up a full stack development environment, run a local node, and interact with any smart contract using React, HardHat, and Ethers.js.
Mastering advanced concepts in TypeScript
React Summit US 2023React Summit US 2023
132 min
Mastering advanced concepts in TypeScript
Top Content
Featured WorkshopFree
Jiri Lojda
Jiri Lojda
TypeScript is not just types and interfaces. Join this workshop to master more advanced features of TypeScript that will make your code bullet-proof. We will cover conditional types and infer notation, template strings and how to map over union types and object/array properties. Each topic will be demonstrated on a sample application that was written with basic types or no types at all and we will together improve the code so you get more familiar with each feature and can bring this new knowledge directly into your projects.
You will learn:- - What are conditional types and infer notation- What are template strings- How to map over union types and object/array properties.