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
SoundBuffer.cpp
Go to the documentation of this file.
1 //
2 // C++ Implementation: Audio::SoundBuffer
3 //
4 
5 #include "SoundBuffer.h"
6 #include "config.h"
7 
8 #include <stdlib.h>
9 #include <memory.h>
10 #include <utility>
11 
12 namespace Audio {
13 
15  throw()
16  : buffer(0),
17  byteCapacity(0),
18  bytesUsed(0)
19  {
20  }
21 
22  SoundBuffer::SoundBuffer(unsigned int capacity, const Format &format)
24  : buffer(0),
25  byteCapacity(0),
26  bytesUsed(0)
27  {
28  reserve(capacity, format);
29  }
30 
33  {
34  bytesUsed = byteCapacity = other.bytesUsed;
35  buffer = malloc(byteCapacity);
36  if (buffer == 0)
37  throw OutOfMemoryException();
38  memcpy(buffer, other.buffer, bytesUsed);
39  format = other.format;
40  }
41 
44  {
45  bytesUsed = byteCapacity = other.bytesUsed;
46  buffer = realloc(buffer, byteCapacity);
47  if (buffer == 0)
48  throw OutOfMemoryException();
49  memcpy(buffer, other.buffer, bytesUsed);
50  format = other.format;
51 
52  return *this;
53  }
54 
55  void SoundBuffer::reserve(unsigned int capacity)
57  {
58  byteCapacity = capacity;
59  bytesUsed = 0;
60 
61  buffer = realloc(buffer, byteCapacity);
62  if (buffer == 0)
63  throw OutOfMemoryException();
64  }
65 
66  void SoundBuffer::reserve(unsigned int capacity, const Format &_format)
68  {
69  format = _format;
70  reserve(capacity * _format.frameSize());
71  }
72 
73  void SoundBuffer::reformat(const Format &newFormat)
74  throw(Exception)
75  {
76  if (newFormat != format)
77  throw(NotImplementedException("Format conversion"));
78  }
79 
81  throw()
82  {
83  std::swap(buffer, other.buffer);
84  std::swap(byteCapacity, other.byteCapacity);
85  std::swap(bytesUsed, other.bytesUsed);
86  std::swap(format, other.format);
87  }
88 
90  throw()
91  {
92  if (bytesUsed == 0) {
93  if (buffer) {
94  free(buffer);
95  buffer = 0;
96  }
97  bytesUsed = byteCapacity = 0;
98  } else {
99  if (bytesUsed != byteCapacity)
100  buffer = realloc(buffer, byteCapacity = bytesUsed);
101  }
102  }
103 
104 };