Wireless Music

Wireless Music

Using an ESP32 and A2DP Wireless Transfer to Bluetooth

Over the years, music has been performed on vinyl records, tapes, and CDs, on radios, boomboxes, and smartphones—and now in mp3 format, forged from our telephones to ear buds, Alexa, and moveable Bluetooth audio system. This month, Jeff makes use of an ESP32, alternately in Wi-Fi and Bluetooth modes, to emulate the musical chimes of a grandfather clock, and sends the music recordsdata to a Bluetooth speaker utilizing A2DPStream. He provides a tiny OLED show for visible suggestions.

Rock and Roll originated from African American music, and was derived from or influenced by jazz, rhythm and blues, boogie-woogie, electrical blues, gospel, and nation music. The style didn’t purchase its title till the Nineteen Fifties. The beat is basically a dance rhythm with an accentuated again beat, and is normally performed with a drum set and a number of electrical guitars. American youngsters found rock and roll due to AM radio and the flexibility to buy vinyl records. It demanded quantity. Naturally, a guardian’s hottest phrase grew to become, “Turn that down!”

The 45rpm file grew to become the preferred strategy to buy your favourite music. All 45s had been monaural. Albums (33rpm) had been additionally monaural. Stereo albums introduced on the demise of mono in 1968, and a increase to part audio techniques. Component audio techniques had been Hi-Fi (High Fidelity) and costly. Japan countered with the moveable radio and cassette participant/recorder. The tape cassette might file music from the radio and you could possibly replay the taped copy any time you needed. Can you say inventive rights?

A “boombox” is a transistorized moveable music participant that includes a mixture of a number of cassette tape gamers/recorders, AM/FM radio, and a CD participant. Sound is delivered by an amplifier and two or extra built-in loudspeakers. A boombox is often able to unusually excessive quantity. Designed for portability, boomboxes had been launched to the American market throughout the late Nineteen Seventies. The want for louder and heavier bass led to greater and heavier bins; by the Eighties, some boomboxes had reached the scale of a suitcase.

The boombox rapidly grew to become related to city society within the United States, significantly African American and Latino youth. The boombox grew to become intently linked to American hip hop tradition, and was instrumental within the rise of hip hop music. A younger LL Cool J together with his boombox (“I can’t reside with out my radio”) is proven in Figure 1.

Figure 1 
 Before NCIS, Rap Star LL Cool J linked his music with the Boom Box— a huge stereo that one blasted from one's shoulder—. you shared your sound with everyone, whether they wanted to hear it or not. Photo Source: Sugar Barons [1].

Figure 1 
 Before NCIS, Rap Star LL Cool J linked his music with the Boom Box— a huge stereo that one blasted from one's shoulder—. you shared your sound with everyone, whether they wanted to hear it or not. Photo Source: Sugar Barons [1].

Figure 1
Before NCIS, Rap Star LL Cool J linked his music with the Boom Box— an enormous stereo that one blasted from one’s shoulder—. you shared your sound with everybody, whether or not they needed to listen to it or not. Photo Source: Sugar Barons [1].

Today, our telephones are our music gamers, whether or not the music is from our digital library or streamed from a service. We have our tunes proper on our cellphone and not want to hold round a separate piece of kit. In truth, you’ll discover many individuals with (Bluetooth) ear buds of their ears, and for probably the most half you may’t inform what they’re listening to. Our world has gone from harmful decibels to the “Sounds of Silence.”

But wait! We have a brand new phenomenon—the Bluetooth Speaker. Big sound, in just a little field. Just like casting your music to ear buds, you may forged to a Bluetooth speaker and fill the room with music for everybody. Many of those audio system are moveable, which implies you may take them into public locations; nevertheless, to date, I’ve not seen this “BT Blaster” being utilized in an invasive approach. If you could have an Amazon Alexa, you may even forged out of your cellphone to “her.”

