Archive

Archive for the ‘music’ Category

Convert mpc to mp3 on Linux

January 1, 2017 Leave a comment

You need the lame and mpcdec commands. On Debian, mpcdec is in the musepack-tools package:

sudo apt-get install lame musepack-tools

Then to convert all mpc files in the current directory to matchingly named mp3 files:

for x in *.mpc; do mpcdec "${x}" - | lame -r - "${x%.mpc}.mp3"; done
Categories: bash, coding, debian, linux, music

Spotify on Debian GNU/Linux in Canada

March 29, 2016 Leave a comment

Today I decided to try out the free ad-sponsored Spotify music streaming service. It has been available in Canada since September 2014.

After signing up you can immediately use the flash-based web player at play.spotify.com.

Installing the client app

Alternatively you can download and install the Spotify client app. I cannot say yet what the advantages or disadvantages are, maybe reading this article can be helpful.

Anyway, if you want to try the client app, for Debian (or Ubuntu) users it works like this:

  1. Add the repo key (to verify downloaded packages)
  2. Add the spotify repo to apt sources
  3. Update apt caches
  4. Install the spotify client

Here are the shell commands (requires sudo):

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys BBEBDCB318AD50EC6865090613B00F1FD2C19886
echo deb http://repository.spotify.com stable non-free | sudo tee /etc/apt/sources.list.d/spotify.list
sudo apt-get update
sudo apt-get install spotify-client

After successful installation you will find a “Spotify” entry in the “Multimedia” section of your start menu.

Using your Facebook login

If you use your Facebook account to sign into Spotify you will probably see this question:

Spotify would like to post to Facebook for you.
Who do you want to share these posts with?

It is safe to choose “Not Now” which prevents Spotify from posting to your timeline. The login will still work.

If your are using the downloaded stand-alone client app and the Facebook login fails with an error page, then simply enter the email address and password from your Facebook account into the login fields of the Spotify client app.

Spotify says that it only uses these credentials to pass through to the Facebook authentication and won’t store your password anywhere. I hope that’s true.

Categories: bash, debian, linux, music Tags:

Klassikradio MP3 streams

February 17, 2016 Leave a comment

Klassikradio is a German radio station with an easy-to-digest selection of classical music, movie themes and other relaxing sounds, with not too many commercials.

They have many channels, all available as mp3 streams. The full list and links to the streams can be found at http://stream.klassikradio.de/

On my computers, I have saved the m3u files for all the 192kb streams. I retrieved them using one convenient wget command and then moved them to my media/radio folder.

These are the shell commands (works on Linux, Unixes, Cygwin and probably MacOS):

cd
mkdir -p media/radio
wget http://stream.klassikradio.de/ -r -l1 -A '*.m3u' \
--accept-regex '.*\/mp3-192\/stream\.klassikradio\.de\/play\.m3u'
cd stream.klassikradio.de
for channel in *; do
mv "${channel}/mp3-192/stream.klassikradio.de/play.m3u" \
"${HOME}/media/radio/klassikradio-${channel}.m3u";
done
cd ; rm -rf stream.klassikradio.de

M3U files are the easiest way to “bookmark” media streams to be played by an audio player of your choice, outside the web browser.

I use the VLC player with the “Allow only one instance” setting and configure it as the default application for *.m3u files.

Categories: bash, music

Play MP3 or OGG using javax.sound.sampled, mp3spi, vorbisspi

July 19, 2013 18 comments

I tried to come up with the simplest possible way of writing a Java class that can play mp3 and ogg files, using standard Java Sound APIs, with purely Open Source libraries from the public Maven Central repositories.

The LGPL-licensed mp3spi and vorbisspi libraries from javazoom.net satisfy these requirements and worked for me right away. As service provider implementations (SPI), they transparently add support for the mp3 and ogg audio formats to javax.sound.sampled, simply by being in the classpath.

For my AudioFilePlayer class below I basically took the example code from javazoom and simplified it as much as possible. Please note that it requires Java 7 as it uses try-with-resources.

Maven dependencies

  <!-- 
    We have to explicitly instruct Maven to use tritonus-share 0.3.7-2 
    and NOT 0.3.7-1, otherwise vorbisspi won't work.
   -->
<dependency>
  <groupId>com.googlecode.soundlibs</groupId>
  <artifactId>tritonus-share</artifactId>
  <version>0.3.7-2</version>
</dependency>
<dependency>
  <groupId>com.googlecode.soundlibs</groupId>
  <artifactId>mp3spi</artifactId>
  <version>1.9.5-1</version>
