Beatmup
Beatmup::Audio::Signal Class Reference

An audio signal. More...

#include <signal.h>

Inheritance diagram for Beatmup::Audio::Signal:
Beatmup::Fragments::SequenceToolkit< Signal > Beatmup::Fragments::Sequence Beatmup::Object

Classes

class  IncompatibleFormat
 Communicates an error when inserting a incompatible fragment into a Signal. More...
 
class  Meter
 Signal dynamics meter. More...
 
class  Pointer
 Implements a Sequence::Pointer for audio signals. More...
 
class  Reader
 Provides reading access to the signal. More...
 
class  Source
 Audio::Source reading samples from a given Signal. More...
 
class  Writer
 Provides writing access to the signal. More...
 

Public Member Functions

 Signal (Context &context, AudioSampleFormat format, int sampleRate, unsigned char channels, float defaultFragmentLenSec=DEFAULT_FRAGMENT_LENGTH_SEC)
 Creates an empty signal. More...
 
void insert (const Signal &signal, dtime time)
 Inserts a Signal into the current signal at a specific time moment. More...
 
void reserve (dtime length)
 Prolongates the sequence if necessary, to ensure given length. More...
 
void saveWAV (const char *filename)
 Stores the signal to a PCM-encoded WAV file. More...
 
unsigned char getChannelCount () const
 
AudioSampleFormat getSampleFormat () const
 
ContextgetContext () const
 
- Public Member Functions inherited from Beatmup::Fragments::SequenceToolkit< Signal >
Signal * copy (dtime fromTime, dtime toTime) const
 
- Public Member Functions inherited from Beatmup::Fragments::Sequence
const dtime getDuration () const
 Returns sequence duration in number of samples. More...
 
bool isEmpty () const
 Returns true if sequence contains no samples. More...
 
void clear ()
 Removes the content of the sequence making it empty (of zero duration). More...
 
void shrink (dtime timeLeft, dtime timeRight)
 Shrinks the sequence to given time bounds. More...
 
Sequencecopy (dtime fromTime, dtime toTime) const
 Copies a given piece of the current sequence into new Sequence. More...
 
void insert (const Sequence &sequence, dtime time)
 Inserts a Sequence at a given position in time. More...
 
void remove (dtime fromTime, dtime toTime)
 Erases a part of the sequence between two given time moments. More...
 
void split (dtime time, Sequence *left, Sequence *right) const
 Splits the sequence in two at a specific time. More...
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 

Static Public Member Functions

static SignalloadWAV (Context &ctx, const char *fileName)
 
static SignalloadWAV (Context &ctx, InputStream &inputStream)
 

Static Public Attributes

static const int DEFAULT_FRAGMENT_LENGTH_SEC = 5
 

Protected Member Functions

virtual SignalcreateEmpty () const
 Initializes an empty sequence, used to bootstrap copying operations. More...
 
- Protected Member Functions inherited from Beatmup::Fragments::Sequence
 Sequence ()
 
virtual ~Sequence ()
 
void concatenate (Fragment &fragment, dtime offset, dtime length)
 Adds a new fragment at the end of the sequence. More...
 
void syncPointers ()
 Resets pointers once the sequence changes to keep them consistent. More...
 

Private Attributes

Contextctx
 
AudioSampleFormat format
 sample format More...
 
const dtime defaultFragmentSize
 
int sampleRate
 sampling frequency More...
 
unsigned char channelCount
 number of channels More...
 

Detailed Description

An audio signal.

Definition at line 36 of file signal.h.

Constructor & Destructor Documentation

◆ Signal()

Signal::Signal ( Context context,
AudioSampleFormat  format,
int  sampleRate,
unsigned char  channels,
float  defaultFragmentLenSec = DEFAULT_FRAGMENT_LENGTH_SEC 
)

Creates an empty signal.

Parameters
contextA Context instance the new signal is associated to
formatSample format of the new signal
sampleRateSample rate, Hz
channelsNumber of channels
defaultFragmentLenSecDefault fragment length in seconds