This month’s challenge will use the ESP32 in Wi-Fi and Bluetooth modes. Since there is just one transmitter, we will’t use them collectively, however we will alternate between them. I wish to emulate the chiming of a Grandfather Clock with out the clockwork mechanism. We’ll begin off utilizing the Wi-Fi to sync up the time. For this I’ll be utilizing the NTPClient library together with the WiFi library to deal with the time synchronization. While not obligatory for this challenge, I’ll add the tiny show for some visible suggestions. This info may be despatched out the serial port as effectively, if you wish to depart off the show. I ship the recordsdata to a Bluetooth speaker with A2DPStream (Advanced Audio Distribution Profile), a wi-fi switch of high-quality audio indicators through Bluetooth

ESP32 AS A BLUETOOTH SOURCE

Let’s take a unique tack this month, through the use of a stream chart to explain the appliance. Code for this challenge is offered on the Circuit Cellar Article Code and Files webpage. Figure 2 exhibits the everyday stream for an Arduino program. This consists of some defining and a setup() operate adopted by a steady loop() operate. The setup() operate handles the initialization of all of the features that shall be used within the software. The one which stands out right here is the syncTime() operate. In this operate we’ll connect with the native WAN and use its entry to the Internet to contact the NTP (Network Time Protocol) server. NTP time is the present Epoch or the variety of seconds since January 1, 1900. This is in reference to GMT (Greenwich England Mean Time). TimeShopper.replace() makes use of this and timeClient.setTimeOffset(5 * 360000) to calculate the Epoch in your time zone.

Figure 2
These flow charts expand the minimum Arduino sketch into an application to display the current time based on the NTP (Network Time Protocol), used to sync this application with the world.

Figure 2
These flow charts expand the minimum Arduino sketch into an application to display the current time based on the NTP (Network Time Protocol), used to sync this application with the world.

Figure 2
These stream charts increase the minimal Arduino sketch into an software to show the present time primarily based on the NTP (Network Time Protocol), used to sync this software with the world.

I exploit EST, which is 5 hours * 360,000 (seconds in an hour) behind GMT. Until we do one other TimeShopper.replace(), any features comparable to timeClient.getHours() will use the final Epoch calculated and alter the response primarily based on the millis() operate of the processor. So ultimately, the time reported will drift barely.