</dependency>
<dependency>
  <groupId>com.googlecode.soundlibs</groupId>
  <artifactId>vorbisspi</artifactId>
  <version>1.0.3-1</version>
</dependency>

AudioFilePlayer.java

package net.doepner.audio;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine.Info;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

import static javax.sound.sampled.AudioSystem.getAudioInputStream;
import static javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED;

public class AudioFilePlayer {

    public static void main(String[] args) {
        final AudioFilePlayer player = new AudioFilePlayer ();
        player.play("something.mp3");
        player.play("something.ogg");
    }

    public void play(String filePath) {
        final File file = new File(filePath);

        try (final AudioInputStream in = getAudioInputStream(file)) {
            
            final AudioFormat outFormat = getOutFormat(in.getFormat());
            final Info info = new Info(SourceDataLine.class, outFormat);

            try (final SourceDataLine line =
                     (SourceDataLine) AudioSystem.getLine(info)) {

                if (line != null) {
                    line.open(outFormat);
                    line.start();
                    stream(getAudioInputStream(outFormat, in), line);
                    line.drain();
                    line.stop();
                }
            }

        } catch (UnsupportedAudioFileException 
               | LineUnavailableException 
               | IOException e) {
            throw new IllegalStateException(e);
        }
    }

    private AudioFormat getOutFormat(AudioFormat inFormat) {
        final int ch = inFormat.getChannels();
        final float rate = inFormat.getSampleRate();
        return new AudioFormat(PCM_SIGNED, rate, 16, ch, ch * 2, rate, false);
    }

    private void stream(AudioInputStream in, SourceDataLine line) 
        throws IOException {
        final byte[] buffer = new byte[65536];
        for (int n = 0; n != -1; n = in.read(buffer, 0, buffer.length)) {
            line.write(buffer, 0, n);
        }
    }
}
Categories: java, maven, music

X with Eddie Vedder at Oracle Party

October 5, 2012 Leave a comment

Part of the JavaOne conference this week was a huge party by Oracle on Treasure Island, San Francisco. It featured a live concert (photos) from around 8pm to 1am with Kings of Leon, Pearl Jam and a band that was apparently big in the US indie scene in the 80s called “X”.

I enjoyed Kings of Leon – who had the unenviable task of warming up a crowd of thousand of mostly male IT dudes, then a great Pearl Jam gig and stayed to see and dance to X, until in the end Pearl Jam’s Eddie Vedder reappeared and joined X on stage …

Categories: conference, javaone, music

Klassikradio MP3 streams [2012/06/23]

June 23, 2012 8 comments

This post has been rewritten and republished in 2016.

Categories: bash, music

Simple webradio playback

October 8, 2011 Leave a comment

I listen to web radio stations but I don’t want to use any player ui for that. All I want is:

  1. Select a station from a list of my favorites and listen to it
  2. Be able to stop current web-radio playback
  3. Never have more than one station playing at the same time

I do it like this:

  • For each radio station save a playlist file (*.pls, *.m3u or sometimes *.asx) in a folder called “radio” on my local machine. I download most of them from the shoutcast or icecast stream directories. I also add one special (empty) file called “none.pls” (which serves to turn off all radio).
  • Add a toolbar to the taskbar that lists the content of the radio folder, i.e. all the webradio playlist files as clickable items. In XFCE add a “Directory Menu” item to the panel.
  • Configure the default app for the playlist mime types mentioned above to be my bash script “radio.sh”. It kills any existing webradio playback and plays the selected playlist file. See below for how to configure mime-type association defaults.
  • Install mpv – the de-facto successor of the now dormant mplayer – to do the actual playback.

This is my little “radio.sh” script (requires the pkill and mpv commands):

#! /bin/bash
pkill -f "mpv --playlist" 
mpv -playlist "$@"

To set this script as the default handler for the most common playlist file types, put the following into ~/.local/share/applications/defaults.list:

[Default Applications]
audio/x-mpegurl=radio.sh.desktop
audio/x-scpls=radio.sh.desktop

Make sure you have a file “radio.sh.desktop” in ~/.local/share/applications or in /usr/local/share/applications with contents like this:

[Desktop Entry]
Exec=radio.sh %U
MimeType=audio/x-mpegurl;audio/x-scpls;video/x-ms-asf
Name=radio.sh
StartupNotify=false
Terminal=false
Type=Application

If I have to use M$ Windows then I do something similar using a taskbar toolbar for the radio folder and the VLC player, configured to run minimized as systray icon.

Categories: bash, debian, linux, music, xfce Tags: , ,