 |
Gorgon Game Engine
|
Go to the documentation of this file.
8 #include "../Audio/Basic.h"
9 #include "../IO/Stream.h"
12 namespace Containers {
30 if(channel >= channels) {
31 throw std::runtime_error(
"Index out of bounds");
35 return current[channel];
40 if(channel >= channels) {
41 throw std::runtime_error(
"Index out of bounds");
45 return current[channel];
57 Sample(
float *current,
unsigned channels) : current(current), channels(channels) { }
59 float *current =
nullptr;
60 unsigned channels = 0;
112 return {current, channels};
116 return current<r.current;
120 return current>r.current;
124 return current<=r.current;
128 return current>=r.current;
132 return current==r.current;
136 return current!=r.current;
140 return {current, channels};
144 current+=diff*channels;
150 temp.current+=diff*channels;
156 current-=diff*channels;
162 temp.current-=diff*channels;
168 return (
unsigned long)((current-r.current)/channels);
172 Iterator(
float *current,
unsigned channels) : current(current), channels(channels) {}
227 this->channels = std::move(
channels);
234 data = (
float*)malloc(
size * this->channels.size() *
sizeof(float));
243 if(this->size ==
size)
277 this->channels = std::move(
channels);
283 data = (
float*)malloc(
size * this->channels.size() *
sizeof(float));
284 memcpy(
data, newdata,
size * this->channels.size() *
sizeof(
float));
314 this->channels = std::move(
channels);
378 throw std::runtime_error(
"Index out of bounds");
388 throw std::runtime_error(
"Index out of bounds");
395 float Get(
unsigned long p,
unsigned ch)
const {
398 throw std::runtime_error(
"Index out of bounds");
411 return (
unsigned int)(
size *
channels.size() *
sizeof(float));
421 return (
unsigned int)
channels.size();
426 if(channel<0 || channel>=(
int)
channels.size())
436 throw std::runtime_error(
"Number of channels does not match");
438 this->channels = std::move(
channels);
443 for(
int i=0; i<(int)
channels.size(); i++) {
444 if(
channels[i] == channel)
return i;
462 std::ifstream file(filename, std::ios::binary);
464 if(!file.is_open())
return false;
489 if(samplesize != 8 && samplesize !=16)
return false;
491 if(byterate !=
samplerate*channelcnt*samplesize/8)
return false;
493 if(blocksize == 0) blocksize = byterate;
498 else if(
channels.size() != channelcnt)
509 auto target = file.tellg() +
size;
513 if(!file)
return false;
515 while(!file.eof() && file.tellg() < target) {
516 for(
int c = 0; c<channelcnt; c++) {
517 if(samplesize == 8) {
524 if(!file)
return false;
528 file.seekg(skip, std::ios::cur);
531 if((
long)file.tellg() != target)
return false;
539 bool ExportWav(
const std::string &filename,
int bits = 16) {
540 std::ofstream file(filename, std::ios::binary);
542 if(!file.is_open())
return false;
555 else if(bits == 16) {
559 throw std::runtime_error(
"Invalid number of bits for wav file");
588 int16_t multiplier = (1<<(bits-1))-1;
590 WriteInt16(file, (
int)std::round((*ptr) * multiplier));
Sample operator[](unsigned long pos) const
Definition: Wave.h:139
unsigned long GetBytes() const
Returns the size of the wave in bytes.
Definition: Wave.h:410
void WriteInt16(std::ostream &stream, int value)
Writes a 16-bit integer from the stream.
Definition: Stream.h:211
float Channel(unsigned channel) const
Definition: Wave.h:38
void Assume(float *newdata)
Definition: Wave.h:322
Iterator operator++(int)
Definition: Wave.h:89
float * Release()
Returns and disowns the current data buffer. If wave is empty, this method will return a nullptr.
Definition: Wave.h:331
Represents a sample in the Wave data.
Definition: Wave.h:23
Wave(Wave &&data)
Move constructor.
Definition: Wave.h:193
bool ExportWav(std::ostream &file, int bits=16)
Exports a PCM based wav file. Bits can be 8 or 16.
Definition: Wave.h:548
void SetChannels(std::vector< Audio::Channel > channels)
Sets the channel assignment to this wave data.
Definition: Wave.h:434
std::vector< Channel > StandardChannels(int channelcount)
Definition: Basic.h:39
void swap(Collection< T_ > &l, Collection< T_ > &r)
Swaps two collections.
Definition: Collection.h:707
void swap(Wave &l, Wave &r)
Swaps two waves. Should be used unqualified for ADL.
Definition: Wave.h:622
unsigned long operator-(const Iterator &r) const
Definition: Wave.h:167
void WriteInt32(std::ostream &stream, long value)
Writes a 32-bit integer to the stream.
Definition: Stream.h:197
void Assume(float *newdata, unsigned long size, std::vector< Audio::Channel > channels)
Assumes the ownership of the given data.
Definition: Wave.h:312
const float * RawData() const
Returns the raw data pointer.
Definition: Wave.h:370
void Assign(float *newdata, unsigned long size, std::vector< Audio::Channel > channels)
Copies the given data assigns the new data to this object, size is the number of samples.
Definition: Wave.h:274
bool operator>(const Iterator &r)
Definition: Wave.h:119
float operator[](unsigned channel) const
Definition: Wave.h:52
float * data
Data that stores pixels of the wave.
Definition: Wave.h:609
bool ImportWav(std::istream &file, std::vector< Audio::Channel > channels={})
Definition: Wave.h:469
float & Channel(unsigned channel)
Definition: Wave.h:28
int FindChannel(Audio::Channel channel) const
Returns the index of the given channel. If the given channel does not exists, this function returns -...
Definition: Wave.h:442
float Get(unsigned long p, unsigned ch) const
Allows access to individual members.
Definition: Wave.h:395
Sample & reference
Definition: Wave.h:74
void Destroy()
Destroys this wave by setting its size to 0 and freeing the memory used by its data.
Definition: Wave.h:346
Audio::Channel GetChannelType(int channel) const
Returns the type of the channel at the given index.
Definition: Wave.h:425
Iterator operator--(int)
Definition: Wave.h:103
bool operator<=(const Iterator &r)
Definition: Wave.h:123
Iterates the elements of a Wave.
Definition: Wave.h:68
void Assume(float *newdata, unsigned long size)
Assumes the ownership of the data.
Definition: Wave.h:299
bool operator>=(const Iterator &r)
Definition: Wave.h:127
Iterator & operator--()
Definition: Wave.h:97
Iterator operator+(unsigned long diff) const
Definition: Wave.h:148
long ReadInt32(std::istream &stream)
Reads a 32-bit integer from the stream.
Definition: Stream.h:26
void Clear()
Cleans the contents of the buffer by setting every byte it contains to 0.
Definition: Wave.h:340
Wave Duplicate() const
Duplicates this wave, essentially performing the work of copy constructor.
Definition: Wave.h:214
void Assign(float *newdata, unsigned long size)
Copies the given data assigns the new data to this object, size is the number of samples.
Definition: Wave.h:257
std::vector< Audio::Channel > channels
Number of channels.
Definition: Wave.h:615
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Wave(const Wave &)=delete
Copy constructor is disabled.
Wave & operator=(Wave &&other)
Move assignment.
Definition: Wave.h:201
int ReadInt16(std::istream &stream)
Reads a 16-bit integer from the stream.
Definition: Stream.h:44
Iterator & operator=(const Iterator &)=default
void SetSampleRate(unsigned rate)
Sets the number samples per second.
Definition: Wave.h:456
Wave()
Constructs an empty wave data.
Definition: Wave.h:179
unsigned long size
Number of samples in the wave.
Definition: Wave.h:612
bool operator==(const Iterator &r)
Definition: Wave.h:131
Iterator & operator-=(unsigned long diff)
Definition: Wave.h:155
void WriteUInt8(std::ostream &stream, Byte value)
Writes an 8-bit unsigned integer from the stream.
Definition: Stream.h:231
Iterator operator-(unsigned long diff) const
Definition: Wave.h:160
Iterator begin()
Definition: Wave.h:599
unsigned long difference_type
Definition: Wave.h:72
Iterator end()
Definition: Wave.h:603
void Resize(unsigned long size, std::vector< Audio::Channel > channels)
Resizes the wave to the given size and channels.
Definition: Wave.h:225
unsigned samplerate
Sampling rate of the wave.
Definition: Wave.h:618
unsigned GetChannelCount() const
Returns the number of channels that this wave data has.
Definition: Wave.h:420
void Assign(float *newdata)
Copies the given data assigns the new data to this object, size is the number of samples.
Definition: Wave.h:294
Iterator & operator+=(unsigned long diff)
Definition: Wave.h:143
bool ImportWav(const std::string &filename, std::vector< Audio::Channel > channels={})
Imports a PCM based wav file. Leave channels empty to determine them automatically.
Definition: Wave.h:461
Sample()
Definition: Wave.h:26
unsigned char Byte
Represents smallest cell in memory.
Definition: Types.h:9
Channel
Names for channels.
Definition: Basic.h:16
std::random_access_iterator_tag iterator_category
Definition: Wave.h:76
float & operator[](unsigned channel)
Definition: Wave.h:48
float GetLength() const
Returns the length of the wave data in seconds.
Definition: Wave.h:415
bool operator<(const Iterator &r)
Definition: Wave.h:115
Sample * pointer
Definition: Wave.h:75
This class is a container for wave data.
Definition: Wave.h:17
Wave & operator=(const Wave &)=delete
Copy assignment is disabled.
Sample value_type
Definition: Wave.h:73
float & operator()(unsigned long p, unsigned ch)
Allows access to individual members.
Definition: Wave.h:375
float * RawData()
Returns the raw data pointer.
Definition: Wave.h:365
bool ExportWav(const std::string &filename, int bits=16)
Exports a PCM based wav file. Bits can be 8 or 16.
Definition: Wave.h:539
unsigned long GetSize() const
Returns the size of the wave.
Definition: Wave.h:405
Iterator & operator++()
Definition: Wave.h:83
unsigned GetSampleRate() const
Returns the number of samples per second.
Definition: Wave.h:451
float operator()(unsigned long p, unsigned ch) const
Allows access to individual members.
Definition: Wave.h:385
Wave(unsigned long size, unsigned samplerate, std::vector< Audio::Channel > channels={Audio::Channel::Mono})
Constructs a new wave data with the given number of samples and channels.
Definition: Wave.h:185
Sample operator*() const
Definition: Wave.h:111
void WriteString(std::ostream &stream, const std::string &value)
Writes a string without its size.
Definition: Stream.h:285
Byte ReadUInt8(std::istream &stream)
Reads an 8-bit unsigned integer from the stream.
Definition: Stream.h:71
std::string ReadString(std::istream &stream)
Reads a string from a given stream.
Definition: Stream.h:135
~Wave()
Destructor.
Definition: Wave.h:209
void Resize(unsigned long size)
Resizes the wave to the given size.
Definition: Wave.h:240
void Swap(Wave &other)
Swaps this wave with another. This function is used to implement move semantics.
Definition: Wave.h:355
bool operator!=(const Iterator &r)
Definition: Wave.h:135