Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Rust Programming By Example

You're reading from   Rust Programming By Example Enter the world of Rust by building engaging, concurrent, reactive, and robust applications

Arrow left icon
Product type Paperback
Published in Jan 2018
Publisher Packt
ISBN-13 9781788390637
Length 454 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Antoni Boucher Antoni Boucher
Author Profile Icon Antoni Boucher
Antoni Boucher
Guillaume Gomez Guillaume Gomez
Author Profile Icon Guillaume Gomez
Guillaume Gomez
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Basics of Rust 2. Starting with SDL FREE CHAPTER 3. Events and Basic Game Mechanisms 4. Adding All Game Mechanisms 5. Creating a Music Player 6. Implementing the Engine of the Music Player 7. Music Player in a More Rusty Way with Relm 8. Understanding FTP 9. Implementing an Asynchronous FTP Server 10. Implementing Asynchronous File Transfer 11. Rust Best Practices 12. Other Books You May Enjoy

Decoding MP3 files


We'll start this chapter by learning how to decode an MP3 file to a format suitable to be played by the operating system using the simplemad crate, a binding for libmad.

Adding dependencies

Let's add the following to Cargo.toml:

crossbeam = "^0.3.0"
pulse-simple = "^1.0.0"
simplemad = "^0.8.1"

We also added the pulse-simple and crossbeam crates because we'll need them later. The former will be used to play the songs with pulseaudio and the latter will be used to implement the event loop of the music player engine.

We also need to add the following statements in main.rs:

extern crate crossbeam;
extern crate pulse_simple;
extern crate simplemad;

mod mp3;

In addition to the extern crate statements, we have a mod statement since we'll create a new module for the MP3 decoder.

Implementing an MP3 decoder

We're now ready to create this new module. Create a new mp3.rs file with the following content:

use std::io::{Read, Seek, SeekFrom};
use std::time::Duration;

use simplemad;

We start...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime