กลับไปหน้ารวมไฟล์
play-polyphonic-tones-c65443-en.md

Project Overview

The "Sync-Harmonizer Polyphonic Engine" is a sophisticated audio project that breaks the "One-Note-at-a-Time" barrier of the standard Arduino tone() function. By leveraging the MusicWithoutDelay library alongside the hardware-timer-based Tone library, this system allows an Arduino Uno to play dual-voice melodies simultaneously. It utilizes the RTTL (Ring Tone Transfer Language) format—famed for its use in classic Nokia phones—to store complex musical scores in memory and play them back without ever blocking the main code execution. This is a vital study in interrupt-driven audio, state machine logic, and frequency modulation.

Video

Download Library

This project is a demonstration for my MusicWithoutDelay library which can be downloaded from here.

You will also need Bhagman's famous Tone library.

Why?

  • I created the library because there was no way to play classic 8-bit music while your main sketch was running. Also, reading other people's music files were hard and disorganized.
  • I want to play more than one note at the same time. After a lot of research I gained this ability through Bhagman's Tone library. Unfortunately, the number of notes that can be played at the same time depends on how many timers your board has(Can't use Timer0 since millis() uses it). So the Arduino Uno can play up to 2 notes at same time.

Since no one made a library to make music easier to play and read, I decided to make my own library.

How does it work?

It uses my MusicWithoutDelay library to convert the songs into readable durations for Bhagman's Tone library to play.

The song files use the famous RTTL(Ring Tone Transfer Language) format that old Nokia phones used for their ringtones.

  • To learn how to read and make your own music, learn RTTL in the ReadMe file of my library
  • RTTL is an extremely easy language to understand for musicians and newbies

These song files are stored in char pointers that you write before the setup(). Then, my library takes the song file and converts them into milliseconds. Remember the BlinkWithoutDelay sketch? My library uses a similar technique to play the notes.

Song files are stored in char *song

Technical Deep-Dive

  • The Polyphony Challenge (Timer Allocation):
    • Hardware Timer Constraints: Pulse generation for specific frequencies requires precise timing. The Arduino Uno (ATmega328P) has three timers: Timer0 (8-bit), Timer1 (16-bit), and Timer2 (8-bit).
    • Resource Management: Since Timer0 is reserved for millis() and delay(), the Tone library utilizes Timer1 and Timer2 to drive two independent output pins. This project manages these two hardware resources to play harmonized notes or separate bass/melody lines concurrently.
  • Non-Blocking "Blink-Without-Delay" Logic:
    • Standard music players use delay() between notes, which freezes the processor. The MusicWithoutDelay library implements a State Machine. It checks the current time versus the nextNoteTime during every loop() iteration. If the time has elapsed, it updates the frequency (Hz) and moves to the next character in the RTTL string.
    • This architecture allows the system to read sensors, update an RGB LED, or respond to button interrupts while the music plays with jitter-free precision.
  • RTTL (Ring Tone Transfer Language) Parsing:
    • Protocol Structure: An RTTL string consists of three parts: Name, Settings (defining default duration d, scale o, and BPM b), and Note Data.
    • Parsing Arithmetic: The library converts the octave and note name into a frequency index. For example, the note 4c (Middle C) is converted to 261.63Hz. The duration is calculated using the formula: $Duration (ms) = (60000 / BPM) \times (4 / NoteValue)$.
  • Synesthetic Visual Feedback:
    • The project integrates an RGB LED that is programmatically synced to the frequencies being played. By mapping the frequency range (31Hz to 4978Hz) to a color spectrum (using PWM), the device provides a visual representation of the harmonics, effectively creating a "Color-Organ" effect.

Engineering & Operation

  • The "Play-Back-Time" Engine: Unlike static players, the MusicWithoutDelay library enables interactive playback. Because it uses pointers to the RTTL string, the system can reverse the melody in real-time or skip to specific segments (verse/chorus) by manipulating the string index.
  • Resonant Speaker Interfacing: A 220-ohm resistor is used in series with the 8-ohm speaker to limit the current draw from the Arduino digital pins (keeping it safely under 40mA). For higher volumes, the developer recommends using an LM386 audio amplifier or a MOSFET-based driver to prevent thermal stress on the ATmega328P.
  • Interface Controls: Three tactile buttons are programmed for:
    1. Play/Pause: Halting the timer interrupts.
    2. Song Selection: Cycling through a list of RTTL pointers stored in Flash memory.
    3. Reverse Mode: Swapping the increment/decrement logic of the parser.
  • Industrial Application: Beyond 8-bit music, this polyphonic technique is critical for Industrial Alarm Systems that need to produce distinct, multi-tone "Urgency" signals while still monitoring thermal or pressure sensors for critical safety data.

Advantages

  • Play more than one note at the same time
  • Run other things like Serial monitor in the background
  • Play music backwards and forwards
  • Pause/play Music
  • Skip to specific sections within the song
  • Choose a new Song
  • more to come :D

Elevate your project's auditory experience: Harmonized melodies meets non-blocking multitasking.

ข้อมูล Frontmatter ดั้งเดิม

apps:
  - "1x Arduino IDE"
  - "MusicWithoutDelay Library"
  - "Tone Library (by bhagman)"
author: "nathan_ramanathan"
category: "Audio & Sound"
components:
  - "1x Speaker: 0.25W, 8 ohms"
  - "1x Arduino UNO"
  - "1x vibrating motor"
  - "2x Resistor 221 ohm"
  - "3x Pushbutton switch 12mm"
  - "1x Jumper wires (generic)"
  - "1x Breadboard (generic)"
  - "1x RGB Diffused Common Cathode"
description: "This project is using my MusicWithoutDelay library to play multiple sounds at the same time."
difficulty: "Intermediate"
documentationLinks: []
downloadableFiles: []
encryptedPayload: "U2FsdGVkX18tD71V2qe4YUZ2SXyPCNRs4rH3J0dQOz1PJq8mZWREOmnHADHPWlfa50SecE7fou3BeCizJQCnVFPAyIbyW4eZVUv1oNbr7Vg="
heroImage: "https://cdn.jsdelivr.net/gh/bigboxthailand/arduino-assets@main/images/projects/play-polyphonic-tones-c65443_cover.jpg"
lang: "en"
likes: 23
passwordHash: "5eb5c09b58f7eb23184fa602c26d9f69a274f69241dc79a0f0d2d8478b1df123"
price: 299
seoDescription: "Play polyphonic tones on Arduino using the MusicWithoutDelay library to output multiple sounds simultaneously."
tags:
  - "polyphonic"
  - "rttl"
  - "audio-engineering"
  - "non-blocking"
  - "music-theory"
title: "Play Polyphonic Tones!"
tools: []
videoLinks:
  - "https://www.youtube.com/embed/uoHhlrqZYDI"
views: 39598