The Battle for Wesnoth  1.19.0-dev
sound_music_track.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
3  by David White <dave@whitevine.net>, 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";
74  return;
75  }
76 
78 
79  if (file_path_.empty()) {
80  LOG_AUDIO << "could not find track '" << id_ << "' for track identification";
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";
88  return;
89  }
90 
91  vorbis_comment* comments = ov_comment(&vf, -1);
92  char** user_comments = comments->user_comments;
93 
94  bool found = false;
95  for (int i=0; i< comments->comments; i++) {
96  const std::string comment_string(user_comments[i]);
97  const std::vector<std::string> comment_list = utils::split(comment_string, '=');
98 
99  if (comment_list[0] == "TITLE" || comment_list[0] == "title") {
100  title_ = comment_list[1];
101  found = true;
102  }
103  }
104  if (!found) {
105  LOG_AUDIO << "No title for music track '" << id_ << "'";
106  }
107 
108  ov_clear(&vf);
109  }
110 
111  LOG_AUDIO << "resolved music track '" << id_ << "' into '" << file_path_ << "'";
112 }
113 
114 void music_track::write(config &parent_node, bool append) const
115 {
116  config& m = parent_node.add_child("music");
117  m["name"] = id_;
118  m["ms_before"] = ms_before_;
119  m["ms_after"] = ms_after_;
120  if(append) {
121  m["append"] = true;
122  }
123  //default behaviour is to shuffle
124  m["shuffle"] = shuffle_;
125 }
126 
127 } /* end namespace sound */
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
config & add_child(config_key_type key)
Definition: config.cpp:441
void write(config &parent_node, bool append) const
Declarations for File-IO.
std::size_t i
Definition: function.cpp:968
Standard logging facilities (interface).
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:40
std::vector< std::string > split(const config_attribute_value &val)
static lg::log_domain log_audio("audio")
#define LOG_AUDIO