Skip to content

Playing Audio Files

Westside Robotics edited this page Dec 26, 2022 · 5 revisions

Introduction

FTC Blocks can play short sounds on Robot Controller (RC) phones and Driver Station (DS) devices. Here's a general guide to this fun and useful feature.

A different FTC Wiki tutorial covers Text-to-Speech Telemetry that plays only on the DS phone or Driver Hub -- also a fun and useful feature.

Step 1 - Audio Files

Find or record your sound files. Blocks can play audio files in the popular .wav and .mp3 formats. Some short samples are posted here. Click a file name and then the Download button.

Most laptop computers can easily make voice recordings.

In Windows, the Sound Recorder program creates .wma files. Many free websites like this one can convert your existing audio file into .wav or .mp3 format.

Return to Top

Step 2 - My Sounds

In the top-level Blocks interface (list of OpModes), click on "Sounds" at the right side of the screen.

This open a new Chrome tab called My Sounds, where you can upload and manage sound files. Upload your .wav and/or .mp3 files from the computer. Use unique filenames; don't upload hello.wav and hello.mp3.

Return to Top

Step 3 - SoundPool Menu

Leave the My Sounds tab open, and return to the first Chrome tab. Open a Blocks OpMode (old or new). At the left side, look under Android, and SoundPool.

Return to Top

Step 4 - Initialize SoundPool

It's best to do this before waitForStart, to save time later.

Return to Top

Step 5 - Preload Sound

Optional to preload the sound file, which theoretically improves response for larger files. This is done with a Repeat Loop. Change the 'while' to 'until', and attach one of the preloadSound Blocks. There's one for the SoundResource class which (in 2019-2020) contains the memorable (!) Skystone sounds, and the other is for your own previously uploaded sound files.

Enter the exact audio filename. The basic preload Block has no pulldown list; check the My Sounds tab to be sure of the filename spelling.

This purple Block returns 'TRUE' when the preloading task is complete. At that point the 'Repeat Until' loop will end, and the OpMode will continue.

The loop can remain empty, unless you want to include telemetry re. the preloading status. In any case you might want telemetry afterwards to indicate completion.

For safety and convenience, add a secondary condition to the Repeat Until loop: OR isStopRequested. Don't use 'OR NOT opModeIsActive', since you haven't yet pressed START (to make the OpMode active).

Return to Top

Step 6 - Play Sound

Now add the play Block where desired. Again, enter the exact audio filename.

This begins playback only; the OpMode will continue immediately afterwards. If you require completion before moving on, determine how much timed pause is needed.

A timed pause is essential if a sound plays in a tight loop. The audio file will start repeatedly, often so fast that it sounds like static noise or an irritating staccato of continuous starts.

Even if a gamepad button triggers playback in a repeat loop, a pause is still needed. The loop cycles faster than a human can release the button, causing that staccato of repeated starts. A "backlog" of waiting playbacks can build up; the sounds may continue spilling out long after the button has been released. Eventually the queued files will all play, and the sounds will stop.

The timed pause can be done with sleep() as shown below, or with a custom timer. An FTC Wiki tutorial on Blocks timers is here.

Windows Explorer gives a handy estimate of playback duration, rounded to the nearest second.

Right-click on any column heading, and choose "Length" (not "Duration"). This helps when starting to determine the exact amount of timed pause needed by a sound clip.

Return to Top

Use in Development

Sounds can be a very useful tool for programming and debugging. They can mark the transition from one autonomous action to another, often very hard to see. Different sounds can indicate certain conditions, such as encoder ranges or sensor values, or different stages in the program. Sometimes the robot is executing an unexpected or unknown operation, easily identified with a unique sound.

Even simple counting values can be indicated with a number of beeps, or with recorded words "one", "two", "three", etc.

Sounds are most practical in a test environment, without the extreme noise of a tournament.

Return to Top

SoundPool Options

Review the optional green Blocks to adjust volume, rate and looping. Hover the cursor over each Block for instructions.

Make these settings before playing the audio file.

Return to Top

Other Notes

The sound plays back on both RC phone and DS device. Make sure the device audio volume is set high, not just the ringtone volume. Note that the REV Control Hub has no speakers.

The .stop command simply stops a currently playing sound file. It does not permanently disable SoundPool.

Sound files are stored on the RC device, in a folder named FIRST/blocks/sounds. Here you will see all the files from the My Sounds listing.

For spoken audio that plays only on the Driver Hub or DS phone, see this FTC Wiki tutorial on speech Telemetry.

Special note... you may find that Android does not load or play the audio files instantly, especially the first time. It takes some practice to determine a file's response characteristics; still there can be unpredictable system-related variation. Consider pre-playing key sounds, for instant response later.

Return to Top

Longer Playbacks

Some users have trouble playing audio files longer than about 6 seconds.

FTC Blocks uses Android's SoundPool which is intended for short audio clips, basically just sound effects. A different Android tool is the MediaPlayer class (not available in FTC Blocks) intended for longer audio files.

Here are two tips to 'stretch' your playing time, possibly up to 30 seconds:

  1. Record the audio in mono rather than stereo.

  2. Convert the file to .OGG format, which is supported in FTC Blocks.

Various websites offer free conversion to .OGG format, including at least this site also allowing audio channel conversion from stereo to mono. Sampling rate may help too; try 16,000 Hz.

For example, a 4-second .wav audio file was reduced from 376 Kb to 18 Kb in .OGG format, and played in FTC Blocks.

Some forums mention keeping the file size under 1 Mb, but apparently the actual limit is 1 Mb of buffer size (memory) which is not the same as file size. Over-limit cases are flagged in the log file. In any case SoundPool does not have an inherent time limit such as 6 seconds.

One FTC team wanted a theme song for autonomous. If the playing time can't be stretched enough, a last resort might be to break up the recording into several sections and play them in order. With great care this could be overlaid onto a completed/fine-tuned Linear OpMode autonomous. Or the autonomous could be re-written as a state machine in an Iterative OpMode, with the audio clips played in a timed sequence. Not so easy but maybe a good programming project! Learn about Blocks timers at this FTC Wiki tutorial.

Return to Top



Questions, comments and corrections to: westsiderobotics@verizon.net

Clone this wiki locally