ptttl v0.2.0
C implementation of a PTTTL parser. PTTTL is a superset of NOKIA's RTTTL that adds polyphony & vibrato.
Loading...
Searching...
No Matches
Classes | Macros | Typedefs | Enumerations | Functions
ptttl_sample_generator.h File Reference

Converts the output of ptttl_parse_next() into a stream of signed 16-bit audio samples suitable for a WAV file. Samples can be obtained one at a time, at your leisure. More...

#include <stdint.h>
#include "ptttl_parser.h"

Go to the source code of this file.

Classes

struct  ptttl_note_stream_t
 
struct  ptttl_sample_generator_config_t
 
struct  ptttl_sample_generator_t
 

Macros

#define PTTTL_SAMPLE_GENERATOR_CONFIG_DEFAULT
 

Typedefs

typedef float(* ptttl_waveform_generator_t) (float x, float p, unsigned int s)
 

Enumerations

enum  ptttl_waveform_type_e {
  WAVEFORM_TYPE_SINE = 0 , WAVEFORM_TYPE_TRIANGLE , WAVEFORM_TYPE_SAWTOOTH , WAVEFORM_TYPE_SQUARE ,
  WAVEFORM_TYPE_COUNT
}
 

Functions

int ptttl_sample_generator_create (ptttl_parser_t *parser, ptttl_sample_generator_t *generator, ptttl_sample_generator_config_t *config)
 
int ptttl_sample_generator_set_waveform (ptttl_sample_generator_t *generator, uint32_t channel, ptttl_waveform_type_e type)
 
int ptttl_sample_generator_set_custom_waveform (ptttl_sample_generator_t *generator, uint32_t channel, ptttl_waveform_generator_t wgen)
 
int ptttl_sample_generator_generate (ptttl_sample_generator_t *generator, uint32_t *num_samples, int16_t *samples)
 

Detailed Description

Converts the output of ptttl_parse_next() into a stream of signed 16-bit audio samples suitable for a WAV file. Samples can be obtained one at a time, at your leisure.

Requires ptttl_parser.c

Requires stdint.h and memset() from string.h

See https://github.com/eriknyquist/ptttl for more details about PTTTL.

Author
Erik Nyquist

Macro Definition Documentation

◆ PTTTL_SAMPLE_GENERATOR_CONFIG_DEFAULT

#define PTTTL_SAMPLE_GENERATOR_CONFIG_DEFAULT
Value:
{.sample_rate=44100u, .attack_samples=100u, \
.decay_samples=500u, .amplitude=0.8f}

ptttl_sample_generator_config_t object initialization with sane defaults

Typedef Documentation

◆ ptttl_waveform_generator_t

typedef float(* ptttl_waveform_generator_t) (float x, float p, unsigned int s)

Callback function for a waveform generator

Parameters
xPhase; current position on the waveform, in turns (0.0 through 1.0)
pWave frequency in Hz
sSampling rate in Hz
Returns
Value for the given position, in the range -1.0 through 1.0

Enumeration Type Documentation

◆ ptttl_waveform_type_e

Enumerates all available built-in waveform types

Enumerator
WAVEFORM_TYPE_SINE 

Generates a sine wave.

WAVEFORM_TYPE_TRIANGLE 

Generates a triangle wave.

WAVEFORM_TYPE_SAWTOOTH 

Generates a sawtooth wave.

WAVEFORM_TYPE_SQUARE 

Generates a square wave.

Function Documentation

◆ ptttl_sample_generator_create()

int ptttl_sample_generator_create ( ptttl_parser_t parser,
ptttl_sample_generator_t generator,
ptttl_sample_generator_config_t config 
)

Initialize a sample generator instance for a specific PTTTL/RTTTL source text

Parameters
parserPointer to initialized PTTTL parser object
generatorPointer to generator instance to initialize
configPointer to sample generator configuration data
Returns
0 if successful, -1 if an error occurred. Call ptttl_parser_error for an error description if -1 is returned.

◆ ptttl_sample_generator_generate()

int ptttl_sample_generator_generate ( ptttl_sample_generator_t generator,
uint32_t *  num_samples,
int16_t *  samples 
)

Generate the next audio sample(s) for an initialized generator object

Parameters
generatorPointer to initialized generator object
num_samplesPointer to number of samples to generate. If successful, then this pointer is re-used to write out the actual number of samples generated.
samplesPointer to location to store sample values. The caller is expected to provide at least (sizeof(int16_t) * num_samples) bytes of storage for the generated samples.
Returns
0 if successful, 1 if all samples have been generated, and -1 if an error occurred. Call ptttl_parser_error for an error description if -1 is returned.

◆ ptttl_sample_generator_set_custom_waveform()

int ptttl_sample_generator_set_custom_waveform ( ptttl_sample_generator_t generator,
uint32_t  channel,
ptttl_waveform_generator_t  wgen 
)

Set a custom waveform generator function for a specific channel of the sample generator

Parameters
generatorPointer to initialized generator object
channelChannel index to set the waveform type for. Channel index is 0-based, and channels are indexed in the order in which they appear in the PTTTL source text. For example, channel 0 is the first channel that appears in the source text, channel 1 is the second channel, and so on.
wgenWaveform generator function to use for the specified channel.
Returns
0 if successful, -1 if an error occurred. Call ptttl_parser_error for an error description if -1 is returned.

◆ ptttl_sample_generator_set_waveform()

int ptttl_sample_generator_set_waveform ( ptttl_sample_generator_t generator,
uint32_t  channel,
ptttl_waveform_type_e  type 
)

Set the built-in waveform type for a specific channel of the sample generator

Parameters
generatorPointer to initialized generator object
channelChannel index to set the waveform type for. Channel index is 0-based, and channels are indexed in the order in which they appear in the PTTTL source text. For example, channel 0 is the first channel that appears in the source text, channel 1 is the second channel, and so on.
typeBuilt-in waveform type to generate for the specified channel.
Returns
0 if successful, -1 if an error occurred. Call ptttl_parser_error for an error description if -1 is returned.