The Battle for Wesnoth  1.15.0-dev
sound_music_track.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Copyright (C) 2009 - 2018 by Iris Morelle <shadowm2006@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #include "sound_music_track.hpp"
17 
18 #include "config.hpp"
19 #include "filesystem.hpp"
20 #include "log.hpp"
22 #include "vorbis/vorbisfile.h"
23 
24 static lg::log_domain log_audio("audio");
25 #define ERR_AUDIO LOG_STREAM(err, log_audio)
26 #define LOG_AUDIO LOG_STREAM(info, log_audio)
27 
28 namespace sound {
29 
31  id_(),
32  file_path_(),
33  ms_before_(0),
34  ms_after_(0),
35  once_(false),
36  append_(false),
37  immediate_(false),
38  shuffle_(true)
39 {
40 }
41 
43  id_(node["name"]),
44  file_path_(),
45  title_(node["title"]),
46  ms_before_(node["ms_before"]),
47  ms_after_(node["ms_after"]),
48  once_(node["play_once"].to_bool()),
49  append_(node["append"].to_bool()),
50  immediate_(node["immediate"].to_bool()),
51  shuffle_(node["shuffle"].to_bool(true))
52 {
53  resolve();
54 }
55 
56 music_track::music_track(const std::string& v_name) :
57  id_(v_name),
58  file_path_(),
59  title_(),
60  ms_before_(0),
61  ms_after_(0),
62  once_(false),
63  append_(false),
64  immediate_(false),
65  shuffle_(true)
66 {
67  resolve();
68 }
69 
71 {
72  if (id_.empty()) {
73  LOG_AUDIO << "empty track filename specified for track identification\n";
74  return;
75  }
76 
78 
79  if (file_path_.empty()) {
80  LOG_AUDIO << "could not find track '" << id_ << "' for track identification\n";
81  return;
82  }
83 
84  if (title_.empty()) {
85  OggVorbis_File vf;
86  if(ov_fopen(file_path_.c_str(), &vf) < 0) {
87  LOG_AUDIO << "Error opening file '" << file_path_ << "' for track identification\n";
88  ov_clear(&vf);
89  return;
90  }
91 
92  vorbis_comment* comments = ov_comment(&vf, -1);
93  char** user_comments = comments->user_comments;
94 
95  bool found = false;
96  for (int i=0; i< comments->comments; i++) {
97  const std::string comment_string(user_comments[i]);
98  const std::vector<std::string> comment_list = utils::split(comment_string, '=');
99 
100  if (comment_list[0] == "TITLE" || comment_list[0] == "title") {
101  title_ = comment_list[1];
102  found = true;
103  }
104  }
105  if (!found) {
106  LOG_AUDIO << "No title for music track '" << id_ << "'\n";
107  }
108 
109  ov_clear(&vf);
110  }
111 
112  LOG_AUDIO << "resolved music track '" << id_ << "' into '" << file_path_ << "'\n";
113 }
114 
115 void music_track::write(config &parent_node, bool append) const
116 {
117  config& m = parent_node.add_child("music");
118  m["name"] = id_;
119  m["ms_before"] = ms_before_;
120  m["ms_after"] = ms_after_;
121  if(append) {
122  m["append"] = true;
123  }
124  //default behaviour is to shuffle
125  m["shuffle"] = shuffle_;
126 }
127 
128 } /* end namespace sound */
static lg::log_domain log_audio("audio")
void write(config &parent_node, bool append) const
std::string get_binary_file_location(const std::string &type, const std::string &filename)
Returns a complete path to the actual file of a given type or an empty string if the file isn&#39;t prese...
Audio output for sound and music.
Definition: sound.cpp:40
Definitions for the interface to Wesnoth Markup Language (WML).
std::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
std::size_t i
Definition: function.cpp:933
Declarations for File-IO.
config & add_child(config_key_type key)
Definition: config.cpp:476
Standard logging facilities (interface).
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
#define LOG_AUDIO