Gorgon Game Engine
Sound.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include <memory>
5 
6 #include "../Types.h"
7 #include "Base.h"
8 #include "../Containers/Wave.h"
9 
10 namespace Gorgon {
11  namespace Resource {
12  class File;
13  class Reader;
14 
17  class Sound : public Base {
18  public:
19 
21  Sound() {}
22 
24  virtual ~Sound() {}
25 
27  virtual GID::Type GetGID() const override { return GID::Sound; }
28 
29  const Containers::Wave &GetWave() const {
30  return data;
31  }
32 
34  return data;
35  }
36 
38  void Destroy() {
39  isloaded=false;
40  }
41 
43  bool Load();
44 
46  bool IsLoaded() const { return isloaded; }
47 
49  static Sound *LoadResource(std::weak_ptr<File> file, std::shared_ptr<Reader> reader, unsigned long size);
50 
54  void Assign(const Containers::Wave &wave) {
55  data=wave.Duplicate();
56 
57  isloaded = true;
58  }
59 
62  void Assume(Containers::Wave &wave) {
63  data = std::move(wave);
64 
65  isloaded = true;
66  }
67 
68  int GetBits() const {
69  return bits;
70  }
71 
75  void SetBits(int bits) {
76  this->bits = bits;
77 
78  checkfmt();
79  }
80 
82  bool IsPCM() const {
83  return pcm;
84  }
85 
88  void SetPCM(bool pcm) {
89  this->pcm = pcm;
90 
91  checkfmt();
92  }
93 
96  return compression;
97  }
98 
103  this->compression = compression;
104 
105  checkfmt();
106  }
107 
108 
109  protected:
110 
112  bool load(std::shared_ptr<Reader> reader, unsigned long size, bool forceload);
113 
114  void save(Writer &writer) const override;
115 
117  void checkfmt() const;
118 
121  unsigned long entrypoint = -1;
122 
124  std::shared_ptr<Reader> reader;
125 
127  bool isloaded = false;
128 
131 
133  bool lateloading = false;
134 
135  bool pcm = true;
136 
139 
141  int bits = 16;
142  };
143  }
144 }
Gorgon::Containers::Wave::GetBytes
unsigned long GetBytes() const
Returns the size of the wave in bytes.
Definition: Wave.h:410
Gorgon::Resource::Sound::Assume
void Assume(Containers::Wave &wave)
Assumes the contents of the given wave as wave data.
Definition: Sound.h:62
Gorgon::Resource::Sound::lateloading
bool lateloading
Whether to load this sound during initial loading.
Definition: Sound.h:133
Gorgon::Resource::Sound::~Sound
virtual ~Sound()
Destructor.
Definition: Sound.h:24
Sound.h
Gorgon::Animation::basic_Storage
This class stores animations as a part of itself so that it can be moved around as a value rather tha...
Definition: Storage.h:21
Gorgon::Graphics::basic_ScalableObjectProvider::GetController
SizeController GetController() const override
Returns the size controller.
Definition: ScalableObject.h:181
Gorgon::Graphics::SizeController
This class allows control over a sizable object.
Definition: Graphics.h:161
Gorgon::Resource::GID::File
constexpr Type File
File.
Definition: GID.h:84
Gorgon::Resource::Initialize
void Initialize()
Definition: Resource.cpp:18
Gorgon::String::From
std::enable_if< decltype(gorgon__enum_tr_loc(T_()))::isupgradedenum, std::string >::type From(const T_ &e)
Definition: Enum.h:303
Gorgon::Resource::Sound::SetCompression
void SetCompression(GID::Type compression)
Changes the compression type of this resource.
Definition: Sound.h:102
Gorgon::Resource::GID::ScalableObject_Props
constexpr Type ScalableObject_Props
Definition: GID.h:248
File.h
Gorgon::Graphics::basic_ScalableObjectProvider::GetBase
A_ * GetBase() const
Returns the base component. Could return nullptr.
Definition: ScalableObject.h:176
Gorgon::Graphics::SizeController::Tiling
Tiling
Controls how a direction is tiled.
Definition: Graphics.h:164
Gorgon::Graphics::basic_TextureAnimationProvider
Definition: TextureAnimation.h:137
Gorgon::Resource::Writer::GetStream
std::ostream & GetStream()
This should be last resort, use if the actual stream is needed.
Definition: Writer.h:96
Gorgon::Containers::Wave::SetChannels
void SetChannels(std::vector< Audio::Channel > channels)
Sets the channel assignment to this wave data.
Definition: Wave.h:434
Gorgon::Audio::StandardChannels
std::vector< Channel > StandardChannels(int channelcount)
Definition: Basic.h:39
Gorgon::Graphics::BitmapAnimationProvider
basic_TextureAnimationProvider< Bitmap, basic_TextureAnimation, basic_AnimationFrame< Bitmap > > BitmapAnimationProvider
Definition: TextureAnimation.h:520
Gorgon::Resource::GID::Null
constexpr Type Null
Null resource.
Definition: GID.h:105
Gorgon::Resource::Sound::bits
int bits
Number of bits per sample.
Definition: Sound.h:141
Gorgon::Resource::ScalableObject::MoveOutProvider
IScalableObjectProvider & MoveOutProvider() override
This function moves this animation provider into a new provider.
Definition: ScalableObject.cpp:209
Base.h
Gorgon::Resource::SaveAnimation
void SaveAnimation(Writer &writer, const Graphics::RectangularAnimationProvider &object)
Saves a given generic rectangular animation as resource.
Definition: Resource.cpp:22
Gorgon::Resource::ScalableObject::LoadResource
static ScalableObject * LoadResource(std::weak_ptr< File > file, std::shared_ptr< Reader > reader, unsigned long size)
This function loads a tinted object resource from the file.
Definition: ScalableObject.cpp:34
Reader.h
Gorgon::Resource::GID::Sound_Props
constexpr Type Sound_Props
Definition: GID.h:220
Gorgon::Resource::Writer::WriteChunkHeader
void WriteChunkHeader(GID::Type type, unsigned long size)
Writes the header of a chunk.
Definition: Writer.h:364
Gorgon::Resource::Image::SaveThis
static void SaveThis(Writer &writer, const Graphics::Bitmap &bmp, GID::Type type=GID::Image)
This function can be used to save a bitmap as image resource without modifying it.
Definition: Image.cpp:171
Gorgon::Resource::StackedObject::SaveThis
static void SaveThis(Writer &writer, const Graphics::IStackedObjectProvider &provider)
Definition: StackedObject.cpp:151
Gorgon::Resource::Line::SaveThis
static void SaveThis(Writer &writer, const Graphics::ILineProvider &provider)
Definition: Line.cpp:175
Gorgon::Resource::Animation::SaveThis
static void SaveThis(Writer &writer, const Graphics::BitmapAnimationProvider &anim, GID::Type type=GID::Animation, std::function< void(Writer &writer)> extra={})
Saves the given animation as a resource.
Definition: Animation.cpp:100
Gorgon::Encoding::Flac
FLAC Flac
Definition: FLAC.cpp:419
Image.h
Gorgon::Containers::Wave::Get
float Get(unsigned long p, unsigned ch) const
Allows access to individual members.
Definition: Wave.h:395
Gorgon::Resource::Sound::Sound
Sound()
Default constructor.
Definition: Sound.h:21
Gorgon::Containers::Wave::Destroy
void Destroy()
Destroys this wave by setting its size to 0 and freeing the memory used by its data.
Definition: Wave.h:346
Gorgon::Containers::Wave::GetChannelType
Audio::Channel GetChannelType(int channel) const
Returns the type of the channel at the given index.
Definition: Wave.h:425
Gorgon::Resource::GID::Image
constexpr Type Image
Image resource.
Definition: GID.h:149
Gorgon::Resource::Writer::WriteObjectStart
Marker WriteObjectStart(const Base &base)
Writes the start of an object. Should have a matching WriteEnd with the returned marker.
Definition: File.cpp:211
Gorgon::Graphics::IRectangleProvider
Interface for RectangleProviders.
Definition: Rectangle.h:12
Gorgon::Resource::Sound::load
bool load(std::shared_ptr< Reader > reader, unsigned long size, bool forceload)
Loads the sound from the data stream.
Definition: Sound.cpp:38
Gorgon::Graphics::IScalableObjectProvider
For ease of use in resource system.
Definition: ScalableObject.h:11
Gorgon::Resource::LoadError::UnsupportedCompression
@ UnsupportedCompression
If the compression type in the file is not supported.
Definition: Reader.h:46
Gorgon::Graphics::Placement
Placement
Defines how an object is placed in a 2D axis system.
Definition: Graphics.h:79
Gorgon::Resource::Writer::WriteUInt32
void WriteUInt32(unsigned long value)
Writes a 32-bit unsigned integer to the stream.
Definition: Writer.h:158
Gorgon::Encoding::FLAC::Decode
void Decode(std::istream &input, Containers::Wave &wave)
Decodes given FLAC compressed data and fills a wave container.
Definition: FLAC.cpp:342
Gorgon::Resource::DataItem::InitializeLoaders
static void InitializeLoaders()
This function will initialize data loaders.
Definition: DataItems.cpp:9
Gorgon::Graphics::ITintedObjectProvider
For ease of use in resource system.
Definition: TintedObject.h:11
Gorgon::Resource::MaskedObject::SaveThis
static void SaveThis(Writer &writer, const Graphics::IMaskedObjectProvider &provider)
Definition: MaskedObject.cpp:141
Gorgon::Utils::ASSERT_FALSE
void ASSERT_FALSE(const std::string &message, int skip=1, int depth=4)
Definition: Assert.h:192
Gorgon::Resource::Sound::GetBits
int GetBits() const
Definition: Sound.h:68
Gorgon::Resource::GID::Sound_Wave
constexpr Type Sound_Wave
Definition: GID.h:221
Gorgon::Resource::Sound::IsLoaded
bool IsLoaded() const
Returns whether the sound data is loaded.
Definition: Sound.h:46
Gorgon::Containers::Wave::Duplicate
Wave Duplicate() const
Duplicates this wave, essentially performing the work of copy constructor.
Definition: Wave.h:214
Null.h
Gorgon::Resource::ScalableObject::save
void save(Writer &writer) const override
Definition: ScalableObject.cpp:133
Gorgon::Graphics::basic_ScalableObjectProvider
This object creates a scalable object from a graphic object.
Definition: ScalableObject.h:122
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
ScalableObject.h
Gorgon::Resource::Writer::WriteChunkStart
Marker WriteChunkStart(GID::Type type)
Writes the start of a chunk. Should have a matching WriteEnd.
Definition: Writer.h:351
Gorgon::Resource::GID::FLAC
constexpr Type FLAC
LZMA compression.
Definition: GID.h:122
Gorgon::Resource::ScalableObject::animmoveout
virtual Graphics::RectangularAnimationStorage animmoveout() override
Definition: ScalableObject.cpp:205
Gorgon::Graphics::basic_ScalableObjectProvider::SetBase
void SetBase(A_ *value)
Sets the base provider, ownership semantics will not be changed.
Definition: ScalableObject.h:191
Gorgon::Resource::Sound::IsPCM
bool IsPCM() const
Returns if the wave data will be saved as PCM data.
Definition: Sound.h:82
Rectangle.h
Gorgon::Containers::Wave::SetSampleRate
void SetSampleRate(unsigned rate)
Sets the number samples per second.
Definition: Wave.h:456
MaskedObject.h
Gorgon::Resource::Sound::save
void save(Writer &writer) const override
Definition: Sound.cpp:159
Gorgon::Containers::Collection
Collection is a container for reference typed objects.
Definition: Collection.h:21
AnimationStorage.h
Gorgon::Resource::Writer::WriteArray
void WriteArray(const T_ *data, unsigned long size)
Writes an array to the file.
Definition: Writer.h:288
Gorgon::Resource::GID::None
constexpr Type None
Empty, different from Null resource.
Definition: GID.h:81
Gorgon::Graphics::ILineProvider
Interface for LineProviders.
Definition: Line.h:12
Gorgon::Graphics::Bitmap
This object contains an bitmap image.
Definition: Bitmap.h:25
Gorgon::Resource::Sound::SetBits
void SetBits(int bits)
Sets the number of bits per sample.
Definition: Sound.h:75
Gorgon::Resource::GID::Animation
constexpr Type Animation
Definition: GID.h:187
Gorgon::Resource::Base
This class is the base for all Gorgon Resources.
Definition: Base.h:20
Gorgon::Resource::Sound::LoadResource
static Sound * LoadResource(std::weak_ptr< File > file, std::shared_ptr< Reader > reader, unsigned long size)
This function loads a sound resource from the given file.
Definition: Sound.cpp:7
Gorgon::Resource::Sound::pcm
bool pcm
Definition: Sound.h:135
Gorgon::Graphics::IStackedObjectProvider
For ease of use in resource system.
Definition: StackedObject.h:10
Gorgon::Resource::Sound::GetWave
Containers::Wave & GetWave()
Definition: Sound.h:33
Gorgon::Containers::Wave::Resize
void Resize(unsigned long size, std::vector< Audio::Channel > channels)
Resizes the wave to the given size and channels.
Definition: Wave.h:225
Gorgon::Graphics::ScalableObjectProvider
basic_ScalableObjectProvider< RectangularAnimationProvider > ScalableObjectProvider
Definition: ScalableObject.h:275
Gorgon::Graphics::SizeController::Horizontal
Tiling Horizontal
Horizontal tiling mode.
Definition: Graphics.h:470
Gorgon::Graphics::SizeController::Place
Placement Place
Placement method.
Definition: Graphics.h:476
Gorgon::Resource::Sound::isloaded
bool isloaded
Whether this sound is loaded or not.
Definition: Sound.h:127
Gorgon::Containers::Collection::GetCount
long GetCount() const
Returns number of elements.
Definition: Collection.h:236
Gorgon::Resource::Sound::data
Containers::Wave data
Sound data.
Definition: Sound.h:138
Gorgon::Resource::Writer
This class allows resource objects to save their data to a stream.
Definition: Writer.h:59
TintedObject.h
Gorgon::Resource::GID::Sound_Fmt
constexpr Type Sound_Fmt
Definition: GID.h:224
Gorgon::Resource::ScalableObject
This is a tinted object resource.
Definition: ScalableObject.h:16
Gorgon::Resource::ScalableObject::SaveThis
static void SaveThis(Writer &writer, const Graphics::IScalableObjectProvider &provider)
Definition: ScalableObject.cpp:152
Gorgon::Containers::Wave::GetChannelCount
unsigned GetChannelCount() const
Returns the number of channels that this wave data has.
Definition: Wave.h:420
Gorgon::Resource::Sound::checkfmt
void checkfmt() const
Checks if the format of the file is well-formed.
Definition: Sound.cpp:222
Gorgon::Resource::Sound::Destroy
void Destroy()
Destroys the data stored in the sound.
Definition: Sound.h:38
Gorgon::Resource::Sound::SetPCM
void SetPCM(bool pcm)
Set whether the wave data should be saved in PCM format.
Definition: Sound.h:88
Gorgon::Resource::LoadError
This class represents a loading error.
Definition: Reader.h:16
Gorgon::Resource::GID::Sound_Cmp_Wave
constexpr Type Sound_Cmp_Wave
Definition: GID.h:222
Gorgon::Resource::Sound::GetWave
const Containers::Wave & GetWave() const
Definition: Sound.h:29
Gorgon::Byte
unsigned char Byte
Represents smallest cell in memory.
Definition: Types.h:9
Gorgon::Audio::Channel
Channel
Names for channels.
Definition: Basic.h:16
Gorgon::Resource::Writer::WriteEnd
void WriteEnd(Marker &marker)
This function performs writes necessary to end a chunk that is represented by the marker.
Definition: Writer.h:373
Gorgon::Graphics::RectangularAnimationProvider
This class provides rectangular animations.
Definition: Animations.h:48
Gorgon::Resource::GID::Sound_Channels
constexpr Type Sound_Channels
Definition: GID.h:225
Gorgon::Graphics::RectangularAnimationStorage
Gorgon::Animation::basic_Storage< RectangularAnimationProvider > RectangularAnimationStorage
Definition: Animations.h:374
Gorgon::Resource::Writer::WriteInt16
void WriteInt16(int value)
Writes a 16-bit integer to the stream.
Definition: Writer.h:167
Gorgon::Resource::TintedObject::SaveThis
static void SaveThis(Writer &writer, const Graphics::ITintedObjectProvider &provider)
Definition: TintedObject.cpp:147
Gorgon::Resource::Sound::GetCompression
GID::Type GetCompression() const
Returns the compression type of this resource.
Definition: Sound.h:95
Gorgon::Resource::Sound::compression
GID::Type compression
Compression mode of this sound resource.
Definition: Sound.h:130
Gorgon::Graphics::ScalableBitmapAnimationProvider
basic_ScalableObjectProvider< BitmapAnimationProvider > ScalableBitmapAnimationProvider
Definition: ScalableObject.h:278
Gorgon::Graphics::IMaskedObjectProvider
For ease of use in resource system.
Definition: MaskedObject.h:10
Gorgon::Encoding::FLAC::Encode
void Encode(const Containers::Wave &input, std::ostream &output, int bps=16)
Encodes the given wave data to FLAC compressed data.
Definition: FLAC.cpp:291
Line.h
Gorgon::Resource::Writer::WriteBool
void WriteBool(bool value)
Writes a boolean value. In resource 1.0, booleans are stored as 32bit integers.
Definition: Writer.h:220
Gorgon::Resource::GID::Type
Type to store GID information.
Definition: GID.h:23
Gorgon::Containers::Wave
This class is a container for wave data.
Definition: Wave.h:17
Gorgon::Resource::Base::children
Containers::Collection< Base > children
Child objects that this resource object have.
Definition: Base.h:162
Gorgon::Resource::Sound::Assign
void Assign(const Containers::Wave &wave)
Assigns the sound to the copy of the given data.
Definition: Sound.h:54
Gorgon::Resource::Rectangle::SaveThis
static void SaveThis(Writer &writer, const Graphics::IRectangleProvider &provider)
Definition: Rectangle.cpp:250
Gorgon::Resource::Writer::WriteEnum32
void WriteEnum32(E_ value)
Writes an enumeration as 32-bit integer to the stream.
Definition: Writer.h:140
Gorgon::Resource::Sound::GetGID
virtual GID::Type GetGID() const override
04010000h (Extended, Sound)
Definition: Sound.h:27
Gorgon::Containers::Wave::RawData
float * RawData()
Returns the raw data pointer.
Definition: Wave.h:365
Gorgon::Resource::Writer::WriteUInt8
void WriteUInt8(Byte value)
Writes an 8-bit unsigned integer to the stream.
Definition: Writer.h:194
Gorgon::Resource::Writer::WriteGID
void WriteGID(GID::Type value)
Writes a GID to the given stream.
Definition: Writer.h:303
Gorgon::Resource::Sound::reader
std::shared_ptr< Reader > reader
Used to handle late loading.
Definition: Sound.h:124
Gorgon::Resource::Sound::entrypoint
unsigned long entrypoint
Entry point of this resource within the physical file.
Definition: Sound.h:121
Gorgon::Resource::Sound::Load
bool Load()
Loads the sound from the disk. If sound is already loaded, this function will return true.
Definition: Sound.cpp:18
Gorgon::Resource::Null::SaveThis
static void SaveThis(Writer &writer)
Definition: Null.cpp:14
StackedObject.h
Gorgon::Containers::Wave::GetSize
unsigned long GetSize() const
Returns the size of the wave.
Definition: Wave.h:405
Gorgon::Containers::Wave::GetSampleRate
unsigned GetSampleRate() const
Returns the number of samples per second.
Definition: Wave.h:451
Gorgon::Resource::GID::Sound
constexpr Type Sound
Definition: GID.h:219
Gorgon::Graphics::SizeController::Vertical
Tiling Vertical
Vertical tiling mode.
Definition: Graphics.h:473
Animation.h
Gorgon::Resource::ScalableObject::ScalableObject
ScalableObject()
Creates a new empty tinted object.
Definition: ScalableObject.h:43
Gorgon::Resource::Sound
This is sound resource.
Definition: Sound.h:17
Gorgon::Resource::GID::ScalableObject
constexpr Type ScalableObject
Definition: GID.h:247
DataItems.h