You could make one other replace any time you want to resync the “precise” time. In truth usually you’ll put the operate timeClient(replace() in your loop() operate. This would constantly examine the timeClient.setUpdateInterval(time in ms) operate and name TimeShopper.replace() if the time between syncs exceeds the interval you’ve set. Since I wish to share the Wi-Fi and Bluetooth, I’ll be dealing with this another way. Once we’ve a TimeShopper.replace() within the syncTime() routine, the NTPClient and Wi-Fi are shut all the way down to unlock the RF transmitter. The syncTime() operate is our on-demand Wi-Fi operate.

The loop operate has just one objective—to concentrate to the seconds and name newSecond() operate when the second modifications. Once a second has modified, the newSecond() operate will replace the OLED, principally, displaying the time. This operate clears the show buffer, and writes the “signalOn” message—which is our software file title and date on strains 1 and a pair of, adopted by the point on line 3—to the show buffer. The show.show() operate strikes the buffer to the show.

The solely routine of curiosity right here could be the printCenteredText() operate. When a textual content string is to be written to the show, you may place it anyplace. I’m utilizing “row” and “column” positions to place the cursor earlier than writing the textual content. Since a personality’s peak (together with a clean line beneath) we’ll use 10 (pixelsPerRow), so the row place shall be: (row – 1) x 10. We need the column place to be indented one-half the show width minus textual content size. Character width (together with a clean column) is 6 (pixelWidthPerCharacter).

EVENTS

Although we will present down-to-the-second occasions, this software will solely examine for hour and quarter-hour occasions. In basic, a grandfather clock will “bong” with the suitable variety of strikes for every hour, with AM and PM hours the identical. In addition, previous granddad can carry out quarterly chimes, the preferred of which is Westminster Chime (Figure 3). These 4 notes had been first performed in Great St Mary’s Church within the heart of Cambridge, England, and never within the clock tower (now often known as the Elizabeth Tower) that homes Big Ben. Big Ben is definitely a nickname for a bell contained in the well-known clock tower (Figure 4).

Figure 3
Only four notes (bells) are needed to play all four Westminster Quarters. A slightly different pattern is used to indicate each quarter hour. Photo Source: Society for Music Theory [3].

Figure 3
Only four notes (bells) are needed to play all four Westminster Quarters. A slightly different pattern is used to indicate each quarter hour. Photo Source: Society for Music Theory [3].

Figure 3
Only 4 notes (bells) are wanted to play all 4 Westminster Quarters. A barely totally different sample is used to point every quarter hour. Photo Source: Society for Music Theory [3].
Figure 4
While most think of “Big Ben” as the actual clock tower in Westminster, London, it is in fact the nickname of the largest bell in the tower. The smaller bells form the four notes of the Westminster Quarters, while Ben announces the hour. Photo Source: Wikipedia [4]

Figure 4
While most think of “Big Ben” as the actual clock tower in Westminster, London, it is in fact the nickname of the largest bell in the tower. The smaller bells form the four notes of the Westminster Quarters, while Ben announces the hour. Photo Source: Wikipedia [4]

Figure 4
While most consider “Big Ben” because the precise clock tower in Westminster, London, it’s in reality the nickname of the most important bell within the tower. The smaller bells kind the 4 notes of the Westminster Quarters, whereas Ben declares the hour. Photo Source: Wikipedia [4]

We wish to play the suitable sound in sync with the actual time. To do that, we have to monitor the hours, minutes, and seconds. The predominant loop() already is dealing with the altering of seconds. Let’s add some code in loop() after the operate newSecond() to examine for zero seconds:

lastSeconds = mySeconds;

if(mySeconds == 0)

{

      examineForEvent();

}

A stream chart for the brand new operate examineForEvents() is proven in Figure 5. I’m utilizing the swap…case management construction to find out when minute equals one of many 4 circumstances for minutes 0, 15, 30, or 45. For the OLED, I’ll show Hour, Quarter Hour, Half Hour, and Three-Quarter Hour on the fifth line of the show.

Figure 5
Once we have the real time, we can use the time to "checkForEvents."  Our events will play an MP3 audio file through an external Bluetooth speaker.  We need to test for the beginning of each quarter hour.  On the hour and each quarter hour, we play the appropriate audio file.

Figure 5
Once we have the real time, we can use the time to "checkForEvents."  Our events will play an MP3 audio file through an external Bluetooth speaker.  We need to test for the beginning of each quarter hour.  On the hour and each quarter hour, we play the appropriate audio file.

Figure 5
Once we’ve the actual time, we will use the time to “examineForEvents.” Our occasions will play an MP3 audio file by an exterior Bluetooth speaker. We want to check for the start of every quarter hour. On the hour and every quarter hour, we play the suitable audio file.

In addition, after the zero-minute department, I’ll use one other swap…case management construction to find out the hour. There are 24 circumstances on this operate, hours 1-24. For the OLED, I’ll show the Hour on the fourth line of the show (Figure 6). Our software will now connect with the native WAN and sync the time. The OLED show ought to be updating the time each second. Now that we’ve an executing framework, we wish to ship music recordsdata to a Bluetooth speaker on command.

Figure 6
The optional OLED display is used to display the program name, program date, actual time,  last hour identified, last quarter hour identified, and status of the MP3.

Figure 6
The optional OLED display is used to display the program name, program date, actual time,  last hour identified, last quarter hour identified, and status of the MP3.

Figure 6
The non-obligatory OLED show is used to show this system title, program date, precise time, final hour recognized, final quarter hour recognized, and standing of the MP3.
SD CARD HOLDS MP3 FILES

To use the playMusic() operate, some setup related to dealing with the SD card and Bluetooth routines is critical for transferring a file to a Bluetooth speaker. The recordsdata will come from an SD card. I used my PC to collect the sound recordsdata I’m interested by. There are 4 fundamental recordsdata: Hour.mp3, QuarterHour.mp3, HalfHour.mp3, and ThreeQuarterHour.mp3. Actually, the Hour.mp3 file must be 24 Hour recordsdata, Hour1.mp3 by Hour12.mp3, each a.m. and p.m. These comprise the hourly Westminster hour chime plus the right variety of gongs to point the hour. Note that different chimes can be utilized rather than the Westminster chime. This might be any mp3 you need, from music to sound results. Just rename your file with the corresponding time you want it to play.

The SD card is related utilizing the SPI, that’s six connections, together with energy and floor. The SD card has been formatted utilizing the FAT (File Allocation Table) file format and the recordsdata copied into the foundation folder. Let’s set this up and confirm that the SD card you set into the cardboard slot has the suitable file on it. We use the default SPI pins for the ESP32.

As proven within the Figure 7 stream diagram, the checkSD() routine opens up the foundation listing on the SD card and will get all of the file names on this listing. After every filename has been retrieved from file.openNext() and file.getName(), the examineFiles() routine is named. The Boolean arrays myHoursFileDiscovered[] and myQuarterFileDiscovered[] are initialized as all false, which means this specific file has not been discovered on the SD card. These arrays discuss with the myHoursEvents[] and myQuarterEvents[] arrays that maintain all of the MP3 filenames. The examineFiles() routine compares the filename discovered on the SD card to every of the MP3 recordsdata within the ‘occasion arrays. If a match happens, then that filefound array is marked as true.

Figure 7
The audio files that I gathered were written to the SD card by my PC.  At the beginning of the application we will "checkSD" for each file, to make sure all the files we need are on the SD card.

Figure 7
The audio files that I gathered were written to the SD card by my PC.  At the beginning of the application we will "checkSD" for each file, to make sure all the files we need are on the SD card.

Figure 7
The audio recordsdata that I gathered had been written to the SD card by my PC. At the start of the appliance we’ll “checkSD” for every file, to verify all of the recordsdata we’d like are on the SD card.

When all of the recordsdata have been discovered on the SD card, we name examineForAllFile() routine, which checks to see if the two filefound arrays comprise all true knowledge. If all are true, all of the recordsdata are on the SD card, and we’re executed . If any recordsdata are lacking, it studies the lacking recordsdata and halts execution.

ESP32 BLUETOOTH

Finally, we will add the audio portion of the appliance. An enormous due to Phil Schatzmann for his Arduino-Audio-Tools libraries [2]. We point out that the “supply” of our audio will come from SD recordsdata. The audio “out” is thru the A2DPStream (Advanced Audio Distribution Profile), a wi-fi switch of high-quality audio indicators through Bluetooth. The audio makes use of the open supply MP3DecoderHelix “decoder”. This is all accessed by the finale setup command:

EncodedAudioStream decoder(&out, new MP3DecoderHelix());

The examineForEvent() operate is liable for in search of every new quarter hour (:00:00, :15:00, :30:00, and :45:00 minutes. There are particular mp3 recordsdata to play on the quarter hour. On the hour (00:00) we search for every hour, 00:00:00 – 23:00:00, and play a unique mp3 for every hour. To play the mp3, myMusic is ready to the suitable mp3 file title, for instance, “Hour1am.mp3.” The playMusic() operate handles grabbing the file from the SD card, decoding the information, and buffering, so it may be despatched out to the Bluetooth speaker.

To keep away from utilizing interrupts, playMP3.copy() have to be positioned within the Loop() operate, in order that the buffer may be up to date when obligatory till the file has been fully transmitted.

WHAT NOW

Once you could have the fundamental software operating, you’ll have the orchestral ambiance of a mallet percussionist, rapping out the Westminster Quarters of Big Ben as for those who had been admiring the Elizabeth Tower belfry within the Palace of Westminster. With a fast swap of the SD card, you may change the venue to the Black Forest space in southwestern Germany, the place the cuckoo clock has change into a cultural icon. Don’t have a penchant for the cuckoo? How about enjoying a unique chook name each hour?

HAND PROTOTYPING

I’m just a little shocked that Arduino not provides protect prototyping boards within the UNO and Mega kind issue. I purchase Arduino proto boards by the dozen from Amazon or ebay. For many tasks, I attempt to use one in all these, since you will get some cheap enclosures which might be made for this manner issue (Figure 8). Even although the indicators for an I2C show and a SPI SD card reader could be commonplace, the precise wiring will rely on the ESP32 module you choose. The largest dev package I’ve seen is 38-pins, and the smallest is 14-pins. Nearly each module helps I2C and SPI.

Figure 8
This $5 enclosure is made for the Arduino UNO, but the proto board fits fine.  Note that my USB cable connector is centered, so I needed to nip off a bit plastic between the USB and power holes in the enclosure.  A window can be cut in the enclosure top, so the OLED is exposed.

Figure 8
This $5 enclosure is made for the Arduino UNO, but the proto board fits fine.  Note that my USB cable connector is centered, so I needed to nip off a bit plastic between the USB and power holes in the enclosure.  A window can be cut in the enclosure top, so the OLED is exposed.

Figure 8
This $5 enclosure is made for the Arduino UNO, however the proto board suits fantastic. Note that my USB cable connector is centered, so I wanted to nip off a bit plastic between the USB and energy holes within the enclosure. A window may be minimize within the enclosure high, so the OLED is uncovered.

Because virtually each software may be powered by USB, you solely have to wire up 4 connections for the show and 6 connections for the SD card interfaces. The ESP32 module I used has each energy and person LEDs on board. I toggle the person LED throughout the once-a-second routine to present me a coronary heart beat. Although the show is up to date every second, for those who depart it off, you’ll need some form of affirmation that the appliance isn’t caught in a routine someplace.

Most SD playing cards are of enough dimension to carry tons of music recordsdata. With my recordsdata simply seconds lengthy, there’s lots room for different units of recordsdata. You might simply place totally different units of recordsdata in numerous folders and choose which folder you want to use, as an example /Westminster and /Cuckoo.

I’m additionally consistently shocked by the pliability of the ESP32. It appears as if many producers agree, as a result of they’ve included this system of their product lineups. Each has included particular peripherals comparable to a digital camera, SD card socket, show, and lithium charger, together with the usual interfaces. The Arduino IDE actually makes utilizing the ESP32 modules a snap. So dream just a little, then go create. Too a lot to do, so little time. 

REFERENCES
[1] – Sugar Barons, The Boombox Project, April 6, 2011:
https://net.archive.org/net/20111018085325/http://www.sugarbarons.com/?p=3553
[2] – Phil Schatzmann’s Arduino-Tools libraries:
https://github.com/pschatzmann/arduino-audio-tools
[3] – Daniel Harrison, “Tolling Time.” Music Theory Online 6:4, October, 2000: https://www.mtosmt.org/points/mto.00.6.4/mto.00.6.4.harrison.html
[4] – Wikipedia, “Big Ben”:
https://en.wikipedia.org/wiki/Big_Ben#/media/File:Big-ben-1858.jpg

Code and Supporting Files

PUBLISHED IN CIRCUIT CELLAR MAGAZINE • JULY 2024 #408 – Get a PDF of the difficulty

Sponsor this Article

Jeff Bachiochi (pronounced BAH-key-AH-key) has been writing for Circuit Cellar since 1988. His background contains product design and manufacturing. You can attain him at: [email protected] or at: www.imaginethatnow.com.

Vinyl

through Circuit Cellar https://ift.tt/18aysAc

February 6, 2025 at 02:26PM