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 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 #if !defined(_WIN32) && !defined(__APPLE__)
23 #include "vorbis/vorbisfile.h"
24 #endif
25 
26 static lg::log_domain log_audio("audio");
27 #define ERR_AUDIO LOG_STREAM(err, log_audio)
28 #define LOG_AUDIO LOG_STREAM(info, log_audio)
29 
30 namespace sound {
31 
33  id_(),
34  file_path_(),
35  ms_before_(0),
36  ms_after_(0),
37  once_(false),
38  append_(false),
39  immediate_(false),
40  shuffle_(true)
41 {
42 }
43 
45  id_(node["name"]),
46  file_path_(),
47  title_(node["title"]),
48  ms_before_(node["ms_before"]),
49  ms_after_(node["ms_after"]),
50  once_(node["play_once"].to_bool()),
51  append_(node["append"].to_bool()),
52  immediate_(node["immediate"].to_bool()),
53  shuffle_(node["shuffle"].to_bool(true))
54 {
55  resolve();
56 }
57 
58 music_track::music_track(const std::string& v_name) :
59  id_(v_name),
60  file_path_(),
61  title_(),
62  ms_before_(0),
63  ms_after_(0),
64  once_(false),
65  append_(false),
66  immediate_(false),
67  shuffle_(true)
68 {
69  resolve();
70 }
71 
73 {
74  if (id_.empty()) {
75  LOG_AUDIO << "empty track filename specified for track identification\n";
76  return;
77  }
78 
80 
81  if (file_path_.empty()) {
82  LOG_AUDIO << "could not find track '" << id_ << "' for track identification\n";
83  return;
84  }
85 
86 
87 #if !defined(_WIN32) && !defined(__APPLE__)
88  if (title_.empty()) {
89  FILE* f;
90  f = fopen(file_path_.c_str(), "r");
91  if (f == nullptr) {
92  LOG_AUDIO << "Error opening file '" << file_path_
93  << "' for track identification\n";
94  return;
95  }
96 
97  OggVorbis_File vf;
98  if(ov_open(f, &vf, nullptr, 0) < 0) {
99  LOG_AUDIO << "track does not appear to be an Ogg file '"
100  << id_ << "', cannot be identified\n";
101  ov_clear(&vf);
102  return;
103  }
104 
105  vorbis_comment* comments = ov_comment(&vf, -1);
106  char** user_comments = comments->user_comments;
107 
108  bool found = false;
109  for (int i=0; i< comments->comments; i++) {
110  const std::string comment_string(user_comments[i]);
111  const std::vector<std::string> comment_list = utils::split(comment_string, '=');
112 
113  if (comment_list[0] == "TITLE" || comment_list[0] == "title") {
114  title_ = comment_list[1];
115  found = true;
116  }
117  }
118  if (!found) {
119  LOG_AUDIO << "No title for music track '" << id_ << "'\n";
120  }
121 
122  ov_clear(&vf);
123  }
124 #endif
125 
126  LOG_AUDIO << "resolved music track '" << id_ << "' into '" << file_path_ << "'\n";
127 }
128 
129 void music_track::write(config &parent_node, bool append) const
130 {
131  config& m = parent_node.add_child("music");
132  m["name"] = id_;
133  m["ms_before"] = ms_before_;
134  m["ms_after"] = ms_after_;
135  if(append) {
136  m["append"] = true;
137  }
138  //default behaviour is to shuffle
139  m["shuffle"] = shuffle_;
140 }
141 
142 } /* 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:479
#define f
Standard logging facilities (interface).
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
#define LOG_AUDIO