How play mp3/audio files in Racket?

Viewed 218

I intend on making a cli audio player for racket, as an exercise to learn Racket, and everything else that would entail this project. I am stuck though how to begin. I can't find any package to play sound files, so I am guessing I may have to make one. How would I go about it?

2 Answers

What you probably want is #lang video (website). It provides a high level interface into audio playback. Allowing you to do something like:

#lang video
(clip "file.mp3")

Since you want to make a little command line player you might also want to take a look at its small preview tool.

I ended doing this the hackish way by calling a shell script via racket, not ideal at all. For reference, putting the code here.

; This creates the initail rsound
; for a song, this rsound is passed around
; so the whole song doesn't have to be
; decoded from the file everytime.
(define (play filepath)
  (cond [(string=? "mp3" (last (regexp-split #rx"\\." filepath)))
         (system* "./mp3-hack" filepath)
         (set! filepath "curr.wav")])
  (define input-pstream (make-pstream))
  (define input-rsound (rs-read filepath))
  (pstream-play input-pstream input-rsound)
  (values input-pstream input-rsound filepath))

And the mp3-hack file just uses ffmpeg

#!/bin/sh
ffmpeg -i $1 -acodec pcm_s16le -ac 1 -ar 44100 curr.wav 

Yeah, I know. Inelegant, but at least I got it working. I needed it for my hackathon project MPEGMafia

Related