1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2019 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "nel/sound/sound_animation.h"
22 #include "nel/sound/sound_anim_marker.h"
23 #include "nel/misc/stream.h"
24 #include "nel/misc/i_xml.h"
25 #include "nel/misc/o_xml.h"
26 #include "nel/misc/file.h"
29 using namespace NLSOUND
;
30 using namespace NLMISC
;
35 // ********************************************************
37 void CSoundAnimation::addMarker(CSoundAnimMarker
* marker
)
43 _Markers
.push_back(marker
);
47 // ********************************************************
49 void CSoundAnimation::removeMarker(CSoundAnimMarker
* marker
)
55 vector
<CSoundAnimMarker
*>::iterator iter
;
56 for (iter
= _Markers
.begin(); iter
!= _Markers
.end(); iter
++)
66 // ********************************************************
68 void CSoundAnimation::sort()
70 //std::sort<CSoundAnimMarker*, eqmarker>(_Markers.begin(), _Markers.end(), eqmarker());
73 // ********************************************************
75 void CSoundAnimation::save()
79 vector
<NLMISC::TStringId
> sounds
;
82 if (!file
.open(_Filename
.c_str()))
84 throw NLMISC::Exception("Can't open the file for writing");
87 // Create the XML stream
91 if (output
.init (&file
, "1.0"))
93 xmlDocPtr xmlDoc
= output
.getDocument();
95 // Create the first node
96 xmlNodePtr root
= xmlNewDocNode (xmlDoc
, NULL
, (const xmlChar
*)"SOUNDANIMATION", NULL
);
97 xmlDocSetRootElement (xmlDoc
, root
);
99 vector
<CSoundAnimMarker
*>::iterator iter
;
100 for (iter
= _Markers
.begin(); iter
!= _Markers
.end(); iter
++)
102 CSoundAnimMarker
* marker
= (*iter
);
104 set
<string
>::iterator iter
;
107 smprintf(s
, 64, "%f", marker
->getTime());
109 xmlNodePtr markerNode
= xmlNewChild (root
, NULL
, (const xmlChar
*)"MARKER", NULL
);
110 xmlSetProp (markerNode
, (const xmlChar
*) "time", (const xmlChar
*) s
);
112 marker
->getSounds(sounds
);
114 vector
<NLMISC::TStringId
>::iterator iter2
;
115 for (iter2
= sounds
.begin(); iter2
!= sounds
.end(); iter2
++)
117 xmlNodePtr soundNode
= xmlNewChild ( markerNode
, NULL
, (const xmlChar
*)"SOUND", NULL
);
118 xmlSetProp (soundNode
, (const xmlChar
*)"name", (const xmlChar
*) CStringMapper::unmap(*iter2
).c_str());
124 // Flush the stream, write all the output file
134 // ********************************************************
136 void CSoundAnimation::play(UAudioMixer
* mixer
, float lastTime
, float curTime
, NL3D::CCluster
*cluster
, CSoundContext
&context
)
138 vector
<CSoundAnimMarker
*>::iterator iter
;
139 for (iter
= _Markers
.begin(); iter
!= _Markers
.end(); iter
++)
141 CSoundAnimMarker
* marker
= *iter
;
144 if ((lastTime
<= marker
->getTime()) && (marker
->getTime() < curTime
))
146 marker
->play(mixer
, cluster
, context
);
151 // ********************************************************
153 void CSoundAnimation::load()
158 if (!file
.open(_Filename
.c_str()))
160 throw NLMISC::Exception("Can't open the file for reading");
163 // Create the XML stream
167 if (input
.init (file
))
169 xmlNodePtr animNode
= input
.getRootNode ();
170 xmlNodePtr markerNode
= input
.getFirstChildNode(animNode
, "MARKER");
172 while (markerNode
!= 0)
174 CSoundAnimMarker
* marker
= new CSoundAnimMarker();
176 const char *time
= (const char*) xmlGetProp(markerNode
, (xmlChar
*) "time");
179 throw NLMISC::Exception("Invalid sound animation marker");
183 NLMISC::fromString(time
, val
);
185 marker
->setTime(val
);
186 xmlFree ((void*)time
);
189 xmlNodePtr soundNode
= input
.getFirstChildNode(markerNode
, "SOUND");
191 while (soundNode
!= 0)
193 char *name
= (char*) xmlGetProp(soundNode
, (xmlChar
*) "name");
196 throw NLMISC::Exception("Invalid sound animation marker");
199 marker
->addSound(CStringMapper::map(string(name
)));
201 xmlFree ((void*)name
);
203 soundNode
= input
.getNextChildNode(soundNode
, "SOUND");
208 markerNode
= input
.getNextChildNode(markerNode
, "MARKER");
217 } //namespace NLSOUND