18 Commits
Author SHA1 Message Date
Earle F. Philhower, III 3044568f85 Update version 2024-12-31 10:30:46 -08:00
Earle F. Philhower, III 8adeb31d20 Automate version bumps, avoid manual mistakes (#725) 2024-12-31 10:30:06 -08:00
Kurt Haenenandkurt.haenen b0a06edde2 Fixing crash on MP3 playback on ESP8266. (#723)
Co-authored-by: kurt.haenen <kurt.haenen@quintux.com>
2024-12-25 13:19:16 -08:00
Earle F. Philhower, III cdd07c92c2 Only block tsf.h on Xtensa ESP32, not RISCV (#722)
Fixes #721
2024-12-19 14:33:24 -08:00
Earle F. Philhower, III aafabf2367 Ensure MP3 can safely exit on OOM (#716)
Fixes #715.  Good catch, @EternityForest
2024-12-19 09:33:01 -08:00
Earle F. Philhower, III 90b83fb95e Update README.md 2024-12-18 15:11:36 -08:00
Earle F. Philhower, III fbe69b8cd8 Fix AAC HR decode on RP2040
U64 big/little endian mismatch for ARM cores.  Remove the whole PowerPC support option as there aren't any commerically significant PPC embedded systems still being developed.
2024-12-18 14:39:07 -08:00
Earle F. Philhower, III 2f43b3b57d Update README.md 2024-10-14 17:48:14 -07:00
Earle F. Philhower, III 0e5870a440 Update library.properties
dot_a_linkage seems to cause issues with CI
2024-10-08 18:24:41 -07:00
Earle F. Philhower, III 7e50a43b4b Update README.md 2024-10-08 18:13:22 -07:00
Earle F. Philhower, III 1278b10717 Update library.properties 2024-10-08 17:51:13 -07:00
Roman Lut 52c49afbc0 NoDAC and PDM documentation update for ESP32 (#519) 2024-10-08 17:49:03 -07:00
TorrenteroandEdgar d32d8b4cc8 Performance improvement: ~13% faster on ESP32 and ~21% faster on ESP8266 (empirically tested) (#554)
* Performance improvement
- new tsf_note_on_fast returning playIndex and removing lowpass filter setup since it is not being processed in tsf_voice_render_fast anyway but is consuming a lot of cpu
- new tsf_note_off_fast using playIndex to stop voices faster
- Removing lowpass filter setup from tsf_voice_render_fast since it is not being processed
- Math tweaks trying to avoid complex math operations (specially conversions) at voice render stage
- Precalculating *F32P32 values to avoid doing them at voice render stage
* Fixing data type
* Initializing play indexes
---------
Co-authored-by: Edgar <edgar.coria@xema.tech>
2024-10-08 17:45:57 -07:00
Earle F. Philhower, III 235bcb12de Fix UTF-8 warning in RCSL doc 2024-10-08 13:59:49 -07:00
Earle F. Philhower, III a4a974363d Update Arduino Lint GH Action 2024-10-08 13:56:03 -07:00
jmr-esandRASMUSSEN Joern 96dc79c9fa AudioOutputPWM: added amplification (#605)
Co-authored-by: RASMUSSEN Joern <joern.rasmussen-ext@hexagon.com>
2024-10-08 13:50:43 -07:00
Piotr Gaczkowski 50bc3f7279 Add MCLK support for RP2040 (#707) 2024-10-06 16:54:01 -07:00
Earle F. Philhower, III 79208f6701 Fix GCC14-detected warnings (#711)
* Fix pedantic GCC14 warnings on calloc order
* Ensure AAC read-in doesn't overflow internal structures when the file is corrupted/illegal.
2024-10-06 16:53:22 -07:00
19 changed files with 281 additions and 103 deletions

No files matched your search

+1 -1
View File
@@ -116,7 +116,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: arduino/arduino-lint-action@v1
- uses: arduino/arduino-lint-action@v2
with:
library-manager: 'update'
+15 -3
View File
@@ -1,6 +1,10 @@
# ESP8266Audio - supports ESP8266 & ESP32 & Raspberry Pi Pico RP2040 [![Gitter](https://badges.gitter.im/ESP8266Audio/community.svg)](https://gitter.im/ESP8266Audio/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
# ESP8266Audio - supports ESP8266 & ESP32 & Raspberry Pi Pico RP2040 and Pico 2 RP2350 [![Gitter](https://badges.gitter.im/ESP8266Audio/community.svg)](https://gitter.im/ESP8266Audio/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
Arduino library for parsing and decoding MOD, WAV, MP3, FLAC, MIDI, AAC, and RTTL files and playing them on an I2S DAC or even using a software-simulated delta-sigma DAC with dynamic 32x-128x oversampling.
# Raspberry Pi Pico (RP2040 and RP2350) Users
Consider using [BackgroundAudio](https://github.com/earlephilhower/BackgroundAudio) instead of this library as it can provide a simpler usage model and better results and performance on the Pico by using an interrupt-based, frame-aligned output model.
# ESP8266 and ESP32 Users
ESP8266 is fully supported and most mature, but ESP32 is also mostly there with built-in DAC as well as external ones.
For real-time, autonomous speech synthesis, check out [ESP8266SAM](https://github.com/earlephilhower/ESP8266SAM), a library which uses this one and a port of an ancient formant-based synthesis program to allow your ESP8266 to talk with low memory and no network required.
@@ -138,7 +142,7 @@ AudioGeneratorRTTTL: Enjoy the pleasures of monophonic, 4-octave ringtones on y
## AudioOutput classes
AudioOutput: Base class for all output drivers. Takes a sample at a time and returns true/false if there is buffer space for it. If it returns false, it is the calling object's (AudioGenerator's) job to keep the data that didn't fit and try again later.
AudioOutputI2S: Interface for any I2S 16-bit DAC. Sends stereo or mono signals out at whatever frequency set. Tested with Adafruit's I2SDAC and a Beyond9032 DAC from eBay. Tested up to 44.1KHz. To use the internal DAC on ESP32, instantiate this class as `AudioOutputI2S(0,1)`, see example `PlayMODFromPROGMEMToDAC` and code in [AudioOutputI2S.cpp](src/AudioOutputI2S.cpp#L29) for details.
AudioOutputI2S: Interface for any I2S 16-bit DAC. Sends stereo or mono signals out at whatever frequency set. Tested with Adafruit's I2SDAC and a Beyond9032 DAC from eBay. Tested up to 44.1KHz. To use the internal DAC on ESP32, instantiate this class as `AudioOutputI2S(0,AudioOutputI2S::INTERNAL_DAC)`, see example `PlayMODFromPROGMEMToDAC` and code in [AudioOutputI2S.cpp](src/AudioOutputI2S.cpp#L29) for details. To use the hardware Pulse Density Modulation (PDM) on ESP32, instantiate this class as `AudioOutputI2S(0,AudioOutputI2S::INTERNAL_PDM)`. For both later cases, default output pins are GPIO25 and GPIO26.
AudioOutputI2SNoDAC: Abuses the I2S interface to play music without a DAC. Turns it into a 32x (or higher) oversampling delta-sigma DAC. Use the schematic below to drive a speaker or headphone from the I2STx pin (i.e. Rx). Note that with this interface, depending on the transistor used, you may need to disconnect the Rx pin from the driver to perform serial uploads. Mono-only output, of course.
@@ -190,7 +194,8 @@ Use the `AudioOutputI2S*No*DAC` object instead of the `AudioOutputI2S` in your c
ESP8266-GND ------------------+ | +------+ K|
| | | E|
ESP8266-I2SOUT (Rx) -----/\/\/\--+ | \ R|
| +-|
or ESP32 DOUT pin | +-|
|
USB 5V -----------------------------+
You may also want to add a 220uF cap from USB5V to GND just to help filter out any voltage droop during high volume playback.
@@ -203,12 +208,19 @@ ESP8266-RX(I2S tx) -- Resistor (~1K ohm, not critical) -- 2N3904 Base
ESP8266-GND -- 2N3904 Emitter
USB-5V -- Speaker + Terminal
2N3904-Collector -- Speaker - Terminal
*For ESP32, default output pin is GPIO22. Note that GPIO25 ang GPIO26 are occupied by wclk/bclk and can not be used.
```
*NOTE*: A prior version of this schematic had a direct connection from the ESP8266 to the base of the transistor. While this does provide the maximum amplitude, it also can draw more current from the 8266 than is safe, and can also cause the transistor to overheat.
As of the latest ESP8266Audio release, with the software delta-sigma DAC the LRCLK and BCLK pins *can* be used by an application. Simply use normal `pinMode` and `digitalWrite` or `digitalRead` as desired.
### Hardware PDM on ESP32
Hardware PDM outputs 128 * 48Khz pulses regardless of sample rate.
It seems that currently hardware PDM either does not output constant One at maximum sample level, or does not output 3.3V voltage at pulse sound is not as loud as desired. You may consider using software delta-sigma DAC instead.
### High pitched buzzing with the 1-T circuit
The 1-T amp can _NOT_ drive any sort of amplified speaker. If there is a power or USB input to the speaker, or it has lights or Bluetooth or a battery, it can _NOT_ be used with this circuit.
+6 -6
View File
@@ -12,15 +12,15 @@
],
"repository": {
"type": "git",
"url": "https://github.com/earlephilhower/ESP8266Audio"
"url": "https://github.com/earlephilhower/ESP8266Audio"
},
"version": "1.9.9",
"version": "2.0.0",
"homepage": "https://github.com/earlephilhower/ESP8266Audio",
"frameworks": "Arduino",
"examples": [
"examples/*/*.ino"
],
"build": {
"libLDFMode": "deep"
}
}
"build": {
"libLDFMode": "deep"
}
}
+1 -1
View File
@@ -1,5 +1,5 @@
name=ESP8266Audio
version=1.9.9
version=2.0.0
author=Earle F. Philhower, III
maintainer=Earle F. Philhower, III
sentence=Audio file and I2S sound playing routines for ESP8266, ESP32, and Raspberry Pi Pico RP2040
+3 -2
View File
@@ -443,8 +443,9 @@ int AudioGeneratorMIDI::PlayMIDI()
for (tgnum = 0; tgnum < num_tonegens; ++tgnum) { /* find which generator is playing it */
tg = &tonegen[tgnum];
if (tg->playing && tg->track == tracknum && tg->note == trk->note) {
tsf_note_off (g_tsf, tg->instrument, tg->note);
tsf_note_off_fast (g_tsf, tg->instrument, tg->note, tg->playIndex);
tg->playing = false;
tg->playIndex = -1;
trk->tonegens[tgnum] = false;
}
}
@@ -476,7 +477,7 @@ int AudioGeneratorMIDI::PlayMIDI()
if (tg->instrument != midi_chan_instrument[trk->chan]) { /* new instrument for this generator */
tg->instrument = midi_chan_instrument[trk->chan];
}
tsf_note_on (g_tsf, tg->instrument, tg->note, trk->velocity / 127.0); // velocity = 0...127
tg->playIndex = tsf_note_on_fast (g_tsf, tg->instrument, tg->note, trk->velocity / 127.0); // velocity = 0...127
} else {
++notes_skipped;
}
+4 -1
View File
@@ -94,10 +94,13 @@ class AudioGeneratorMIDI : public AudioGenerator
unsigned long earliest_time = 0;
struct tonegen_status { /* current status of a tone generator */
bool playing; /* is it playing? */
bool playing; /* is it playing? */
char track; /* if so, which track is the note from? */
char note; /* what note is playing? */
char instrument; /* what instrument? */
int playIndex; /* is index provided?
Unique identifier generated when note starts playing.
This help us to turn the note off faster */
} tonegen[MAX_TONEGENS];
struct track_status { /* current processing point of a MIDI track */
+9
View File
@@ -27,6 +27,9 @@ AudioGeneratorMP3::AudioGeneratorMP3()
file = NULL;
output = NULL;
buff = NULL;
synth = NULL;
frame = NULL;
stream = NULL;
nsCountMax = 1152/32;
madInitted = false;
}
@@ -37,6 +40,9 @@ AudioGeneratorMP3::AudioGeneratorMP3(void *space, int size): preallocateSpace(sp
file = NULL;
output = NULL;
buff = NULL;
synth = NULL;
frame = NULL;
stream = NULL;
nsCountMax = 1152/32;
madInitted = false;
}
@@ -51,6 +57,9 @@ AudioGeneratorMP3::AudioGeneratorMP3(void *buff, int buffSize, void *stream, int
file = NULL;
output = NULL;
buff = NULL;
synth = NULL;
frame = NULL;
stream = NULL;
nsCountMax = 1152/32;
madInitted = false;
}
+13 -7
View File
@@ -116,7 +116,7 @@ bool AudioOutputI2S::SetPinout(int bclk, int wclk, int dout, int mclk)
bclkPin = bclk;
wclkPin = wclk;
doutPin = dout;
#ifdef ESP32
#if defined(ESP32) || defined(ARDUINO_ARCH_RP2040)
mclkPin = mclk;
if (i2sOn)
return SetPinout();
@@ -184,6 +184,8 @@ bool AudioOutputI2S::SetMclk(bool enabled){
if (output_mode == INTERNAL_DAC || output_mode == INTERNAL_PDM)
return false; // Not allowed
use_mclk = enabled;
#elif defined(ARDUINO_ARCH_RP2040)
use_mclk = enabled;
#endif
return true;
@@ -310,12 +312,16 @@ bool AudioOutputI2S::begin(bool txDAC)
#elif defined(ARDUINO_ARCH_RP2040)
(void)txDAC;
if (!i2sOn) {
i2s.setBCLK(bclkPin);
i2s.setDATA(doutPin);
if (swap_clocks) {
i2s.swapClocks();
}
i2s.begin(hertz);
i2s.setSysClk(hertz);
i2s.setBCLK(bclkPin);
i2s.setDATA(doutPin);
i2s.setMCLK(mclkPin);
i2s.setMCLKmult(256);
if (swap_clocks) {
i2s.swapClocks();
}
i2s.setBitsPerSample(bps);
i2s.begin(hertz);
}
#endif
i2sOn = true;
+2 -2
View File
@@ -71,8 +71,8 @@ bool AudioOutputMixerStub::stop()
AudioOutputMixer::AudioOutputMixer(int buffSizeSamples, AudioOutput *dest) : AudioOutput()
{
buffSize = buffSizeSamples;
leftAccum = (int32_t*)calloc(sizeof(int32_t), buffSize);
rightAccum = (int32_t*)calloc(sizeof(int32_t), buffSize);
leftAccum = (int32_t*)calloc(buffSize, sizeof(int32_t));
rightAccum = (int32_t*)calloc(buffSize, sizeof(int32_t));
for (int i=0; i<maxStubs; i++) {
stubAllocated[i] = false;
stubRunning[i] = false;
+3
View File
@@ -89,6 +89,9 @@ bool AudioOutputPWM::ConsumeSample(int16_t sample[2]) {
ms[LEFTCHANNEL] = ms[RIGHTCHANNEL] = (ttl>>1) & 0xffff;
}
ms[LEFTCHANNEL] = Amplify(ms[LEFTCHANNEL]);
ms[RIGHTCHANNEL] = Amplify(ms[RIGHTCHANNEL]);
if (pwm.available()) {
pwm.write((int16_t) ms[0]);
pwm.write((int16_t) ms[1]);
-6
View File
@@ -558,14 +558,8 @@ static __inline int CLZ(int x)
typedef union _U64 {
Word64 w64;
struct {
#if defined(__XTENSA__) || defined (__riscv)
unsigned int lo32;
signed int hi32;
#else
/* PowerPC = big endian */
signed int hi32;
unsigned int lo32;
#endif
} r;
} U64;
+2 -2
View File
@@ -258,10 +258,10 @@ int DecodeProgramConfigElement(ProgConfigElement *pce, BitStreamInfo *bsi)
pce->bce[i] |= GetBits(bsi, 4); /* tag select */
}
for (i = 0; i < pce->numLCE; i++)
for (i = 0; i < pce->numLCE && i < MAX_NUM_LCE; i++)
pce->lce[i] = GetBits(bsi, 4); /* tag select */
for (i = 0; i < pce->numADE; i++)
for (i = 0; i < pce->numADE && i < MAX_NUM_ADE; i++)
pce->ade[i] = GetBits(bsi, 4); /* tag select */
for (i = 0; i < pce->numCCE; i++) {
+1 -1
View File
@@ -253,7 +253,7 @@ static int DecodeOneScaleFactor(BitStreamInfo *bsi)
pi->numPulse = GetBits(bsi, 2) + 1; /* add 1 here */
pi->startSFB = GetBits(bsi, 6);
for (i = 0; i < pi->numPulse; i++) {
for (i = 0; i < pi->numPulse && i < MAX_PULSES; i++) {
pi->offset[i] = GetBits(bsi, 5);
pi->amp[i] = GetBits(bsi, 4);
}
+6 -6
View File
@@ -195,7 +195,7 @@ static void UnpackSBRGrid(BitStreamInfo *bsi, SBRHeader *sbrHdr, SBRGrid *sbrGri
absBorder = GetBits(bsi, 2) + NUM_TIME_SLOTS;
numRelBorder = GetBits(bsi, 2);
sbrGrid->numEnv = numRelBorder + 1;
for (rel = 0; rel < numRelBorder; rel++)
for (rel = 0; rel < numRelBorder && rel < 3; rel++)
relBorder[rel] = 2*GetBits(bsi, 2) + 2;
pBits = cLog2[sbrGrid->numEnv + 1];
@@ -221,13 +221,13 @@ static void UnpackSBRGrid(BitStreamInfo *bsi, SBRHeader *sbrHdr, SBRGrid *sbrGri
absBorder = GetBits(bsi, 2);
numRelBorder = GetBits(bsi, 2);
sbrGrid->numEnv = numRelBorder + 1;
for (rel = 0; rel < numRelBorder; rel++)
for (rel = 0; rel < numRelBorder && rel < 3; rel++)
relBorder[rel] = 2*GetBits(bsi, 2) + 2;
pBits = cLog2[sbrGrid->numEnv + 1];
sbrGrid->pointer = GetBits(bsi, pBits);
for (env = 0; env < sbrGrid->numEnv; env++)
for (env = 0; env < sbrGrid->numEnv && env < MAX_NUM_ENV; env++)
sbrGrid->freqRes[env] = GetBits(bsi, 1);
absBordLead = absBorder;
@@ -253,16 +253,16 @@ static void UnpackSBRGrid(BitStreamInfo *bsi, SBRHeader *sbrHdr, SBRGrid *sbrGri
sbrGrid->numEnv = numRelBorder0 + numRelBorder1 + 1;
ASSERT(sbrGrid->numEnv <= 5);
for (rel = 0; rel < numRelBorder0; rel++)
for (rel = 0; rel < numRelBorder0 && rel < 3; rel++)
relBorder0[rel] = 2*GetBits(bsi, 2) + 2;
for (rel = 0; rel < numRelBorder1; rel++)
for (rel = 0; rel < numRelBorder1 && rel < 3; rel++)
relBorder1[rel] = 2*GetBits(bsi, 2) + 2;
pBits = cLog2[numRelBorder0 + numRelBorder1 + 2];
sbrGrid->pointer = GetBits(bsi, pBits);
for (env = 0; env < sbrGrid->numEnv; env++)
for (env = 0; env < sbrGrid->numEnv && env < MAX_NUM_ENV; env++)
sbrGrid->freqRes[env] = GetBits(bsi, 1);
numRelLead = numRelBorder0;
+1 -1
View File
@@ -801,7 +801,7 @@ REQUIRED IN ALL CASES
Notice to be included in header file of all Error Corrections and Shared
Modifications:
Portions Copyright 1994-2003 © RealNetworks, Inc. All rights reserved.
Portions Copyright 1994-2003 (c) RealNetworks, Inc. All rights reserved.
The contents of this file, and the files included with this file, are
subject to the current version of RealNetworks Community Source License
+12 -6
View File
@@ -363,7 +363,8 @@ static mad_fixed_t const root_table_val[7] PROGMEM = {
};
static inline mad_fixed_t root_table(int i)
{
return root_table_val[i];
volatile mad_fixed_t a = root_table_val[i];
return a;
}
/*
coefficients for aliasing reduction
@@ -381,7 +382,8 @@ static mad_fixed_t const cs_val[8] PROGMEM = {
};
static inline mad_fixed_t cs(int i)
{
return cs_val[i];
volatile mad_fixed_t a = cs_val[i];
return a;
}
static mad_fixed_t const ca_val[8] PROGMEM = {
@@ -392,7 +394,8 @@ static mad_fixed_t const ca_val[8] PROGMEM = {
};
static inline mad_fixed_t ca(int i)
{
return ca_val[i];
volatile mad_fixed_t a = ca_val[i];
return a;
}
/*
@@ -438,7 +441,8 @@ static mad_fixed_t const window_l_val[36] PROGMEM = {
};
static inline mad_fixed_t window_l(int i)
{
return window_l_val[i];
volatile mad_fixed_t a = window_l_val[i];
return *(mad_fixed_t*)&a;
}
# endif /* ASO_IMDCT */
@@ -458,7 +462,8 @@ static mad_fixed_t const window_s_val[12] PROGMEM = {
};
static inline mad_fixed_t window_s(int i)
{
return window_s_val[i];
volatile mad_fixed_t a = window_s_val[i];
return a;
}
/*
@@ -479,7 +484,8 @@ static mad_fixed_t const is_table_val[7] PROGMEM = {
};
static inline mad_fixed_t is_table(int i)
{
return is_table_val[i];
volatile mad_fixed_t a = is_table_val[i];
return a;
}
/*
+146 -58
View File
@@ -50,7 +50,9 @@
// 2101 | }
// | ^
#if !defined(ESP32)
#if defined(ESP32) && (__GNUC__ >= 8) && (__XTENSA__)
// Not defined, can't build
#else
#ifndef TSF_INCLUDE_TSF_INL
#define TSF_INCLUDE_TSF_INL
@@ -612,11 +614,11 @@ struct tsf_voice
{
int playingPreset, playingKey, playingChannel;
struct tsf_region* region;
double pitchInputTimecents, pitchOutputFactor;
double pitchRatio;
double sourceSamplePosition;
fixed32p32 sourceSamplePositionF32P32;
float noteGainDB, panFactorLeft, panFactorRight;
unsigned int playIndex, loopStart, loopEnd;
fixed32p32 sourceSamplePositionF32P32, loopStartF32P32, loopEndF32P32, loopSizeF32P32, pitchRatioF32P32;
float noteGain, panFactorLeft, panFactorRight;
int playIndex, loopStart, loopEnd;
struct tsf_voice_envelope ampenv, modenv;
struct tsf_voice_lowpass lowpass;
struct tsf_voice_lfo modlfo, viblfo;
@@ -635,6 +637,14 @@ struct tsf_channels
int channelNum, activeChannel;
};
// Math properties of tsf_timecents2Secs* (tc2s):
// # tc2s(a + b) = tc2s(a) * tc2s(b)
// # tc2s(a - b) = tc2s(a) / tc2s(b)
// # 1 / tc2s(a) = tc2s(-a)
// # The same applies to tsf_cents2Hertz and tsf_decibelsToGain
// Path properties of tsf_decibelsToGain (db2g) and tsf_gainToDecibels (g2db)
// # db2g(g2db(a)) = a
// # g2db(db2g(a)) = a
static double tsf_timecents2Secsd(double timecents) { return TSF_POW(2.0, timecents / 1200.0); }
static float tsf_timecents2Secsf(float timecents) { return TSF_POWF(2.0f, timecents / 1200.0f); }
static float tsf_cents2Hertz(float cents) { return 8.176f * TSF_POWF(2.0f, cents / 1200.0f); }
@@ -1230,6 +1240,7 @@ static void tsf_voice_end(struct tsf_voice* v, float outSampleRate)
{
// Continue playing, but stop looping.
v->loopEnd = v->loopStart;
v->loopSizeF32P32 = 0;
}
}
@@ -1242,10 +1253,9 @@ static void tsf_voice_endquick(struct tsf_voice* v, float outSampleRate)
static void tsf_voice_calcpitchratio(struct tsf_voice* v, float pitchShift, float outSampleRate)
{
double note = v->playingKey + v->region->transpose + v->region->tune / 100.0;
double adjustedPitch = v->region->pitch_keycenter + (note - v->region->pitch_keycenter) * (v->region->pitch_keytrack / 100.0);
if (pitchShift) adjustedPitch += pitchShift;
v->pitchInputTimecents = adjustedPitch * 100.0;
v->pitchOutputFactor = v->region->sample_rate / (tsf_timecents2Secsd(v->region->pitch_keycenter * 100.0) * outSampleRate);
double adjustedPitch = (note - v->region->pitch_keycenter) * v->region->pitch_keytrack;
if (pitchShift) adjustedPitch += adjustedPitch * 100.0f;
v->pitchRatio = v->region->sample_rate * tsf_timecents2Secsd(adjustedPitch) / outSampleRate;
}
short tsf_read_short_cached(tsf *f, int pos)
@@ -1313,10 +1323,10 @@ static void tsf_voice_render(tsf* f, struct tsf_voice* v, float* outputBuffer, i
else tmpInitialFilterFc = 0, tmpModLfoToFilterFc = 0, tmpModEnvToFilterFc = 0;
if (dynamicPitchRatio) pitchRatio = 0, tmpModLfoToPitch = (float)region->modLfoToPitch, tmpVibLfoToPitch = (float)region->vibLfoToPitch, tmpModEnvToPitch = (float)region->modEnvToPitch;
else pitchRatio = tsf_timecents2Secsd(v->pitchInputTimecents) * v->pitchOutputFactor, tmpModLfoToPitch = 0, tmpVibLfoToPitch = 0, tmpModEnvToPitch = 0;
else pitchRatio = v->pitchRatio, tmpModLfoToPitch = 0, tmpVibLfoToPitch = 0, tmpModEnvToPitch = 0;
if (dynamicGain) tmpModLfoToVolume = (float)region->modLfoToVolume * 0.1f;
else noteGain = tsf_decibelsToGain(v->noteGainDB), tmpModLfoToVolume = 0;
else noteGain = v->noteGain, tmpModLfoToVolume = 0;
while (numSamples)
{
@@ -1333,10 +1343,10 @@ static void tsf_voice_render(tsf* f, struct tsf_voice* v, float* outputBuffer, i
}
if (dynamicPitchRatio)
pitchRatio = tsf_timecents2Secsd(v->pitchInputTimecents + (v->modlfo.level * tmpModLfoToPitch + v->viblfo.level * tmpVibLfoToPitch + v->modenv.level * tmpModEnvToPitch)) * v->pitchOutputFactor;
pitchRatio = v->pitchRatio * tsf_timecents2Secsd(v->modlfo.level * tmpModLfoToPitch + v->viblfo.level * tmpVibLfoToPitch + v->modenv.level * tmpModEnvToPitch);
if (dynamicGain)
noteGain = tsf_decibelsToGain(v->noteGainDB + (v->modlfo.level * tmpModLfoToVolume));
noteGain = v->noteGain * tsf_decibelsToGain(v->modlfo.level * tmpModLfoToVolume);
gainMono = noteGain * v->ampenv.level;
@@ -1445,40 +1455,22 @@ static void tsf_voice_render_fast(tsf* f, struct tsf_voice* v, short* outputBuff
TSF_BOOL updateModEnv = (region->modEnvToPitch || region->modEnvToFilterFc);
TSF_BOOL updateModLFO = (v->modlfo.delta && (region->modLfoToPitch || region->modLfoToFilterFc || region->modLfoToVolume));
TSF_BOOL updateVibLFO = (v->viblfo.delta && (region->vibLfoToPitch));
TSF_BOOL isLooping = (v->loopStart < v->loopEnd);
unsigned int tmpLoopStart = v->loopStart, tmpLoopEnd = v->loopEnd;
//double tmpSampleEndDbl = (double)v->sampleEnd, tmpLoopEndDbl = (double)tmpLoopEnd + 1.0;
//double tmpSourceSamplePosition = v->sourceSamplePosition;
fixed32p32 tmpSampleEndF32P32 = ((fixed32p32)(region->end)) << 32;
fixed32p32 tmpLoopStartF32P32 = ((fixed32p32)(tmpLoopStart + 1)) << 32;
fixed32p32 tmpLoopEndF32P32 = ((fixed32p32)(tmpLoopEnd + 1)) << 32;
TSF_BOOL isLooping = (v->loopSizeF32P32 > 0);
fixed32p32 tmpSampleEndF32P32 = (fixed32p32)(region->end) << 32;
fixed32p32 tmpSourceSamplePositionF32P32 = v->sourceSamplePositionF32P32;
struct tsf_voice_lowpass tmpLowpass = v->lowpass;
TSF_BOOL dynamicLowpass = (region->modLfoToFilterFc || region->modEnvToFilterFc);
float tmpSampleRate = f->outSampleRate, tmpInitialFilterFc, tmpModLfoToFilterFc, tmpModEnvToFilterFc;
TSF_BOOL dynamicPitchRatio = (region->modLfoToPitch || region->modEnvToPitch || region->vibLfoToPitch);
//double pitchRatio;
fixed32p32 pitchRatioF32P32;
fixed32p32 pitchRatioF32P32, tmpPitchRatioF32P32;
float tmpModLfoToPitch, tmpVibLfoToPitch, tmpModEnvToPitch;
TSF_BOOL dynamicGain = (region->modLfoToVolume != 0);
float noteGain, tmpModLfoToVolume;
float noteGain, tmpNoteGain, tmpModLfoToVolume;
if (dynamicLowpass) tmpInitialFilterFc = (float)region->initialFilterFc, tmpModLfoToFilterFc = (float)region->modLfoToFilterFc, tmpModEnvToFilterFc = (float)region->modEnvToFilterFc;
else tmpInitialFilterFc = 0, tmpModLfoToFilterFc = 0, tmpModEnvToFilterFc = 0;
if (dynamicPitchRatio) tmpPitchRatioF32P32 = v->pitchRatioF32P32, pitchRatioF32P32 = 0, tmpModLfoToPitch = (float)region->modLfoToPitch, tmpVibLfoToPitch = (float)region->vibLfoToPitch, tmpModEnvToPitch = (float)region->modEnvToPitch;
else pitchRatioF32P32 = v->pitchRatioF32P32, tmpModLfoToPitch = 0, tmpVibLfoToPitch = 0, tmpModEnvToPitch = 0;
if (dynamicPitchRatio) pitchRatioF32P32 = 0, tmpModLfoToPitch = (float)region->modLfoToPitch, tmpVibLfoToPitch = (float)region->vibLfoToPitch, tmpModEnvToPitch = (float)region->modEnvToPitch;
else {
double pr = tsf_timecents2Secsd(v->pitchInputTimecents) * v->pitchOutputFactor;
fixed32p32 adj = 1LL<<32;
pr *= adj;
pitchRatioF32P32 = (int64_t)pr, tmpModLfoToPitch = 0, tmpVibLfoToPitch = 0, tmpModEnvToPitch = 0;
}
if (dynamicGain) noteGain = 0, tmpModLfoToVolume = (float)region->modLfoToVolume * 0.1f;
else noteGain = tsf_decibelsToGain(v->noteGainDB), tmpModLfoToVolume = 0;
if (dynamicGain) noteGain = 0, tmpNoteGain = v->noteGain, tmpModLfoToVolume = (float)region->modLfoToVolume * 0.1f;
else noteGain = v->noteGain, tmpModLfoToVolume = 0;
while (numSamples)
{
@@ -1486,19 +1478,17 @@ static void tsf_voice_render_fast(tsf* f, struct tsf_voice* v, short* outputBuff
int blockSamples = (numSamples > TSF_RENDER_EFFECTSAMPLEBLOCK ? TSF_RENDER_EFFECTSAMPLEBLOCK : numSamples);
numSamples -= blockSamples;
if (dynamicLowpass)
{
float fres = tmpInitialFilterFc + v->modlfo.level * tmpModLfoToFilterFc + v->modenv.level * tmpModEnvToFilterFc;
tmpLowpass.active = (fres <= 13500.0f);
if (tmpLowpass.active) tsf_voice_lowpass_setup(&tmpLowpass, tsf_cents2Hertz(fres) / tmpSampleRate);
if (dynamicPitchRatio) {
if (v->modlfo.level || v->viblfo.level || v->modenv.level)
pitchRatioF32P32 = tmpPitchRatioF32P32 * tsf_timecents2Secsd(v->modlfo.level * tmpModLfoToPitch + v->viblfo.level * tmpVibLfoToPitch + v->modenv.level * tmpModEnvToPitch);
else
pitchRatioF32P32 = tmpPitchRatioF32P32; // If all levels are 0, just bypass conversion function
}
if (dynamicPitchRatio) {
pitchRatioF32P32 = tsf_timecents2Secsd(v->pitchInputTimecents + (v->modlfo.level * tmpModLfoToPitch + v->viblfo.level * tmpVibLfoToPitch + v->modenv.level * tmpModEnvToPitch)) * v->pitchOutputFactor * (1LL<<32);
}
if (dynamicGain)
noteGain = tsf_decibelsToGain(v->noteGainDB + (v->modlfo.level * tmpModLfoToVolume));
if (dynamicGain) {
if (v->modlfo.level) noteGain = tmpNoteGain * tsf_decibelsToGain(v->modlfo.level * tmpModLfoToVolume);
else noteGain = tmpNoteGain; // If level is 0, just bypass conversion function
}
gainMono = noteGain * v->ampenv.level;
short gainMonoFP = gainMono * 32767;
@@ -1511,7 +1501,7 @@ static void tsf_voice_render_fast(tsf* f, struct tsf_voice* v, short* outputBuff
if (updateModLFO) tsf_voice_lfo_process(&v->modlfo, blockSamples);
if (updateVibLFO) tsf_voice_lfo_process(&v->viblfo, blockSamples);
while (blockSamples-- && tmpSourceSamplePositionF32P32 < tmpSampleEndF32P32)
while (blockSamples--)
{
unsigned int pos = (unsigned int)(tmpSourceSamplePositionF32P32>>32);
if (pos == 0xffffffff) pos = 0;
@@ -1523,8 +1513,14 @@ static void tsf_voice_render_fast(tsf* f, struct tsf_voice* v, short* outputBuff
// Next sample.
tmpSourceSamplePositionF32P32 += pitchRatioF32P32;
if (tmpSourceSamplePositionF32P32 >= tmpLoopEndF32P32 && isLooping)
tmpSourceSamplePositionF32P32 -= (tmpLoopEndF32P32 - tmpLoopStartF32P32 + (1LL<<32));
if (tmpSourceSamplePositionF32P32 >= v->loopEndF32P32)
{
if (isLooping)
tmpSourceSamplePositionF32P32 -= v->loopSizeF32P32;
else if (tmpSourceSamplePositionF32P32 >= tmpSampleEndF32P32)
break;
}
}
if (tmpSourceSamplePositionF32P32 >= tmpSampleEndF32P32 || v->ampenv.segment == TSF_SEGMENT_DONE)
@@ -1535,7 +1531,6 @@ static void tsf_voice_render_fast(tsf* f, struct tsf_voice* v, short* outputBuff
}
v->sourceSamplePositionF32P32 = tmpSourceSamplePositionF32P32;
if (tmpLowpass.active || dynamicLowpass) v->lowpass = tmpLowpass;
}
@@ -1730,13 +1725,14 @@ TSFDEF void tsf_note_on(tsf* f, int preset_index, int key, float vel)
}
voice = &f->voices[f->voiceNum - 4];
voice[1].playingPreset = voice[2].playingPreset = voice[3].playingPreset = -1;
voice[1].playIndex = voice[2].playIndex = voice[3].playIndex = -1;
}
voice->region = region;
voice->playingPreset = preset_index;
voice->playingKey = key;
voice->playIndex = voicePlayIndex;
voice->noteGainDB = f->globalGainDB - region->attenuation - tsf_gainToDecibels(1.0f / vel);
voice->noteGain = vel * tsf_decibelsToGain(f->globalGainDB - region->attenuation);
if (f->channels)
{
@@ -1752,7 +1748,6 @@ TSFDEF void tsf_note_on(tsf* f, int preset_index, int key, float vel)
// Offset/end.
voice->sourceSamplePosition = region->offset;
voice->sourceSamplePositionF32P32 = ((int64_t)region->offset)<< 32;
// Loop.
doLoop = (region->loop_mode != TSF_LOOPMODE_NONE && region->loop_start < region->loop_end);
@@ -1777,6 +1772,86 @@ TSFDEF void tsf_note_on(tsf* f, int preset_index, int key, float vel)
}
}
/**
* Returns the generated playing index that can be used to identify all voices related to this note.
*/
TSFDEF int tsf_note_on_fast(tsf* f, int preset_index, int key, float vel)
{
if (preset_index < 0 || preset_index >= f->presetNum) return -1;
if (!vel) { tsf_note_off(f, preset_index, key); return -1; }
if (f->presets[preset_index].regions == NULL) tsf_load_preset(f, f->hydra, preset_index);
short midiVelocity = (short)(vel * 127);
struct tsf_region *region, *regionEnd;
// Play all matching regions.
int voicePlayIndex = f->voicePlayIndex++;
for (region = f->presets[preset_index].regions, regionEnd = region + f->presets[preset_index].regionNum; region != regionEnd; region++)
{
if (key < region->lokey || key > region->hikey || midiVelocity < region->lovel || midiVelocity > region->hivel) continue;
struct tsf_voice *voice, *v, *vEnd; TSF_BOOL doLoop;
voice = TSF_NULL, v = f->voices, vEnd = v + f->voiceNum;
if (region->group)
{
for (; v != vEnd; v++)
if (v->playingPreset == preset_index && v->region->group == region->group) tsf_voice_endquick(v, f->outSampleRate);
else if (v->playingPreset == -1 && !voice) voice = v;
}
else for (; v != vEnd; v++) if (v->playingPreset == -1) { voice = v; break; }
if (!voice)
{
f->voiceNum += 4;
struct tsf_voice *saveVoice = f->voices;
f->voices = (struct tsf_voice*)TSF_REALLOC(f->voices, f->voiceNum * sizeof(struct tsf_voice));
if (!f->voices) {
f->voices = saveVoice;
printf("OOM, no room for new voice. Ignoring note_on\n");
return -1;
}
voice = &f->voices[f->voiceNum - 4];
voice[1].playingPreset = voice[2].playingPreset = voice[3].playingPreset = -1;
voice[1].playIndex = voice[2].playIndex = voice[3].playIndex = -1;
}
voice->region = region;
voice->playingPreset = preset_index;
voice->playingKey = key;
voice->playIndex = voicePlayIndex;
voice->noteGain = vel * tsf_decibelsToGain(f->globalGainDB - region->attenuation);
if (f->channels)
{
f->channels->setupVoice(f, voice);
}
else
{
tsf_voice_calcpitchratio(voice, 0, f->outSampleRate);
voice->pitchRatioF32P32 = voice->pitchRatio * (1LL << 32);
}
// Offset/end.
voice->sourceSamplePositionF32P32 = ((int64_t)region->offset)<< 32;
// Loop.
doLoop = (region->loop_mode != TSF_LOOPMODE_NONE && region->loop_start < region->loop_end);
voice->loopStartF32P32 = doLoop ? ((int64_t)region->loop_start + 1) << 32 : 0;
voice->loopEndF32P32 = doLoop ? ((int64_t)region->loop_end + 1) << 32 : 0;
voice->loopSizeF32P32 = doLoop ? voice->loopEndF32P32 - voice->loopStartF32P32 + (1LL << 32) : 0;
// Setup envelopes.
tsf_voice_envelope_setup(&voice->ampenv, &region->ampenv, key, midiVelocity, TSF_TRUE, f->outSampleRate);
tsf_voice_envelope_setup(&voice->modenv, &region->modenv, key, midiVelocity, TSF_FALSE, f->outSampleRate);
// Setup LFO filters.
tsf_voice_lfo_setup(&voice->modlfo, region->delayModLFO, region->freqModLFO, f->outSampleRate);
tsf_voice_lfo_setup(&voice->viblfo, region->delayVibLFO, region->freqVibLFO, f->outSampleRate);
}
return voicePlayIndex;
}
TSFDEF int tsf_bank_note_on(tsf* f, int bank, int preset_number, int key, float vel)
{
int preset_index = tsf_get_presetindex(f, bank, preset_number);
@@ -1805,6 +1880,19 @@ TSFDEF void tsf_note_off(tsf* f, int preset_index, int key)
}
}
/**
* Stops all voices with the given playing index. If no key provided or if -1, fallbacks to tsf_note_off
*/
TSFDEF void tsf_note_off_fast(tsf* f, int preset_index, int key, int playIndex = -1)
{
if (playIndex < 0)
tsf_note_off(f, preset_index, key);
else
for (struct tsf_voice *v = f->voices, *vEnd = v + f->voiceNum; v != vEnd; v++)
if (v->playIndex == playIndex && v->playingPreset == preset_index && v->playingKey == key && v->ampenv.segment < TSF_SEGMENT_RELEASE)
tsf_voice_end(v, f->outSampleRate);
}
TSFDEF int tsf_bank_note_off(tsf* f, int bank, int preset_number, int key)
{
int preset_index = tsf_get_presetindex(f, bank, preset_number);
@@ -1884,7 +1972,7 @@ static void tsf_channel_setup_voice(tsf* f, struct tsf_voice* v)
struct tsf_channel* c = &f->channels->channels[f->channels->activeChannel];
float newpan = v->region->pan + c->panOffset;
v->playingChannel = f->channels->activeChannel;
v->noteGainDB += c->gainDB;
v->noteGain *= tsf_decibelsToGain(c->gainDB);
tsf_voice_calcpitchratio(v, (c->pitchWheel == 8192 ? c->tuning : ((c->pitchWheel / 16383.0f * c->pitchRange * 2.0f) - c->pitchRange + c->tuning)), f->outSampleRate);
if (newpan <= -0.5f) { v->panFactorLeft = 1.0f; v->panFactorRight = 0.0f; }
else if (newpan >= 0.5f) { v->panFactorLeft = 0.0f; v->panFactorRight = 1.0f; }
@@ -1994,7 +2082,7 @@ TSFDEF void tsf_channel_set_volume(tsf* f, int channel, float volume)
if (gainDBChange == 0) return;
for (v = f->voices, vEnd = v + f->voiceNum; v != vEnd; v++)
if (v->playingChannel == channel && v->playingPreset != -1)
v->noteGainDB += gainDBChange;
v->noteGain *= tsf_decibelsToGain(gainDBChange);
c->gainDB = gainDB;
}
+15
View File
@@ -0,0 +1,15 @@
Publishing New Releases
=======================
First, update the version number throughout the repo and push the change:
./tools/makever.py --version X.Y.Z
git commit -a -m "Update version"
git push
Then tag it
git tag X.Y.Z
git push origin X.Y.Z
Then on the GH web interface make a new release from that tag.
+41
View File
@@ -0,0 +1,41 @@
#!/usr/bin/env python3
import sys
import struct
import subprocess
import re
import os
import os.path
import argparse
import time
import shutil
import json
def main():
parser = argparse.ArgumentParser(description='Version updater')
parser.add_argument('-v', '--version', action='store', required=True, help='Version in X.Y.Z form')
args = parser.parse_args()
major, minor, sub = args.version.split(".")
# Silly way to check for integer x.y.z
major = int(major)
minor = int(minor)
sub = int(sub)
# library.properties
with open("library.properties", "r") as fin:
with open("library.properties.new", "w") as fout:
for l in fin:
if l.startswith("version="):
l = "version=" + str(args.version) + "\n"
fout.write(l);
shutil.move("library.properties.new", "library.properties")
# package.json
with open("library.json", "r") as fin:
library = json.load(fin);
library["version"] = str(args.version);
with open("library.json.new", "w") as fout:
json.dump(library, fout, indent = 4);
shutil.move("library.json.new", "library.json")
main()