Klang C++ 0.7.15
Language Reference (draft)
Loading...
Searching...
No Matches
klang::File

#include <klang.h>

Classes

struct  Format
 

Public Member Functions

 Chunk ()=default
 
 Chunk (ID id, unsigned int size=0)
 
 Header ()=default
 
 Header (ID id, ID format, unsigned int size=0)
 
bool isWAV () const
 
bool isAIFF () const
 
 Format ()
 
 operator File::Format () const
 
 Data ()
 
bool load (const char *path)
 
bool load (Memory &memory)
 
bool load ()
 
bool operator>> (variable::buffer &buffer)
 

Static Public Member Functions

template<typename TYPE , int STEP = 1>
static void decode (float *buffer, const TYPE *data, int length)
 

Public Attributes

packed Chunk
 
unsigned int size = 0
 
packed Header: Chunk{ ID format
 
packed WAV
 
packed WAVE
 
packed Format: Chunk { unsigned short AudioFormat
 
unsigned short NumChannels
 
unsigned int SampleRate
 
unsigned int ByteRate
 
unsigned short BlockAlign
 
unsigned short BitsPerSample
 
* format = nullptr
 
packed Data: Chunk { unsigned char data[1] = { 0 }
 
* data = nullptr
 

Detailed Description

Definition at line 7886 of file klang.h.


Class Documentation

◆ klang::File::Format

struct klang::File::Format

Definition at line 7892 of file klang.h.

Class Members
unsigned int bitrate = 0
unsigned int channels = 0
unsigned int samplerate = 0

Member Function Documentation

◆ Chunk() [1/2]

klang::File::Chunk ( )
default

◆ Chunk() [2/2]

klang::File::Chunk ( ID id,
unsigned int size = 0 )
inline

Definition at line 7913 of file klang.h.

7913: id(id), size(size) {}
unsigned int size
Definition klang.h:7910

◆ Data()

klang::File::Data ( )
inline

Definition at line 7949 of file klang.h.

7949: Chunk("data") {}
packed Chunk
Definition klang.h:7898

◆ decode()

template<typename TYPE , int STEP = 1>
static void klang::File::decode ( float * buffer,
const TYPE * data,
int length )
inlinestatic

Definition at line 7992 of file klang.h.

7992 {
7993 if constexpr (std::is_floating_point<TYPE>::value) {
7994 while (length--) {
7995 *buffer++ = *data;
7996 data += STEP;
7997 }
7998 } else {
7999 constexpr unsigned int twoComp = 1U << (sizeof(TYPE) * 8 - 1);
8000 if constexpr (std::numeric_limits<TYPE>::is_signed) {
8001 constexpr float scale = 1.f / twoComp;
8002 while (length--) {
8003 *buffer++ = static_cast<float>(*data) * scale;
8004 data += STEP;
8005 }
8006 } else {
8007 constexpr float scale = 1.f / ((twoComp << 1) - 1);
8008 while (length--) {
8009 *buffer++ = (static_cast<float>(*data) - twoComp) * scale;
8010 data += STEP;
8011 }
8012 }
8013 }
8014 }
klang::interleaved::buffer< 2 > buffer
Definition klang.h:5999

◆ Format()

klang::File::Format ( )
inline

Definition at line 7941 of file klang.h.

7941: Chunk("fmt ", sizeof(Format)) {}
packed Format
Definition klang.h:7934

◆ Header() [1/2]

klang::File::Header ( )
default

◆ Header() [2/2]

klang::File::Header ( ID id,
ID format,
unsigned int size = 0 )
inline

Definition at line 7920 of file klang.h.

7920: Chunk(id, size), format(format) {}

◆ isAIFF()

bool klang::File::isAIFF ( ) const
inline

Definition at line 7923 of file klang.h.

7923{ return id == "FORM" && format == "AIFF"; }

◆ isWAV()

bool klang::File::isWAV ( ) const
inline

Definition at line 7922 of file klang.h.

7922{ return id == "RIFF" && format == "WAVE"; }

◆ load() [1/3]

bool klang::File::load ( )
inline

Definition at line 7964 of file klang.h.

7964 {
7965 if (!memory.ptr)
7966 return false;
7967 memory.rewind();
7968
7969 // header
7970 header = (Header*)memory.ptr;
7971 if (!header->isWAV())
7972 return false;
7973 memory.skip(sizeof(Header));
7974
7975 // other chunks
7976 while (!memory.finished()) {
7977 const Chunk chunk = memory;
7978 memory.rewind(sizeof(Chunk));
7979 if (chunk.id == "fmt ")
7980 format = (Format*)memory.ptr;
7981 else if (chunk.id == "data") {
7982 if (memory.ptr + chunk.size > memory.end)
7983 return false; // corrupt
7984 data = (Data*)memory.ptr;
7985 }
7986 memory.skip(sizeof(Chunk) + chunk.size);
7987 }
7988 return true;
7989 }
packed Data
Definition klang.h:7947
packed Header
Definition klang.h:7917

References klang::Memory::end, klang::Memory::finished(), klang::Memory::ptr, klang::Memory::rewind(), and klang::Memory::skip().

◆ load() [2/3]

bool klang::File::load ( const char * path)
inline

Definition at line 7953 of file klang.h.

7953 {
7954 memory.load(path);
7955 return load();
7956 }
bool load()
Definition klang.h:7964

References klang::Memory::load().

◆ load() [3/3]

bool klang::File::load ( Memory & memory)
inline

Definition at line 7959 of file klang.h.

7959 {
7960 WAV::memory = &memory;
7961 return load();
7962 }

◆ operator File::Format()

klang::File::operator File::Format ( ) const
inline

Definition at line 7943 of file klang.h.

7943{ return { NumChannels, SampleRate, BitsPerSample }; }
unsigned short NumChannels
Definition klang.h:7935
unsigned int SampleRate
Definition klang.h:7936
unsigned short BitsPerSample
Definition klang.h:7939

◆ operator>>()

bool klang::File::operator>> ( variable::buffer & buffer)
inline

Definition at line 8016 of file klang.h.

8016 {
8017 if (!header || !format || !data)
8018 return false;
8019
8020 buffer.resize(data->size / format->BlockAlign);
8021 if (format->AudioFormat == 1) { // PCM (integer)
8022 if (format->BitsPerSample == 8)
8023 decode<unsigned char>(buffer.data(), (unsigned char*)data->data, buffer.size); // 8-bit (unsigned)
8024 else if(format->BitsPerSample == 16)
8025 decode<signed short>(buffer.data(), (signed short*)data->data, buffer.size); // 16-bit (signed)
8026 else if(format->BitsPerSample == 32)
8027 decode<signed int>(buffer.data(), (signed int*)data->data, buffer.size); // 32-bit (signed)
8028 } else if (format->AudioFormat == 3) {
8029 if(format->BitsPerSample == 32)
8030 decode<float>(buffer.data(), (float*)data->data, buffer.size); // 32-bit (float)
8031 }
8032 return true;
8033 }
static void decode(float *buffer, const TYPE *data, int length)
Definition klang.h:7992

References klang::buffer::data(), and klang::buffer::size.

Member Data Documentation

◆ BitsPerSample

unsigned short klang::File::BitsPerSample

Definition at line 7939 of file klang.h.

◆ BlockAlign

unsigned short klang::File::BlockAlign

Definition at line 7938 of file klang.h.

◆ ByteRate

unsigned int klang::File::ByteRate

Definition at line 7937 of file klang.h.

◆ Chunk

packed klang::File::Chunk
Initial value:
{
struct ID {
char id[4] = { 0 };
ID() = default;
ID(const char* id) : id{ id[0], id[1], id[2], id[3] } {}
bool operator==(const ID& id) const {
return memcmp(ID::id, id.id, 4) == 0;
}
bool operator==(const char* const id) const {
return memcmp(ID::id, id, 4) == 0;
}
} id

Definition at line 7898 of file klang.h.

7898 {
7899 struct ID {
7900 char id[4] = { 0 };
7901 ID() = default;
7902 ID(const char* id) : id{ id[0], id[1], id[2], id[3] } {}
7903 bool operator==(const ID& id) const {
7904 return memcmp(ID::id, id.id, 4) == 0;
7905 }
7906 bool operator==(const char* const id) const {
7907 return memcmp(ID::id, id, 4) == 0;
7908 }
7909 } id;

◆ Data

packed klang::File::Data

Definition at line 7946 of file klang.h.

◆ data

* klang::File::data = nullptr

Definition at line 7950 of file klang.h.

◆ Format

packed klang::File::Format

Definition at line 7933 of file klang.h.

◆ format

* klang::File::format = nullptr

Definition at line 7944 of file klang.h.

◆ Header

packed klang::File::Header

Definition at line 7916 of file klang.h.

◆ NumChannels

unsigned short klang::File::NumChannels

Definition at line 7935 of file klang.h.

◆ SampleRate

unsigned int klang::File::SampleRate

Definition at line 7936 of file klang.h.

◆ size

unsigned int klang::File::size = 0

Definition at line 7910 of file klang.h.

◆ WAV

packed klang::File::WAV
Initial value:
{
Memory memory

Definition at line 7926 of file klang.h.

◆ WAVE

packed klang::File::WAVE
Initial value:
{}
} *header = nullptr

Definition at line 7930 of file klang.h.

7930 : File::Header("RIFF", "WAVE") {}
7931 } *header = nullptr;