読者です 読者をやめる 読者になる 読者になる

MatlabでMP3を読み書きする

MatlabでMP3を読み書きするルーチンが欲しくなって、作りました。無圧縮ファイルを使うほど音質重視じゃないけど、処理したいファイル数はたくさんある、という時にちょっと便利かもしれません。自分の環境のことしか考えてなかったので、Unix系でLAMEが入っているのが条件です。

MP3READ

function [x,fs,nbits] = mp3read(filename)
%MP3READ   Read MP3 file as a matrix
%   [x,fs,nbits] = mp3read(filename) reads MP3 file as a matrix.  The MP3
%   decoder must be specified at the beginning of the m-file.

%   2008-09-10 JikanBae

MP3DECODER = '/usr/local/bin/lame --silent --decode';
TMPDIR = '/tmp';

tmpfilename = ['MP3READ_' datestr(now, 'yyyymmddHHMMSSFFF')];
system(sprintf('%s %s %s/%s.wav', MP3DECODER, filename, TMPDIR, tmpfilename));

[x,fs,nbits] = wavread(sprintf('%s/%s.wav', TMPDIR, tmpfilename));
system(sprintf('rm %s/%s.wav', TMPDIR, tmpfilename));

MP3WRITE

function mp3write(x, fs, filename)
%MP3WRITE   Write a matrix as an MP3 file
%   mp3write(x, fs, filename) writes a matrix as an MP3 file.  The MP3
%   encoder must be specified at the beginning of the m-file.  'fs' is the
%   sampling frequency of 'x'.

%   2008-09-10 JikanBae

MP3ENCODER = '/usr/local/bin/lame --silent -V 0';
TMPDIR = '/tmp';

tmpfilename = ['MP3WRITE_' datestr(now, 'yyyymmddHHMMSSFFF')];
wavwrite(x, fs, 24, sprintf('%s/%s.wav', TMPDIR, tmpfilename));

system(sprintf('%s %s/%s.wav %s', MP3ENCODER, TMPDIR, tmpfilename, filename));
system(sprintf('rm %s/%s.wav', TMPDIR, tmpfilename));