Example #1
0
void cMediaInfo::readTagProperties(TagLib::PropertyMap& tags)
{
	QString		szID;

	for(TagLib::PropertyMap::ConstIterator i = tags.begin(); i != tags.end(); ++i)
	{
		QStringList	szValue;
		szID	= QString::fromStdWString(i->first.toWString());
		if(!szID.isEmpty())
		{
			for(TagLib::StringList::ConstIterator j = i->second.begin(); j != i->second.end(); ++j)
			{
				QString	sz = QString::fromStdWString((*j).toWString());
				if(!sz.isEmpty())
				{
					if(!szID.compare("LYRICS"))
					{
						sz.replace("\"", "'");
						sz.replace("\r\n", "\n");
						szValue.append(sz);
					}
					else
						szValue.append(sz.split("\n"));
				}
			}
			if(!szID.isEmpty() && !szValue.isEmpty())
				m_TAGPropertiesList.add(szID, szValue);
		}
	}
}
Example #2
0
int handle_file(const char* filepath, const char* filekey, AudioFileRecordStore& record_store)
{
    bool record_exists = record_store.find_record(filekey) != NULL;

    // Scanning a file for tags is expensive, so only do it if required.
    if(record_exists
            && !record_store.record_update_required(filekey))
    {
        // no update reqquired so has been handled.
        return 1;
    }

    TagLib::FileRef f(filepath);
    if (!f.isNull() && f.tag())
    {
        AudioFileRecord &record = record_store.get_record(filekey);
        record.update_start();

        if (verbose)
        {
            TagLib::Tag *tag = f.tag();
            std::cout << filepath << endl;
            std::cout << filekey << endl;
            std::cout << "-- TAG (basic) --" << endl;
            std::cout << "title   - \"" << tag->title()   << "\"" << endl;
            std::cout << "artist  - \"" << tag->artist()  << "\"" << endl;
            std::cout << "album   - \"" << tag->album()   << "\"" << endl;
            std::cout << "year    - \"" << tag->year()    << "\"" << endl;
            std::cout << "comment - \"" << tag->comment() << "\"" << endl;
            std::cout << "track   - \"" << tag->track()   << "\"" << endl;
            std::cout << "genre   - \"" << tag->genre()   << "\"" << endl;
        }

        TagLib::PropertyMap tags = f.file()->properties();

        for(TagLib::PropertyMap::ConstIterator i = tags.begin(); i != tags.end(); ++i)
        {
            for(TagLib::StringList::ConstIterator j = i->second.begin(); j != i->second.end(); ++j) 
            {
                record.update(i->first.toCString(true), j->toCString(true));
            }
        }

        if (f.audioProperties())
        {
            TagLib::AudioProperties *properties = f.audioProperties();
            record.update(audio_tags::BITRATE, properties->bitrate());
            record.update(audio_tags::LENGTH, properties->length());
            record.update(audio_tags::SAMPLERATE, properties->sampleRate());
            record.update(audio_tags::CHANNELS, properties->channels());
        }

        record.update_complete();
        return 1;
    }

    return 0;
}
Example #3
0
int main(int argc, char *argv[])
{
  for(int i = 1; i < argc; i++) {

    cout << "******************** \"" << argv[i] << "\" ********************" << endl;

    TagLib::FileRef f(argv[i]);

    if(!f.isNull() && f.tag()) {

      TagLib::Tag *tag = f.tag();

      cout << "-- TAG (basic) --" << endl;
      cout << "title   - \"" << tag->title()   << "\"" << endl;
      cout << "artist  - \"" << tag->artist()  << "\"" << endl;
      cout << "album   - \"" << tag->album()   << "\"" << endl;
      cout << "year    - \"" << tag->year()    << "\"" << endl;
      cout << "comment - \"" << tag->comment() << "\"" << endl;
      cout << "track   - \"" << tag->track()   << "\"" << endl;
      cout << "genre   - \"" << tag->genre()   << "\"" << endl;

      TagLib::PropertyMap tags = f.file()->properties();

      unsigned int longest = 0;
      for(TagLib::PropertyMap::ConstIterator i = tags.begin(); i != tags.end(); ++i) {
        if (i->first.size() > longest) {
          longest = i->first.size();
        }
      }

      cout << "-- TAG (properties) --" << endl;
      for(TagLib::PropertyMap::ConstIterator i = tags.begin(); i != tags.end(); ++i) {
        for(TagLib::StringList::ConstIterator j = i->second.begin(); j != i->second.end(); ++j) {
          cout << left << std::setw(longest) << i->first << " - " << '"' << *j << '"' << endl;
        }
      }

    }

    if(!f.isNull() && f.audioProperties()) {

      TagLib::AudioProperties *properties = f.audioProperties();

      int seconds = properties->length() % 60;
      int minutes = (properties->length() - seconds) / 60;

      cout << "-- AUDIO --" << endl;
      cout << "bitrate     - " << properties->bitrate() << endl;
      cout << "sample rate - " << properties->sampleRate() << endl;
      cout << "channels    - " << properties->channels() << endl;
      cout << "length      - " << minutes << ":" << setfill('0') << setw(2) << seconds << endl;
    }
  }
  return 0;
}
Example #4
0
QString FileHelper::extractGenericFeature(const QString &featureToExtract) const
{
	QString feature;
	TagLib::PropertyMap p = _file->properties();
	if (p.contains(featureToExtract.toStdString().data())) {
		TagLib::StringList list = p[featureToExtract.toStdString().data()];
		if (!list.isEmpty()) {
			feature = list.front().toCString(true);
		}
	}
	return feature;
}
Example #5
0
BlockResult Block::eval(const TagLib::FileRef& file)
{
    TagLib::PropertyMap metadata = file.tag()->properties();
    TagLib::StringList list;
    std::stringstream ss;
    // TODO: Needs to be refactored
    // TODO: Add more
    ss << file.audioProperties()->length();
    list.append(ss.str());
    metadata.insert("length", list);
    list.clear();
    ss.clear();

    ss << file.audioProperties()->bitrate();
    list.append(ss.str());
    metadata.insert("bitrate", list);
    list.clear();
    ss.clear();

    if (file.audioProperties()->channels() == 1) {
        list.append("mono");
    } else if (file.audioProperties()->channels() == 2) {
        list.append("stereo");
    } //TODO: Add more channels names

    metadata.insert("channels", list);
    list.clear();
    ss.clear();

    ss << file.audioProperties()->sampleRate();
    list.append(ss.str());
    metadata.insert("samplerate", list);
    list.clear();
    ss.clear();

    list.append(file.file()->name());
    metadata.insert("filename", list);
    list.clear();
    ss.clear();

    return this->eval(metadata);
}
Example #6
0
BlockResult Block::eval(const TagLib::PropertyMap& metadata)
{
    BlockResult result;

    std::sort(this->variables.begin(), this->variables.end() ,
    [](Block::Variable a, Block::Variable  b) {
        return a.pos < b.pos;
    });
    std::sort(this->functions.begin(), this->functions.end() ,
    [](Block::Function a, Block::Function  b) {
        return a.pos < b.pos;
    });

    result.success = false;
    result.result = this->parsed_statement;

    int offset = 0;
    auto var_itr = this->variables.begin();
    auto func_itr = this->functions.begin();

    // Iterate over the Function and Variables lists in order of appearance in the raw statement
    while (var_itr != this->variables.end() || func_itr != this->functions.end()) {
        bool eval_var = true;

        // Decide if the next item to be evaluated should be a variable or a function call
        if (var_itr == this->variables.end()) {
            eval_var = false;
        } else if (func_itr != this->functions.end()) {
            // Check the positions of the statements in the raw string
            if ((*var_itr).raw_pos > (*func_itr).raw_pos) {
                eval_var = false;
            }
        }

        if (eval_var) {
            if (metadata.contains((*var_itr).name)) {
                // insert at the index of the variable
                // TODO : alias some names, e.g. tacknumber -> track
                std::string meta = metadata[(*var_itr).name].toString().toCString(false);
                result.result.insert((*var_itr).pos + offset, meta);
                offset += meta.length();

                result.success = true;
            }
        } else {
            if (FunctionMap.find((*func_itr).name) != FunctionMap.end()) {
                // Call function
                BlockResult func_result = FunctionMap[(*func_itr).name].eval(metadata, (*func_itr).args);

                // OR'd to keep existing successes
                result.success |= func_result.success;

                result.result.insert((*func_itr).pos + offset, func_result.result);
                offset += func_result.result.length();
            } else {
                // Throw unknown function exception
            }
        }

        if (eval_var) {
            var_itr++;
        } else {
            func_itr++;
        }
    }

    /**
     * Using this conversion from String to integer it will allow for the
     * convention of:
     *	"c3po" → 0
     *	"4.8"  → 4
     *	"-12"  → -12
     *	"- 12" → 0
     */
    std::stringstream ss(result.result);
    ss >> result.value;

    return result;
}
Example #7
0
void MetadataReader::compute() {
  if (!parameter("filename").isConfigured()) {
    throw EssentiaException("MetadataReader: 'filename' parameter has not been configured");
  }

#ifdef _WIN32
  int len = MultiByteToWideChar(CP_UTF8, 0, _filename.c_str(), -1, NULL, 0);
  wchar_t *buf = (wchar_t*)malloc(sizeof(wchar_t)*len);
  memset(buf, 0, len);
  MultiByteToWideChar(CP_UTF8, 0, _filename.c_str(), -1, buf, len);
  TagLib::FileRef f(buf);
  free(buf);
#else
  TagLib::FileRef f(_filename.c_str());
#endif

  Pool tagPool;

  if (f.isNull()) {
    // in case TagLib can't get metadata out of this file, try some basic PCM approach
    int pcmSampleRate = 0;
    int pcmChannels = 0;
    int pcmBitrate = 0;

    try {
      pcmMetadata(_filename, pcmSampleRate, pcmChannels, pcmBitrate);
      // works only for 16bit wavs/pcm; it should output incorrect value for 
      // 24bit or 32bit float files, therefore, print a warning
      E_WARNING("MetadataReader: TagLib could not get metadata for this file. The output bitrate is estimated treating the input as 16-bit PCM, and therefore may be incorrect.");
    }
    catch (EssentiaException& e) {
      if (parameter("failOnError").toBool())
        throw EssentiaException("MetadataReader: File does not exist or does not seem to be of a supported filetype. ", e.what());
    }

    _title.get()   = "";
    _artist.get()  = "";
    _album.get()   = "";
    _comment.get() = "";
    _genre.get()   = "";
    _track.get()   = "";
    _date.get()    = "";

    _tagPool.get()  = tagPool;

    _duration.get()   = 0;
    _bitrate.get()    = pcmBitrate;
    _sampleRate.get() = pcmSampleRate;
    _channels.get()   = pcmChannels;

    return;
  }

  TagLib::PropertyMap tags = f.file()->properties();

  _title.get()   = formatString(tags["TITLE"]);
  _artist.get()  = formatString(tags["ARTIST"]);
  _album.get()   = formatString(tags["ALBUM"]);
  _comment.get() = formatString(tags["COMMENT"]);
  _genre.get()   = formatString(tags["GENRE"]);
  _track.get()   = formatString(tags["TRACKNUMBER"]);
  _date.get()    = formatString(tags["DATE"]);

  // populate tag pool
  for(TagLib::PropertyMap::ConstIterator i = tags.begin(); i != tags.end(); ++i) {
    string key = i->first.to8Bit(true);
    if (!_filterMetadata || std::find(_filterMetadataTags.begin(), _filterMetadataTags.end(), key) != _filterMetadataTags.end()) {
        // remove '.' chars which are used in Pool descriptor names as a separator
        // convert to lowercase
        std::replace(key.begin(), key.end(), '.', '_');
        std::transform(key.begin(), key.end(), key.begin(), ::tolower);
        key = _tagPoolName + "." + key;

        for(TagLib::StringList::ConstIterator str = i->second.begin(); str != i->second.end(); ++str) {
          tagPool.add(key, str->to8Bit(true));
        }
    }
  }

  _tagPool.get()  = tagPool;

  _duration.get()     = f.audioProperties()->length();
  _bitrate.get()    = f.audioProperties()->bitrate();
  _sampleRate.get() = f.audioProperties()->sampleRate();
  _channels.get()   = f.audioProperties()->channels();

  // fix for taglib incorrectly returning the bitrate for wave files
  string ext = toLower(_filename.substr(_filename.size()-3));
  if (ext == "wav") {
    _bitrate.get() = _bitrate.get() * 1024 / 1000;
  }
}