Definition at line 36 of file signal.cpp.

36  :
37  ctx(context),
38  format(format),
39  defaultFragmentSize(floorf_fast(defaultFragmentLenSec * sampleRate)),
42 {
44 }
AudioSampleFormat format
sample format
Definition: signal.h:187
unsigned char channelCount
number of channels
Definition: signal.h:191
int sampleRate
sampling frequency
Definition: signal.h:190
const dtime defaultFragmentSize
Definition: signal.h:189
#define BEATMUP_ASSERT_DEBUG(C)
Definition: exception.h:163
int floorf_fast(float x)
Definition: utils.hpp:26
JNIEnv jobject jint jint jint channels

Member Function Documentation

◆ createEmpty()

Signal * Signal::createEmpty ( ) const
protectedvirtual

Initializes an empty sequence, used to bootstrap copying operations.

Implements Beatmup::Fragments::Sequence.

Definition at line 31 of file signal.cpp.

31  {
33 }
Signal(Context &context, AudioSampleFormat format, int sampleRate, unsigned char channels, float defaultFragmentLenSec=DEFAULT_FRAGMENT_LENGTH_SEC)
Creates an empty signal.
Definition: signal.cpp:36

◆ insert()

void Signal::insert ( const Signal signal,
dtime  time 
)

Inserts a Signal into the current signal at a specific time moment.

Parameters
signalThe signal to insert
timeThe time moment to insert the signal at

Definition at line 47 of file signal.cpp.

47  {
48  if (signal.channelCount != channelCount || signal.format != format)
49  throw Signal::IncompatibleFormat(signal, *this);
50  Sequence::insert(signal, time);
51 }
Communicates an error when inserting a incompatible fragment into a Signal.
Definition: signal.h:177
jlong jlong jint time

◆ reserve()

void Signal::reserve ( dtime  length)

Prolongates the sequence if necessary, to ensure given length.

Definition at line 54 of file signal.cpp.

