/**************************************************************************** * libtremorplugin.cpp * * Copyright (C) 2002 Latchesar Ionkov * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ****************************************************************************/ // fixed and adapted for opieplayer 2003 ljp #include "libtremorplugin.h" /* OPIE */ #include /* QT */ #include /* STD */ #include #include #include #include #include #include #include #include #include #include #include #include #include extern "C" { #include "tremor/ivorbisfile.h" } #define MPEG_BUFFER_SIZE 65536 //#define debugMsg(a) qDebug(a) #define debugMsg(a) class LibTremorPluginData { public: char* filename; FILE* f; OggVorbis_File vf; vorbis_info* vi; vorbis_comment* vc; bool bos; int csection; QString finfo; }; LibTremorPlugin::LibTremorPlugin() { odebug << "<<<<<<<<<<<<>>>>>>>>>>>>>>>>>" << oendl; d = new LibTremorPluginData; d->f = 0; d->vi = 0; d->vc = 0; d->bos = 0; d->csection = 0; d->finfo = ""; } LibTremorPlugin::~LibTremorPlugin() { close(); delete d; } bool LibTremorPlugin::isFileSupported( const QString& path ) { debugMsg( "LibTremorPlugin::isFileSupported" ); // Mpeg file extensions // "mp2","mp3","m1v","m2v","m2s","mpg","vob","mpeg",ac3" // Other media extensions // "wav","mid","mod","s3m","ogg","avi","mov","sid" char *ext = strrchr( path.latin1(), '.' ); // Test file extension if ( ext ) { if ( strncasecmp(ext, ".ogg", 4) == 0 ) return TRUE; } return FALSE; } bool LibTremorPlugin::open( const QString& path ) { debugMsg( "LibTremorPlugin::open" ); d->filename = (char*) path.latin1(); d->f = fopen( d->filename, "r" ); if (d->f == 0) { odebug << "error opening " << d->filename << "" << oendl; return FALSE; } if (ov_open(d->f, &d->vf, NULL, 0) < 0) { odebug << "error opening " << d->filename << "" << oendl; return FALSE; } d->vc = ov_comment(&d->vf, -1); d->vi = ov_info(&d->vf, -1); d->bos = false; QString comments[] = { "title", "artist", "album", "year", "tracknumber", "" }; QString cdescr[] = { "Title", "Artist", "Album", "Year", "Track", "" }; QMap cmap; char** cptr = d->vc->user_comments; while (*cptr != 0) { QString s(*cptr); int n = s.find('='); if (n < 0) { continue; } QString key = s.left(n).lower(); QString value = s.mid(n+1); cmap[key] = value; cptr++; } d->finfo = ""; for(int i = 0; !comments[i].isEmpty(); i++) { QString v = cmap[comments[i].lower()]; if (!v.isEmpty()) { if (!d->finfo.isEmpty()) { d->finfo += ", "; } d->finfo += cdescr[i] + ": " + v; } } odebug << "finfo: " + d->finfo << oendl; return TRUE; } bool LibTremorPlugin::close() { debugMsg( "LibTremorPlugin::close" ); int result = TRUE; if (fclose(d->f) == -1) { odebug << "error closing file " << d->filename << "" << oendl; result = FALSE; } d->f = 0; d->finfo = ""; return result; } bool LibTremorPlugin::isOpen() { debugMsg( "LibTremorPlugin::isOpen" ); return ( d->f != 0 ); } const QString &LibTremorPlugin::fileInfo() { return d->finfo; } int LibTremorPlugin::audioStreams() { debugMsg( "LibTremorPlugin::audioStreams" ); return 1; } int LibTremorPlugin::audioChannels( int ) { odebug << "LibTremorPlugin::audioChannels: " << d->vi->channels << "" << oendl; return d->vi->channels; } int LibTremorPlugin::audioFrequency( int ) { odebug << "LibTremorPlugin::audioFrequency: " << d->vi->rate << "" << oendl; return d->vi->rate; } int LibTremorPlugin::audioSamples( int ) { debugMsg( "LibTremorPlugin::audioSamples" ); return (int) ov_pcm_total(&d->vf,-1); } bool LibTremorPlugin::audioSetSample( long, int ) { debugMsg( "LibTremorPlugin::audioSetSample" ); return FALSE; } long LibTremorPlugin::audioGetSample( int ) { debugMsg( "LibTremorPlugin::audioGetSample" ); return 0; } bool LibTremorPlugin::audioReadSamples( short *output, int, long samples, long& samplesMade, int ) { // odebug << "<<<<<<<<<<<csection; char* buf = (char*) output; int length = samples * 4; if ( samples == 0 ) return false; while (length > 0) { if (d->bos) { d->vi = ov_info(&d->vf, -1); d->vc = ov_comment(&d->vf, -1); } int n = 4096; if (length < n) { n = length; } long ret = ov_read(&d->vf, buf, n, &d->csection); // odebug << "" << ret << "" << oendl; if (ret == 0) { break; } else if (ret < 0) { return true; } if (old_section != d->csection) { d->bos = true; } buf += ret; length -= ret; } samplesMade = samples; return true; } double LibTremorPlugin::getTime() { debugMsg( "LibTremorPlugin::getTime" ); return 0.0; }