Verfasst am: 17.07.2009, 13:41
Titel: mp3 nur 10 sekunden abspielbar
Halli Hallo!!!
Ich hätte da gerne mal ein Problem.
Mein Programm spielt Sinustöne sowie mp3 und wavs ab.
Allerdings ist mein Problem ich kann nur maximal 30 Sekunden lange mp3 Stücke einlesen und es werden immer nur ca. 10 sekunden abgespielt. Ich habe keine Ahnung warum dann immer Schluß ist.
Realisiert wurde das ganze so:
Zusätzlich habe ich noch eine fertige mp3read M-File bekommen.
Code:
function[Y,FS,NBITS,OPTS] = mp3read(FILE,N,MONO,DOWNSAMP,DELAY) % MP3READ Read MP3 audio file via use of external binaries. % Y = MP3READ(FILE) reads an mp3-encoded audio file into the % vector Y just like wavread reads a wav-encoded file (one channel % per column). Extension ".mp3" is added if FILE has none. % Also accepts other formats of wavread, such as % Y = MP3READ(FILE,N) to read just the first N sample frames (N % scalar), or the frames from N(1) to N(2) if N is a two-element vector. % Y = MP3READ(FILE,FMT) or Y = mp3read(FILE,N,FMT) % with FMT as 'native' returns int16 samples instead of doubles; % FMT can be 'double' for default behavior (to exactly mirror the % syntax of wavread).
%
% [Y,FS,NBITS,OPTS] = MP3READ(FILE...) returns extra information: % FS is the sampling rate, NBITS is the bit depth (always 16), % OPTS.fmt is a format info string; OPTS has multiple other % fields, see WAVREAD.
%
% SIZ = MP3READ(FILE,'size') returns the size of the audio data contained % in the file in place of the actual audio data, returning the % 2-element vector SIZ=[samples channels].
%
% [Y...] = MP3READ(FILE,N,MONO,DOWNSAMP,DELAY) extends the % WAVREAD syntax to allow access to special features of the % mpg123 engine: MONO = 1 forces output to be mono (by % averaging stereo channels); DOWNSAMP = 2 or 4 downsamples by % a factor of 2 or 4 (thus FS returns as 22050 or 11025 % respectively for a 44 kHz mp3 file); DELAY controls how many % "warm up" samples to drop at the start of the file; the % default value of 2257 makes an mp3write/mp3read loop for a 44 % kHz mp3 file be as close as possible to being temporally % aligned; specify as 0 to prevent discard of initial samples.
%
% Example: % To read an mp3 file as doubles at its original width and sampling rate: % [Y,FS] = mp3read('piano.mp3'); % To read the first 1 second of the same file, downsampled by a % factor of 4, cast to mono, using the default filename % extension: % [Y,FS4] = mp3read('piano', FS/4, 1, 4);
%
% Note: Because the mp3 format encodes samples in blocks of 26 ms (at % 44 kHz), and because of the "warm up" period of the encoder, % the file length may not be exactly what you expect.
%
% Note: requires external binaries mpg123 and mp3info; you % can find binaries for several platforms at: % http://labrosa.ee.columbia.edu/matlab/mp3read.html
%
% See also mp3write, wavread.
% 2003-07-20 dpwe@ee.columbia.edu This version calls mpg123. % 2004-08-31 Fixed to read whole files correctly % 2004-09-08 Uses mp3info to get info about mp3 files too % 2004-09-18 Reports all mp3info fields in OPTS.fmt; handles MPG2LSF sizes % + added MONO, DOWNSAMP flags, changed default behavior. % 2005-09-28 Fixed bug reading full-rate stereo as 1ch (thx bjoerns@vjk.dk) % 2006-09-17 Chop off initial 2257 sample delay (for 44.1 kHz mp3) % so read-write loop doesn't get progressively delayed. % You can suppress this with a 5th argument of 0. % 2007-02-04 Added support for FMT argument to match wavread % Added automatic selection of binary etc. to allow it % to work cross-platform without editing prior to % submitting to Matlab File Exchange % 2007-07-23 Tweaks to 'size' mode so it exactly agrees with read data.
% %%%%% Directory for temporary file (if needed) % % Try to read from environment, or use /tmp if it exists, or use CWD
tmpdir = getenv('TMPDIR');
ifisempty(tmpdir) || exist(tmpdir,'file')==0
tmpdir = '/tmp';
end ifexist(tmpdir,'file')==0
tmpdir = '';
end % ensure it exists
%iflength(tmpdir) > 0 && exist(tmpdir,'file')==0 % mkdir(tmpdir);
%end
%%%%%% Location of the binaries - attempt to choose automatically
%%%%%% (or edit to be hard-coded for your installation)
ext = lower(computer);
ifispc
ext = 'exe';
rmcmd = 'del';
end
mpg123 = fullfile(path,['mpg123.',ext]);
mp3info = fullfile(path,['mp3info.',ext]);
%%%%% Process input arguments ifnargin < 2
N = 0;
end
% Check for FMT spec (per wavread)
FMT = 'double';
ifischar(N)
FMT = lower(N);
N = 0;
end
iflength(N) == 1 % Specified N was upper limit
N = [1 N];
end ifnargin < 3
forcemono = 0;
else % Check for 3rd arg as FMT ifischar(MONO)
FMT = lower(MONO);
MONO = 0;
end
forcemono = (MONO ~= 0);
end ifnargin < 4
downsamp = 1;
else
downsamp = DOWNSAMP;
end if downsamp ~= 1 && downsamp ~= 2 && downsamp ~= 4 error('DOWNSAMP can only be 1, 2, or 4');
end ifnargin < 5
mpg123delay44kHz = 2257; % empirical delay of lame/mpg123 loop
delay = round(mpg123delay44kHz/downsamp);
else
delay = DELAY;
end
ifstrcmp(FMT,'native') == 0 && strcmp(FMT,'double') == 0 && ... strcmp(FMT,'size') == 0 error(['FMT must be ''native'' or ''double''(or ''size''), not ''',FMT,'''']);
end
%%%%%% Constants
NBITS=16;
%%%%% add extension if none (like wavread) [path,file,ext] = fileparts(FILE);
ifisempty(ext)
FILE = [FILE, '.mp3'];
end
%%%%%% Probe file to find format, size, etc. using "mp3info" utility
cmd = ['"',mp3info, '" -r m -p "%Q %u %b %r %v * %C %e %E %L %O %o %p" "', FILE,'"'];
% Q = samprate, u = #frames, b = #badframes (needed to get right answer from %u) % r = bitrate, v = mpeg version (1/2/2.5) % C = Copyright, e = emph, E = CRC, L = layer, O = orig, o = mono, p = pad
w = mysystem(cmd);
% Break into numerical and ascii parts by finding the delimiter we put in
starpos = findstr(w,'*');
nums = str2num(w(1:(starpos - 2)));
strs = tokenize(w((starpos+2):end));
% chop off transcoding delay?
%sttfrm = sttfrm + delay; % empirically measured % no, we want to *decode* those samples, then drop them % so delay gets added to skipx instead
lenstr = '';
endfrm = -1;
decblk = 0;
iflength(N) > 1
endfrm = N(2);
if endfrm > sttfrm
decblk = ceil((endfrm+delay)*downsamp/smpspfrm) - skipblks + 10;
% we read 10 extra blks (+10) to cover the case where up to 10 bad % blocks are included in the part we are trying to read (it happened)
lenstr = [' -n ', num2str(decblk)];
% This generates a spurious "Warn: requested..." if reading right % to the last sample by index (or bad blks), but no matter. end end
% Run the decode
cmd=['"',mpg123,'"', downsampstr, chansstr, skipstr, lenstr, ...
' -q -w "', tmpfile,'" "',FILE,'"'];
%w =
mysystem(cmd);
% % pad delay on to end, just in case % Y = [Y; zeros(delay,size(Y,2))]; % % no, the saved file is just longer
if decblk > 0 && length(Y) < decblk*smpspfrm/downsamp
% This will happen if the selected block range includes >1 bad block disp(['Warn: requested ', num2str(decblk*smpspfrm/downsamp),' frames, returned ',num2str(length(Y))]);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function w = mysystem(cmd) % Run system command; report error; strip all but last line [s,w] = system(cmd);
if s ~= 0 error(['unable to execute ',cmd,'(',w,')']);
end % Keep just final line
w = w((1+max([0,findstr(w,10)])):end);
% Debug
%disp([cmd,' -> ','*',w,'*']);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function a = tokenize(s) % Break space-separated string into cell array of strings % 2004-09-18 dpwe@ee.columbia.edu
a = [];
p = 1;
n = 1;
l = length(s);
nss = findstr([s(p:end),' '],' ');
for ns = nss
% Skip initial spaces if ns == p
p = p+1;
else if p <= l
a{n} = s(p:(ns-1));
n = n+1;
p = ns+1;
end end end
Note The playback duration that results from setting Fs depends on the sound card you have installed. Most sound cards support sample frequencies of approximately 5-10 kHz to 44.1 kHz. Sample frequencies outside this range can produce unexpected results.
Oh Super!!!
Vielen Dank erstmal.
Jetzt muß ich mich erstmal mit dem Hinweis von Harald auseinandersetzen.
Einstellungen und Berechtigungen
Du kannst Beiträge in dieses Forum schreiben. Du kannst auf Beiträge in diesem Forum antworten. Du kannst deine Beiträge in diesem Forum nicht bearbeiten. Du kannst deine Beiträge in diesem Forum nicht löschen. Du kannst an Umfragen in diesem Forum nicht mitmachen. Du kannst Dateien in diesem Forum posten Du kannst Dateien in diesem Forum herunterladen
MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.