54  {
55  int curDur;
56  while ((curDur = getDuration()) < length) {
58  newbie->zero();
59  concatenate(*newbie, 0, std::min(defaultFragmentSize, length - curDur));
60  }
61 }
const dtime getDuration() const
Returns sequence duration in number of samples.
Definition: sequence.h:123
void concatenate(Fragment &fragment, dtime offset, dtime length)
Adds a new fragment at the end of the sequence.
CustomPoint< numeric > min(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:724
Beatmup::CustomPipeline::TaskHolder * newbie

◆ saveWAV()

void Signal::saveWAV ( const char *  filename)

Stores the signal to a PCM-encoded WAV file.

Definition at line 117 of file signal.cpp.

117  {
118  // open file
119  std::ofstream file(filename, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
120  if (!file.is_open())
121  throw IOError(filename, "Unable to open for writing");
122 
123  // init and write header
124  unsigned char sampleSize = AUDIO_SAMPLE_SIZE[format];
125  WAV::Header header;
126  header.set(sampleRate, sampleSize * 8, channelCount, getDuration() * channelCount * sampleSize);
127  file.write((char*)&header, sizeof(header));
128 
129  // write out
130  Reader pointer(*this, 0);
131  while (pointer.hasData() && !file.eof()) {
132  if (file.fail())
133  throw IOError(filename, "Failed while writing");
134  const void* data;
135  dtime length = pointer.acquireBuffer(data);
136  file.write((const char*)data, length * channelCount * sampleSize);
137  pointer.releaseBuffer();
138  pointer.jump(length);
139  }
140 }
void set(uint32_t sampleRate, uint16_t bitsPerSample, uint16_t channelCount, uint32_t dataSize)
const int AUDIO_SAMPLE_SIZE[]
int dtime
discrete time
Definition: basic_types.h:37
JNIEnv jlong jstring filename
JNIEnv jlong jint out

◆ loadWAV() [1/2]

Signal * Signal::loadWAV ( Context ctx,
const char *  fileName 
)
static

Definition at line 64 of file signal.cpp.

64  {
65  // open file
66  FileInputStream stream(filename);
67  if (!stream.isOpen())
68  throw IOError(filename, "Unable to open for reading");
69  return Signal::loadWAV(ctx, stream);
70 }
static Signal * loadWAV(Context &ctx, const char *fileName)
Definition: signal.cpp:64
InputStream reading from a file.
Definition: input_stream.h:54

◆ loadWAV() [2/2]

Signal * Signal::loadWAV ( Context ctx,
InputStream inputStream 
)
static

Definition at line 72 of file signal.cpp.

72  {
73  // read header
74  WAV::Header header;
75  stream(&header, sizeof(header));
77 
78  // get sample format
80  switch (header.bitsPerSample) {
81  case 8:
82  format = Int8;
83  break;
84  case 16:
85  format = Int16;
86  break;
87  case 32:
88  format = Int32;
89  break;
90  default:
91  throw WAV::InvalidWavFile("Incorrect WAV file: unsupported BPS");
92  }
93 
94  // check number of channels (insanity check, basically)
95  if (header.numChannels <= 0 || header.numChannels > 255)
96  throw WAV::InvalidWavFile("Incorrect WAV file: unsupported channel count");
97 
98  // create signal
99  Signal* signal = new Signal(ctx, format, header.sampleRate, header.numChannels);
100  dtime totalTime = header.dataSizeBytes / (header.numChannels * header.bitsPerSample / 8);
101  signal->reserve(totalTime);
102 
103  // read data using pointer
104  Writer pointer(*signal, 0);
105  while (pointer.hasData() && !stream.eof()) {
106  void* data;
107  dtime length = pointer.acquireBuffer(data);
108  stream(data, length * header.numChannels * AUDIO_SAMPLE_SIZE[format]);
109  pointer.releaseBuffer();
110  pointer.jump(length);
111  }
112 
113  return signal;
114 }
An audio signal.
Definition: signal.h:36
void reserve(dtime length)
Prolongates the sequence if necessary, to ensure given length.
Definition: signal.cpp:54
Communicates an error related to a WAV file content.
Definition: wav_utilities.h:68
static void check(Header &header)
AudioSampleFormat
Format of audio samples.
@ Int8
signed integer, 8 bit per sample
@ Int32
signed integer, 32 bit per sample
@ Int16
signed integer, 16 bit per sample

◆ getChannelCount()

unsigned char Beatmup::Audio::Signal::getChannelCount ( ) const
inline
Returns
number of channels in the signal.

Definition at line 232 of file signal.h.

232 { return channelCount; }

◆ getSampleFormat()

AudioSampleFormat Beatmup::Audio::Signal::getSampleFormat ( ) const
inline
Returns
sample format of the signal.

Definition at line 237 of file signal.h.

237 { return format; }

◆ getContext()

Context& Beatmup::Audio::Signal::getContext ( ) const
inline
Returns
a Context instance the signal is attached to.

Definition at line 242 of file signal.h.

242 { return ctx; }

Member Data Documentation

◆ ctx

Context& Beatmup::Audio::Signal::ctx
private

Definition at line 185 of file signal.h.

◆ format

AudioSampleFormat Beatmup::Audio::Signal::format
private

sample format

Definition at line 187 of file signal.h.

◆ defaultFragmentSize

const dtime Beatmup::Audio::Signal::defaultFragmentSize
private

Definition at line 189 of file signal.h.

◆ sampleRate

int Beatmup::Audio::Signal::sampleRate
private

sampling frequency

Definition at line 190 of file signal.h.

◆ channelCount

unsigned char Beatmup::Audio::Signal::channelCount
private

number of channels

Definition at line 191 of file signal.h.

◆ DEFAULT_FRAGMENT_LENGTH_SEC

const int Beatmup::Audio::Signal::DEFAULT_FRAGMENT_LENGTH_SEC = 5
static

Definition at line 197 of file signal.h.


The documentation for this class was generated from the following files: