-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbeeper.h
134 lines (116 loc) · 3.6 KB
/
beeper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#pragma once
#ifndef BEEPER_H
#define BEEPER_H
/*
beeper.h -- simple square-wave beeper
TODO: docs
## zlib/libpng license
Copyright (c) 2018 Andre Weissflog
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in a
product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
// error-accumulation precision boost
#define BEEPER_FIXEDPOINT_SCALE (1000)
// DC adjust buffer size
#define BEEPER_DCADJ_BUFLEN (256)
// initialization parameters
typedef struct {
float tick_hz;
int sound_hz;
float base_volume;
} beeper_desc_t;
// beeper state
typedef struct {
int state;
int period;
int counter;
float base_volume;
float volume;
float sample;
float dcadj_sum;
uint32_t dcadj_pos;
float dcadj_buf[BEEPER_DCADJ_BUFLEN];
} beeper_t;
// initialize beeper instance
void beeper_init(beeper_t* beeper, const beeper_desc_t* desc);
// reset the beeper instance
void beeper_reset(beeper_t* beeper);
// set current on/off state
static inline void beeper_set(beeper_t* beeper, bool state) {
beeper->state = state ? 1 : 0;
}
// toggle current state (on->off or off->on)
static inline void beeper_toggle(beeper_t* beeper) {
beeper->state = 1 - beeper->state;
}
// set current volume 0.0 to 1.0
static inline void beeper_set_volume(beeper_t* beeper, float vol) {
beeper->volume = vol;
}
// tick the beeper, return true if a new sample is ready
bool beeper_tick(beeper_t* beeper);
#ifdef __cplusplus
} /* extern "C" */
#endif
/*-- IMPLEMENTATION ----------------------------------------------------------*/
#ifdef CHIPS_IMPL
#include <string.h>
#ifndef CHIPS_ASSERT
#include <assert.h>
#define CHIPS_ASSERT(c) assert(c)
#endif
void beeper_init(beeper_t* b, const beeper_desc_t* desc) {
CHIPS_ASSERT(b && desc);
CHIPS_ASSERT((desc->tick_hz > 0) && (desc->sound_hz > 0));
b->period = ((int)(desc->tick_hz * BEEPER_FIXEDPOINT_SCALE)) / desc->sound_hz;
b->counter = b->period;
b->base_volume = desc->base_volume;
b->volume = 1.0f;
}
void beeper_reset(beeper_t* b) {
CHIPS_ASSERT(b);
b->state = 0;
b->counter = b->period;
b->sample = 0;
}
/* DC adjustment filter from StSound, this moves an "offcenter"
signal back to the zero-line (e.g. the volume-level output
from the chip simulation which is >0.0 gets converted to
a +/- sample value)
*/
static void _beeper_dcadjust(beeper_t* bp, float s) {
bp->dcadj_sum -= bp->dcadj_buf[bp->dcadj_pos];
bp->dcadj_sum += s;
bp->dcadj_buf[bp->dcadj_pos] = s;
bp->dcadj_pos = (bp->dcadj_pos + 1) & (BEEPER_DCADJ_BUFLEN-1);
}
bool beeper_tick(beeper_t* bp) {
_beeper_dcadjust(bp, (float)bp->state * bp->volume * bp->base_volume);
/* generate a new sample? */
bp->counter -= BEEPER_FIXEDPOINT_SCALE;
if (bp->counter <= 0) {
bp->counter += bp->period;
bp->sample = bp->dcadj_sum / BEEPER_DCADJ_BUFLEN;
return true;
}
return false;
}
#endif /* CHIPS_IMPL */
#endif /* BEEPER_H */