| 1 |
/* |
| 2 |
Copyright (C) 2002 Steve Harris <[email protected]> |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation; either version 2 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program; if not, write to the Free Software |
| 16 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <stdlib.h> |
| 20 |
#include <math.h> |
| 21 |
#include <fcntl.h> |
| 22 |
#include <sys/types.h> |
| 23 |
#ifndef WIN32 |
| 24 |
#include <sys/mman.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include "blo.h" |
| 28 |
|
| 29 |
/* Create the lookup tables needed to generate the bandlimited signals, we |
| 30 |
* create sin tables (As well as the usual sin, square, saw), which is almost |
| 31 |
* completely pointless, but doesn't cost much ram. |
| 32 |
* |
| 33 |
* Memory use is roughly 8 * table_size * num_of_harmonics, eg. 2048 point |
| 34 |
* tables with 64 harmonics costs ~1Meg. The oscilators will be accurate down |
| 35 |
* to sample_rate / (2 * number_of_harmonics) Hz, below that they will sound a |
| 36 |
* bit soft. |
| 37 |
*/ |
| 38 |
|
| 39 |
blo_h_tables *blo_h_tables_new(int table_size) |
| 40 |
{ |
| 41 |
blo_h_tables *this; |
| 42 |
float *all_tables = NULL; |
| 43 |
float *table; |
| 44 |
float table_size_f = table_size; |
| 45 |
float max; |
| 46 |
unsigned int table_count = 0; |
| 47 |
int i; |
| 48 |
unsigned int h; |
| 49 |
size_t all_tables_size = sizeof(float) * (table_size + BLO_TABLE_WR) |
| 50 |
* (BLO_N_HARMONICS - 1) * 2; |
| 51 |
#ifndef WIN32 |
| 52 |
int shm_fd; |
| 53 |
#endif |
| 54 |
char shm_path[128]; |
| 55 |
|
| 56 |
this = malloc(sizeof(blo_h_tables)); |
| 57 |
this->alloc_size = all_tables_size; |
| 58 |
this->table_size = table_size; |
| 59 |
this->table_mask = table_size - 1; |
| 60 |
this->store_type = BLO_MMAP; |
| 61 |
|
| 62 |
snprintf(shm_path, 128, "/blo-1-%dx%dx%d.tbl", BLO_N_WAVES, |
| 63 |
BLO_N_HARMONICS, table_size + BLO_TABLE_WR); |
| 64 |
#ifndef WIN32 |
| 65 |
if ((shm_fd = shm_open(shm_path, O_RDONLY, 0)) > 0) { |
| 66 |
/* There is an existing SHM segment that matches what we want */ |
| 67 |
|
| 68 |
all_tables = mmap(0, all_tables_size, PROT_READ, MAP_SHARED, |
| 69 |
shm_fd, 0); |
| 70 |
close(shm_fd); |
| 71 |
this->alloc_space = all_tables; |
| 72 |
|
| 73 |
/* Map the pointers to the correct places in SHM */ |
| 74 |
|
| 75 |
/* The zero harmonics tables (trivial) */ |
| 76 |
table = BLO_NEXT_TABLE; |
| 77 |
for (i=0; i<BLO_N_WAVES; i++) { |
| 78 |
this->h_tables[i][0] = table; |
| 79 |
} |
| 80 |
|
| 81 |
/* The 1st harmonic table (trivial) */ |
| 82 |
table = BLO_NEXT_TABLE; |
| 83 |
for (i=0; i<BLO_N_WAVES; i++) { |
| 84 |
this->h_tables[i][1] = table; |
| 85 |
} |
| 86 |
|
| 87 |
/* The sin 2nd+ harmonics sine tables (trivial) */ |
| 88 |
for (h=2; h<BLO_N_HARMONICS; h++) { |
| 89 |
this->h_tables[BLO_SINE][h] = table; |
| 90 |
} |
| 91 |
|
| 92 |
/* The tri 2nd+ harmonics tables */ |
| 93 |
table = this->h_tables[BLO_TRI][1]; |
| 94 |
for (h=2; h<BLO_N_HARMONICS; h++) { |
| 95 |
if (h % 2 == 0) { |
| 96 |
/* Even harmonic - add no partials */ |
| 97 |
this->h_tables[BLO_TRI][h] = table; |
| 98 |
} else { |
| 99 |
/* Odd harmonic add sin(hp)/h^2 */ |
| 100 |
table = BLO_NEXT_TABLE; |
| 101 |
this->h_tables[BLO_TRI][h] = table; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/* The square 2nd+ harmonics tables */ |
| 106 |
table = this->h_tables[BLO_SQUARE][1]; |
| 107 |
for (h=2; h<BLO_N_HARMONICS; h++) { |
| 108 |
if (h % 2 == 0) { |
| 109 |
/* Even harmonic - add no partials */ |
| 110 |
this->h_tables[BLO_SQUARE][h] = table; |
| 111 |
} else { |
| 112 |
/* Odd harmonic add sin(hp)/h */ |
| 113 |
table = BLO_NEXT_TABLE; |
| 114 |
this->h_tables[BLO_SQUARE][h] = table; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/* The saw 2nd+ harmonics tables */ |
| 119 |
for (h=2; h<BLO_N_HARMONICS; h++) { |
| 120 |
/* All harmonics add sin(hp)/h */ |
| 121 |
table = BLO_NEXT_TABLE; |
| 122 |
this->h_tables[BLO_SAW][h] = table; |
| 123 |
} |
| 124 |
|
| 125 |
return this; |
| 126 |
} else if ((shm_fd = shm_open(shm_path, O_CREAT | O_RDWR, 0644)) > 0) { |
| 127 |
/* There is no existing SHM segment, but we can make one */ |
| 128 |
int err = ftruncate(shm_fd, all_tables_size); |
| 129 |
|
| 130 |
if (!err) { |
| 131 |
all_tables = mmap(0, all_tables_size, PROT_READ | PROT_WRITE, |
| 132 |
MAP_SHARED, shm_fd, 0); |
| 133 |
} |
| 134 |
close(shm_fd); |
| 135 |
} |
| 136 |
#endif |
| 137 |
|
| 138 |
/* Fallback case, can't map a SHM segment, just malloc it and suffer */ |
| 139 |
if (!all_tables) { |
| 140 |
all_tables = malloc(all_tables_size); |
| 141 |
this->store_type = BLO_MALLOC; |
| 142 |
} |
| 143 |
this->alloc_space = all_tables; |
| 144 |
|
| 145 |
/* Calculate the harmonic amplitudes and place the index pointers */ |
| 146 |
|
| 147 |
/* Make a zero harmonics table (trivial) */ |
| 148 |
table = BLO_NEXT_TABLE; |
| 149 |
for (i=0; i<table_size + BLO_TABLE_WR; i++) { |
| 150 |
table[i] = 0.0f; |
| 151 |
} |
| 152 |
for (i=0; i<BLO_N_WAVES; i++) { |
| 153 |
this->h_tables[i][0] = table; |
| 154 |
} |
| 155 |
|
| 156 |
/* Make a 1st harmonic table (trivial) */ |
| 157 |
table = BLO_NEXT_TABLE; |
| 158 |
for (i=0; i<table_size + BLO_TABLE_WR; i++) { |
| 159 |
table[i] = BLO_SIN_GEN((float)i); |
| 160 |
} |
| 161 |
for (i=0; i<BLO_N_WAVES; i++) { |
| 162 |
this->h_tables[i][1] = table; |
| 163 |
} |
| 164 |
|
| 165 |
/* Make the sin 2nd+ harmonics tables (trivial) */ |
| 166 |
for (h=2; h<BLO_N_HARMONICS; h++) { |
| 167 |
this->h_tables[BLO_SINE][h] = table; |
| 168 |
} |
| 169 |
|
| 170 |
/* Make the tri 2nd+ harmonics tables */ |
| 171 |
table = this->h_tables[BLO_TRI][1]; |
| 172 |
for (h=2; h<BLO_N_HARMONICS; h++) { |
| 173 |
if (h % 2 == 0) { |
| 174 |
/* Even harmonic - add no partials */ |
| 175 |
this->h_tables[BLO_TRI][h] = table; |
| 176 |
} else { |
| 177 |
float sign = 1.0f; |
| 178 |
if (h % 4 == 3) { |
| 179 |
sign = -1.0f; |
| 180 |
} |
| 181 |
/* Odd harmonic add sin(hp)/h^2 */ |
| 182 |
table = BLO_NEXT_TABLE; |
| 183 |
this->h_tables[BLO_TRI][h] = table; |
| 184 |
for (i=0; i<table_size + BLO_TABLE_WR; i++) { |
| 185 |
table[i] = this->h_tables[BLO_TRI][h - 1][i] + |
| 186 |
sign * BLO_SIN_GEN((float)i * (float)h) / |
| 187 |
((float)h * (float) h); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/* Make the square 2nd+ harmonics tables */ |
| 193 |
table = this->h_tables[BLO_SQUARE][1]; |
| 194 |
for (h=2; h<BLO_N_HARMONICS; h++) { |
| 195 |
if (h % 2 == 0) { |
| 196 |
/* Even harmonic - add no partials */ |
| 197 |
this->h_tables[BLO_SQUARE][h] = table; |
| 198 |
} else { |
| 199 |
/* Odd harmonic add sin(hp)/h */ |
| 200 |
table = BLO_NEXT_TABLE; |
| 201 |
this->h_tables[BLO_SQUARE][h] = table; |
| 202 |
for (i=0; i<table_size + BLO_TABLE_WR; i++) { |
| 203 |
table[i] = |
| 204 |
this->h_tables[BLO_SQUARE][h - 1][i] + |
| 205 |
BLO_SIN_GEN((float)i * (float)h) / (float)h; |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/* Make the saw 2nd+ harmonics tables */ |
| 211 |
for (h=2; h<BLO_N_HARMONICS; h++) { |
| 212 |
/* All harmonics add sin(hp)/h */ |
| 213 |
table = BLO_NEXT_TABLE; |
| 214 |
this->h_tables[BLO_SAW][h] = table; |
| 215 |
for (i=0; i<table_size + BLO_TABLE_WR; i++) { |
| 216 |
table[i] = this->h_tables[BLO_SAW][h - 1][i] + |
| 217 |
BLO_SIN_GEN((float)i * (float)h) / (float)h; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
/* Normalise table levels */ |
| 222 |
for (h=1; h<table_count; h++) { |
| 223 |
table = all_tables + (h * (table_size + BLO_TABLE_WR)); |
| 224 |
max = 0.0f; |
| 225 |
for (i=0; i<table_size; i++) { |
| 226 |
if (fabs(table[i]) > max) { |
| 227 |
max = fabs(table[i]); |
| 228 |
} |
| 229 |
} |
| 230 |
max = 1.0f / max; |
| 231 |
for (i=0; i<table_size + BLO_TABLE_WR; i++) { |
| 232 |
table[i] *= max; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
#ifndef WIN32 |
| 237 |
msync(all_tables, all_tables_size, MS_ASYNC); |
| 238 |
#endif |
| 239 |
|
| 240 |
return this; |
| 241 |
} |
| 242 |
|
| 243 |
void blo_h_tables_free(blo_h_tables *tables) |
| 244 |
{ |
| 245 |
if (tables->store_type == BLO_MMAP) { |
| 246 |
#ifndef WIN32 |
| 247 |
munmap(tables->alloc_space, tables->alloc_size); |
| 248 |
#endif |
| 249 |
} else { |
| 250 |
free(tables->alloc_space); |
| 251 |
} |
| 252 |
free(tables); |
| 253 |
} |
| 254 |
|
| 255 |
blo_h_osc *blo_h_new(blo_h_tables *tables, unsigned int wave, float sample_rate) |
| 256 |
{ |
| 257 |
blo_h_osc *this = malloc(sizeof(blo_h_osc)); |
| 258 |
|
| 259 |
this->tables = tables; |
| 260 |
this->wave = wave; |
| 261 |
this->sample_rate = sample_rate; |
| 262 |
this->nyquist = sample_rate * 0.49f; |
| 263 |
this->ph.all = 0; |
| 264 |
this->ph_coef = ((float)(tables->table_size) * 65536.0f) / sample_rate; |
| 265 |
this->ph_mask = tables->table_size * 65536 - 1; |
| 266 |
this->table_mask = tables->table_mask; |
| 267 |
this->table_size = tables->table_size; |
| 268 |
this->table = tables->h_tables[0][0]; |
| 269 |
this->table_b = tables->h_tables[0][0]; |
| 270 |
|
| 271 |
return this; |
| 272 |
} |
| 273 |
|
| 274 |
void blo_h_free(blo_h_osc *osc) |
| 275 |
{ |
| 276 |
free(osc); |
| 277 |
} |