Debian Codegraph @

src/gengine/SDLMusicLooper.cpp

raw
1 /*
2 * Copyright (C) 2010 Brian Raiter
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9 #include "SDLMusicLooper.h"
10
11 #include "Path.h"
12 #include "StringTool.h"
13 #include "Log.h"
14
15 #include <string.h> //memset
16
17 //-----------------------------------------------------------------
18 /**
19 * Initialize the player for a specific piece of music.
20 */
21 SDLMusicLooper::SDLMusicLooper(const Path &file)
22 : m_volume(MIX_MAX_VOLUME), m_position(0)
23 {
24 Uint16 fmt;
25 int freq, channels;
26 Mix_QuerySpec(&freq, &fmt, &channels);
27 int sampleSize = fmt == AUDIO_S8 || fmt == AUDIO_U8 ? 1 : 2;
28
29 m_music = Mix_LoadWAV(file.getNative().c_str());
30 if (m_music) {
31 lookupLoopData(file, sampleSize * channels * freq / 22050);
32 }
33 else {
34 LOG_WARNING(ExInfo("cannot play music")
35 .addInfo("music", file.getNative())
36 .addInfo("Mix", Mix_GetError()));
37 }
38 }
39 //-----------------------------------------------------------------
40 SDLMusicLooper::~SDLMusicLooper()
41 {
42 stop();
43 if (m_music) {
44 Mix_FreeChunk(m_music);
45 }
46 }
47 //-----------------------------------------------------------------
48 void
49 SDLMusicLooper::setVolume(int volume)
50 {
51 m_volume = volume;
52 if (m_volume > MIX_MAX_VOLUME) {
53 m_volume = MIX_MAX_VOLUME;
54 }
55 else if (m_volume < 0) {
56 m_volume = 0;
57 }
58 }
59 //-----------------------------------------------------------------
60 void
61 SDLMusicLooper::start()
62 {
63 if (m_music) {
64 Mix_HookMusic(musicOutput, (void*)this);
65 }
66 }
67 //-----------------------------------------------------------------
68 void
69 SDLMusicLooper::stop()
70 {
71 Mix_HookMusic(NULL, NULL);
72 }
73 //-----------------------------------------------------------------
74 /**
75 * Check for a loop associated with this filename. If not, then
76 * loop over the entire chunk.
77 *
78 * In the original game, the music loop definitions were stored as metadata
79 * inside the sampler chunk of the WAV files.
80 * Now we store them in *.ogg.meta.
81 *
82 * File format:
83 * loop_start
84 * loop_end
85 */
86 void
87 SDLMusicLooper::lookupLoopData(const Path &file, int multiplier)
88 {
89 m_startLoop = 0;
90 m_endLoop = m_music->alen;
91
92 std::string metafile = file.getNative() + ".meta";
93 char buffer[1024];
94 memset(buffer, 0, sizeof(buffer));
95 FILE *meta = fopen(metafile.c_str(), "r");
96 if(!meta) {
97 return;
98 }
99
100 if (!fread(&buffer, 1, sizeof(buffer) - 1, meta)) {
101 LOG_WARNING(ExInfo("unable to read music meta data")
102 .addInfo("file", metafile));
103 return;
104 }
105
106 StringTool::t_args lines = StringTool::split(buffer, '\n');
107 if (lines.size() < 2) {
108 LOG_WARNING(ExInfo("unrecognized music meta data format")
109 .addInfo("file", metafile));
110 return;
111 }
112
113 m_startLoop = strtol(lines[0].c_str(), NULL, 10) * multiplier;
114 m_endLoop = strtol(lines[1].c_str(), NULL, 10) * multiplier;
115 if ((unsigned int)m_endLoop > m_music->alen) {
116 m_endLoop = m_music->alen;
117 }
118 LOG_DEBUG(ExInfo("looping music")
119 .addInfo("start", m_startLoop / multiplier)
120 .addInfo("end", m_endLoop / multiplier));
121 }
122 //-----------------------------------------------------------------
123 /**
124 * Callback function that provides the music data to the mixer.
125 */
126 void
127 SDLMusicLooper::musicOutput(void *udata, Uint8 *stream, int length)
128 {
129 SDLMusicLooper *that = (SDLMusicLooper*)udata;
130 int n;
131
132 memset(stream, 0, length);
133 while (length >= that->m_endLoop - that->m_position) {
134 n = that->m_endLoop - that->m_position;
135 SDL_MixAudio(stream, that->m_music->abuf + that->m_position, n,
136 that->m_volume);
137 stream += n;
138 length -= n;
139 that->m_position = that->m_startLoop;
140 }
141 SDL_MixAudio(stream, that->m_music->abuf + that->m_position, length,
142 that->m_volume);
143 that->m_position += length;
144 }