Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SimpleSound.cpp
Go to the documentation of this file.
1 //
2 // C++ Implementation: Audio::SimpleSound
3 //
4 
5 #include "SimpleSound.h"
6 #include "config.h"
7 
8 #include "CodecRegistry.h"
9 #include "Stream.h"
10 
11 namespace Audio {
12 
13  SimpleSound::SimpleSound(const std::string& name, VSFileSystem::VSFileType _type, bool streaming)
14  throw()
15  : Sound(name, streaming),
16  type(_type)
17  {
18  }
19 
21  {
22  }
23 
25  throw(Exception)
26  {
27  if (isStreamLoaded())
29 
30  // Open stream and initialize shared pointer
31  stream.reset(
33  getName(),
34  getType()
35  )
36  );
37 
38  // Copy format
39  getFormat() = getStream()->getFormat();
40  }
41 
44  {
45  if (!isStreamLoaded())
46  throw(ResourceNotLoadedException());
47  stream.reset();
48  }
49 
50  SharedPtr<Stream> SimpleSound::getStream() const
52  {
53  if (!isStreamLoaded())
54  throw(ResourceNotLoadedException());
55  return stream;
56  }
57 
59  throw(Exception)
60  {
61  if (buffer.getFormat() == getFormat()) {
62  // Same formats, so all we have to do is read bytes ;)
63  buffer.setUsedBytes(
64  getStream()->read( buffer.getBuffer(), buffer.getByteCapacity() )
65  );
66  } else {
67  // Save the buffer format, we'll have to reformat to this format
68  Format targetFormat = buffer.getFormat();
69 
70  // Set buffer format to stream format
71  buffer.setFormat(getFormat());
72 
73  // Now read bytes from the stream
74  buffer.setUsedBytes(
75  getStream()->read( buffer.getBuffer(), buffer.getByteCapacity() )
76  );
77 
78  // Finally we have to reformat the buffer back to the original format
79  buffer.reformat(targetFormat);
80  }
81  }
82 
84  throw()
85  {
86  // Intentionally blank
87  }
88 
89 };