ptttl v0.3.0
C implementation of a PTTTL parser. PTTTL is a superset of NOKIA's RTTTL that adds polyphony & vibrato.
ptttl_sample_generator.h
Go to the documentation of this file.
1 
16 #ifndef PTTTL_SAMPLE_GENERATOR_H
17 #define PTTTL_SAMPLE_GENERATOR_H
18 
19 
20 #include <stdint.h>
21 #include "ptttl_parser.h"
22 
23 
24 #ifdef __cplusplus
25  extern "C" {
26 #endif
27 
28 
34 #ifndef PTTTL_SAMPLE_GENERATOR_NUM_HARMONICS
35 #define PTTTL_SAMPLE_GENERATOR_NUM_HARMONICS 16
36 #endif // PTTTL_SAMPLE_GENERATOR_NUM_HARMONICS
37 
41 #define PTTTL_SAMPLE_GENERATOR_CONFIG_DEFAULT {.sample_rate=44100u, .attack_samples=100u, \
42  .decay_samples=500u, .amplitude=0.8f}
43 
44 
48 typedef enum
49 {
55  WAVEFORM_TYPE_COUNT
57 
58 
70 typedef float (*ptttl_waveform_generator_t)(float x, float p, unsigned int s, void *wgendata);
71 
91 typedef void (*ptttl_waveform_note_setup_t)(uint32_t channel_idx,
92  uint8_t note_number,
93  unsigned int sample_rate,
94  void *wgendata);
95 
101 typedef struct
102 {
109  void *wgendata;
111 
115 typedef struct
116 {
118  uint32_t vibrato_frequency;
119  uint32_t vibrato_variance;
120  unsigned int sine_index;
121  unsigned int start_sample;
122  unsigned int num_samples;
123  unsigned int attack;
124  unsigned int decay;
125  unsigned int note_number;
126  float pitch_hz;
127  float phasor_state;
128  float sample_error;
130 
134 typedef struct
135 {
136  unsigned int sample_rate;
137  unsigned int attack_samples;
138  unsigned int decay_samples;
139  float amplitude;
141 
145 typedef struct
146 {
147  unsigned int current_sample;
149  uint8_t channel_finished[PTTTL_MAX_CHANNELS_PER_FILE];
151  ptttl_parser_t *parser;
153 
154 
167 
182  uint32_t channel, ptttl_waveform_type_e type);
183 
196  uint32_t channel,
197  const ptttl_waveform_t *waveform);
198 
214  uint32_t *num_samples, int16_t *samples);
215 
216 
217 #ifdef __cplusplus
218  }
219 #endif
220 
221 #endif // PTTTL_SAMPLE_GENERATOR_H
Parser for RTTTL (Ring Tone Text Transfer Language) and PTTTL (Polyphonic Tone Text Transfer Language...
#define PTTTL_MAX_CHANNELS_PER_FILE
Definition: ptttl_parser.h:46
ptttl_waveform_type_e
Definition: ptttl_sample_generator.h:49
@ WAVEFORM_TYPE_NOKIA
Mimics the sound of a Nokia 3310 phone.
Definition: ptttl_sample_generator.h:54
@ WAVEFORM_TYPE_TRIANGLE
Generates a triangle wave.
Definition: ptttl_sample_generator.h:51
@ WAVEFORM_TYPE_SINE
Generates a sine wave.
Definition: ptttl_sample_generator.h:50
@ WAVEFORM_TYPE_SQUARE
Generates a square wave.
Definition: ptttl_sample_generator.h:53
@ WAVEFORM_TYPE_SAWTOOTH
Generates a sawtooth wave.
Definition: ptttl_sample_generator.h:52
float(* ptttl_waveform_generator_t)(float x, float p, unsigned int s, void *wgendata)
Definition: ptttl_sample_generator.h:70
int ptttl_sample_generator_set_custom_waveform(ptttl_sample_generator_t *generator, uint32_t channel, const ptttl_waveform_t *waveform)
void(* ptttl_waveform_note_setup_t)(uint32_t channel_idx, uint8_t note_number, unsigned int sample_rate, void *wgendata)
Definition: ptttl_sample_generator.h:91
int ptttl_sample_generator_generate(ptttl_sample_generator_t *generator, uint32_t *num_samples, int16_t *samples)
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)
Definition: ptttl_sample_generator.h:116
unsigned int start_sample
The sample index on which this note started.
Definition: ptttl_sample_generator.h:121
float phasor_state
Phasor state for vibrato (frequency modulation)
Definition: ptttl_sample_generator.h:127
uint32_t vibrato_variance
Vibrato variance, in HZ.
Definition: ptttl_sample_generator.h:119
uint32_t vibrato_frequency
Vibrato frequency, in HZ.
Definition: ptttl_sample_generator.h:118
unsigned int sine_index
Monotonically increasing index for sinf() function, note pitch.
Definition: ptttl_sample_generator.h:120
unsigned int num_samples
Number of samples this note runs for.
Definition: ptttl_sample_generator.h:122
ptttl_waveform_t waveform
Waveform generator functions.
Definition: ptttl_sample_generator.h:117
float sample_error
Accumulated sample count error for this note.
Definition: ptttl_sample_generator.h:128
unsigned int note_number
Piano key number for this note, 1-88.
Definition: ptttl_sample_generator.h:125
float pitch_hz
Note pitch in Hz.
Definition: ptttl_sample_generator.h:126
unsigned int attack
Note attack length, in samples.
Definition: ptttl_sample_generator.h:123
unsigned int decay
Note decay length, in samples.
Definition: ptttl_sample_generator.h:124
Definition: ptttl_parser.h:167
Definition: ptttl_sample_generator.h:135
float amplitude
Amplitude of generated samples between 0.0-1.0, with 1.0 being full volume.
Definition: ptttl_sample_generator.h:139
unsigned int sample_rate
Sampling rate in samples per second (Hz)
Definition: ptttl_sample_generator.h:136
unsigned int attack_samples
no. of samples to ramp from 0 to full volume, at note start
Definition: ptttl_sample_generator.h:137
unsigned int decay_samples
no. of samples to ramp from full volume to 0, at note end
Definition: ptttl_sample_generator.h:138
Definition: ptttl_sample_generator.h:146
Definition: ptttl_sample_generator.h:102
void * wgendata
Definition: ptttl_sample_generator.h:109
ptttl_waveform_generator_t wgen
Per-sample waveform function (must not be NULL)
Definition: ptttl_sample_generator.h:103
ptttl_waveform_note_setup_t note_setup
Per-note setup callback, may be NULL.
Definition: ptttl_sample_generator.h:104