ptttl v0.2.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 {
54  WAVEFORM_TYPE_COUNT
56 
57 
67 typedef float (*ptttl_waveform_generator_t)(float x, float p, unsigned int s);
68 
69 
73 typedef struct
74 {
76  uint32_t vibrato_frequency;
77  uint32_t vibrato_variance;
78  unsigned int sine_index;
79  unsigned int start_sample;
80  unsigned int num_samples;
81  unsigned int attack;
82  unsigned int decay;
83  unsigned int note_number;
84  float pitch_hz;
85  float phasor_state;
87 
91 typedef struct
92 {
93  unsigned int sample_rate;
94  unsigned int attack_samples;
95  unsigned int decay_samples;
96  float amplitude;
98 
102 typedef struct
103 {
104  unsigned int current_sample;
106  uint8_t channel_finished[PTTTL_MAX_CHANNELS_PER_FILE];
108  ptttl_parser_t *parser;
110 
111 
124 
139  uint32_t channel, ptttl_waveform_type_e type);
140 
155  uint32_t channel, ptttl_waveform_generator_t wgen);
156 
172  uint32_t *num_samples, int16_t *samples);
173 
174 
175 #ifdef __cplusplus
176  }
177 #endif
178 
179 #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
float(* ptttl_waveform_generator_t)(float x, float p, unsigned int s)
Definition: ptttl_sample_generator.h:67
ptttl_waveform_type_e
Definition: ptttl_sample_generator.h:49
@ 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
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_custom_waveform(ptttl_sample_generator_t *generator, uint32_t channel, ptttl_waveform_generator_t wgen)
int ptttl_sample_generator_set_waveform(ptttl_sample_generator_t *generator, uint32_t channel, ptttl_waveform_type_e type)
Definition: ptttl_sample_generator.h:74
unsigned int start_sample
The sample index on which this note started.
Definition: ptttl_sample_generator.h:79
float phasor_state
Phasor state for vibrato (frequency modulation)
Definition: ptttl_sample_generator.h:85
uint32_t vibrato_variance
Vibrato variance, in HZ.
Definition: ptttl_sample_generator.h:77
uint32_t vibrato_frequency
Vibrato frequency, in HZ.
Definition: ptttl_sample_generator.h:76
unsigned int sine_index
Monotonically increasing index for sinf() function, note pitch.
Definition: ptttl_sample_generator.h:78
unsigned int num_samples
Number of samples this note runs for.
Definition: ptttl_sample_generator.h:80
ptttl_waveform_generator_t wgen
Waveform generator function.
Definition: ptttl_sample_generator.h:75
unsigned int note_number
Piano key number for this note, 1-88.
Definition: ptttl_sample_generator.h:83
float pitch_hz
Note pitch in Hz.
Definition: ptttl_sample_generator.h:84
unsigned int attack
Note decay length, in samples.
Definition: ptttl_sample_generator.h:81
unsigned int decay
Note decay length, in samples.
Definition: ptttl_sample_generator.h:82
Definition: ptttl_parser.h:159
Definition: ptttl_sample_generator.h:92
float amplitude
Amplitude of generated samples between 0.0-1.0, with 1.0 being full volume.
Definition: ptttl_sample_generator.h:96
unsigned int sample_rate
Sampling rate in samples per second (Hz)
Definition: ptttl_sample_generator.h:93
unsigned int attack_samples
no. of samples to ramp from 0 to full volume, at note start
Definition: ptttl_sample_generator.h:94
unsigned int decay_samples
no. of samples to ramp from full volume to 0, at note end
Definition: ptttl_sample_generator.h:95
Definition: ptttl_sample_generator.h:103