The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
sound_music_track.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
3  Copyright (C) 2009 - 2016 by Ignacio R. Morelle <[email protected]>
4  Part of the Battle for Wesnoth Project http://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 "util.hpp"
23 #if !defined(_WIN32) && !defined(__APPLE__)
24 #include "vorbis/vorbisfile.h"
25 #endif
26 
27 static lg::log_domain log_audio("audio");
28 #define ERR_AUDIO LOG_STREAM(err, log_audio)
29 #define LOG_AUDIO LOG_STREAM(info, log_audio)
30 
31 namespace sound {
32 
34  id_(),
35  file_path_(),
36  ms_before_(0),
37  ms_after_(0),
38  once_(false),
39  append_(false),
40  immediate_(false),
41  shuffle_(true)
42 {
43 }
44 
46  id_(node["name"]),
47  file_path_(),
48  title_(node["title"]),
49  ms_before_(node["ms_before"]),
50  ms_after_(node["ms_after"]),
51  once_(node["play_once"].to_bool()),
52  append_(node["append"].to_bool()),
53  immediate_(node["immediate"].to_bool()),
54  shuffle_(node["shuffle"].to_bool(true))
55 {
56  resolve();
57 }
58 
60  id_(v_name),
61  file_path_(),
62  title_(),
63  ms_before_(0),
64  ms_after_(0),
65  once_(false),
66  append_(false),
67  immediate_(false),
68  shuffle_(true)
69 {
70  resolve();
71 }
72 
74 {
75  if (id_.empty()) {
76  LOG_AUDIO << "empty track filename specified for track identification\n";
77  return;
78  }
79 
81 
82  if (file_path_.empty()) {
83  LOG_AUDIO << "could not find track '" << id_ << "' for track identification\n";
84  return;
85  }
86 
87 
88 #if !defined(_WIN32) && !defined(__APPLE__) && !defined(PANDORA)
89  if (title_.empty()) {
90  FILE* f;
91  f = fopen(file_path_.c_str(), "r");
92  if (f == nullptr) {
93  LOG_AUDIO << "Error opening file '" << file_path_
94  << "' for track identification\n";
95  return;
96  }
97 
98  OggVorbis_File vf;
99  if(ov_open(f, &vf, nullptr, 0) < 0) {
100  LOG_AUDIO << "track does not appear to be an Ogg file '"
101  << id_ << "', cannot be identified\n";
102  ov_clear(&vf);
103  return;
104  }
105 
106  vorbis_comment* comments = ov_comment(&vf, -1);
107  char** user_comments = comments->user_comments;
108 
109  bool found = false;
110  for (int i=0; i< comments->comments; i++) {
111  const std::string comment_string(user_comments[i]);
112  const std::vector<std::string> comment_list = utils::split(comment_string, '=');
113 
114  if (comment_list[0] == "TITLE" || comment_list[0] == "title") {
115  title_ = comment_list[1];
116  found = true;
117  }
118  }
119  if (!found) {
120  LOG_AUDIO << "No title for music track '" << id_ << "'\n";
121  }
122 
123  ov_clear(&vf);
124  }
125 #endif
126 
127  LOG_AUDIO << "resolved music track '" << id_ << "' into '" << file_path_ << "'\n";
128 }
129 
130 void music_track::write(config &parent_node, bool append) const
131 {
132  config& m = parent_node.add_child("music");
133  m["name"] = id_;
134  m["ms_before"] = ms_before_;
135  m["ms_after"] = ms_after_;
136  if(append) {
137  m["append"] = true;
138  }
139  //default behaviour is to shuffle
140  m["shuffle"] = shuffle_;
141 }
142 
143 } /* end namespace sound */
std::string id_
Definition: formula.cpp:636
static lg::log_domain log_audio("audio")
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't prese...
Audio output for sound and music.
Definition: sound.cpp:43
Definitions for the interface to Wesnoth Markup Language (WML).
config & add_child(const std::string &key)
Definition: config.cpp:743
Templates and utility-routines for strings and numbers.
void write(config &parent_node, bool append) const
size_t i
Definition: function.cpp:1057
Declarations for File-IO.
const GLdouble * m
Definition: glew.h:6968
Standard logging facilities (interface).
std::vector< std::string > split(std::string const &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLsizei const GLcharARB ** string
Definition: glew.h:4503
#define LOG_AUDIO
GLclampf f
Definition: glew.h:3024