Browse Source

Add simple AudioAmplifier

dds
PaulStoffregen 6 years ago
parent
commit
bebf5dce98
4 changed files with 85 additions and 5 deletions
  1. +38
    -0
      gui/index.html
  2. +1
    -0
      keywords.txt
  3. +26
    -1
      mixer.cpp
  4. +20
    -4
      mixer.h

+ 38
- 0
gui/index.html View File

{"type":"AudioOutputTDM","data":{"defaults":{"name":{"value":"new"}},"shortName":"tdm","inputs":16,"outputs":0,"category":"output-function","color":"#E6E0F8","icon":"arrow-in.png"}}, {"type":"AudioOutputTDM","data":{"defaults":{"name":{"value":"new"}},"shortName":"tdm","inputs":16,"outputs":0,"category":"output-function","color":"#E6E0F8","icon":"arrow-in.png"}},
{"type":"AudioOutputADAT","data":{"defaults":{"name":{"value":"new"}},"shortName":"adat","inputs":8,"outputs":0,"category":"output-function","color":"#E6E0F8","icon":"arrow-in.png"}}, {"type":"AudioOutputADAT","data":{"defaults":{"name":{"value":"new"}},"shortName":"adat","inputs":8,"outputs":0,"category":"output-function","color":"#E6E0F8","icon":"arrow-in.png"}},
{"type":"AudioOutputUSB","data":{"defaults":{"name":{"value":"new"}},"shortName":"usb","inputs":2,"outputs":0,"category":"output-function","color":"#E6E0F8","icon":"arrow-in.png"}}, {"type":"AudioOutputUSB","data":{"defaults":{"name":{"value":"new"}},"shortName":"usb","inputs":2,"outputs":0,"category":"output-function","color":"#E6E0F8","icon":"arrow-in.png"}},
{"type":"AudioAmplifier","data":{"defaults":{"name":{"value":"new"}},"shortName":"amp","inputs":1,"outputs":1,"category":"mixer-function","color":"#E6E0F8","icon":"arrow-in.png"}},
{"type":"AudioMixer4","data":{"defaults":{"name":{"value":"new"}},"shortName":"mixer","inputs":4,"outputs":1,"category":"mixer-function","color":"#E6E0F8","icon":"arrow-in.png"}}, {"type":"AudioMixer4","data":{"defaults":{"name":{"value":"new"}},"shortName":"mixer","inputs":4,"outputs":1,"category":"mixer-function","color":"#E6E0F8","icon":"arrow-in.png"}},
{"type":"AudioPlayMemory","data":{"defaults":{"name":{"value":"new"}},"shortName":"playMem","inputs":0,"outputs":1,"category":"play-function","color":"#E6E0F8","icon":"arrow-in.png"}}, {"type":"AudioPlayMemory","data":{"defaults":{"name":{"value":"new"}},"shortName":"playMem","inputs":0,"outputs":1,"category":"play-function","color":"#E6E0F8","icon":"arrow-in.png"}},
{"type":"AudioPlaySdWav","data":{"defaults":{"name":{"value":"new"}},"shortName":"playSdWav","inputs":0,"outputs":2,"category":"play-function","color":"#E6E0F8","icon":"arrow-in.png"}}, {"type":"AudioPlaySdWav","data":{"defaults":{"name":{"value":"new"}},"shortName":"playSdWav","inputs":0,"outputs":2,"category":"play-function","color":"#E6E0F8","icon":"arrow-in.png"}},
</div> </div>
</script> </script>


<script type="text/x-red" data-help-name="AudioAmplifier">
<h3>Summary</h3>
<div class=tooltipinfo>
<p>Amplify or attenuate a signal, or switch it on/off.
</p>
</div>
<h3>Audio Connections</h3>
<table class=doc align=center cellpadding=3>
<tr class=top><th>Port</th><th>Purpose</th></tr>
<tr class=odd><td align=center>In 0</td><td>Input signal</td></tr>
<tr class=odd><td align=center>Out 0</td><td>Amplified/Attn. Output</td></tr>
</table>
<h3>Functions</h3>
<p class=func><span class=keyword>gain</span>(level);</p>
<p class=desc>Adjust the amplification or attenuation.
"level" may be any floating point number from 0 to 32767.0.
1.0 passes the signal through directly. Level of 0 shuts the channel
off completely. Between 0 to 1.0 attenuates the signal, and above
1.0 amplifies it. Negative numbers may also be used, to invert the
signal.
</p>
<!--<h3>Examples</h3>
<p class=exam>File &gt; Examples &gt; Audio &gt; SamplePlayer
</p>-->
<h3>Notes</h3>
<p>Gain of 0 and 1.0 are handled efficiently as special cases. Zero
discards data without processing. 1.0 passes data directly, with
minimal overhead</p>
<p>Signal clipping can occur when any channel has gain greater than 1.0</p>
</script>
<script type="text/x-red" data-template-name="AudioAmplifier">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>

<script type="text/x-red" data-help-name="AudioMixer4"> <script type="text/x-red" data-help-name="AudioMixer4">
<h3>Summary</h3> <h3>Summary</h3>
<div class=tooltipinfo> <div class=tooltipinfo>

+ 1
- 0
keywords.txt View File

AudioInputAnalog KEYWORD2 AudioInputAnalog KEYWORD2
AudioInputAnalogStereo KEYWORD2 AudioInputAnalogStereo KEYWORD2
AudioMixer4 KEYWORD2 AudioMixer4 KEYWORD2
AudioAmplifier KEYWORD2
AudioOutputAnalog KEYWORD2 AudioOutputAnalog KEYWORD2
AudioOutputAnalogStereo KEYWORD2 AudioOutputAnalogStereo KEYWORD2
AudioPlayMemory KEYWORD2 AudioPlayMemory KEYWORD2

+ 26
- 1
mixer.cpp View File

} }
} }



void AudioAmplifier::update(void)
{
audio_block_t *block;
int32_t mult = multiplier;

if (mult == 0) {
// zero gain, discard any input and transmit nothing
block = receiveReadOnly(0);
if (block) release(block);
} else if (mult == MULTI_UNITYGAIN) {
// unity gain, pass input to output without any change
block = receiveReadOnly(0);
if (block) {
transmit(block);
release(block);
}
} else {
// apply gain to signal
block = receiveWritable(0);
if (block) {
applyGain(block->data, mult);
transmit(block);
release(block);
}
}
}

+ 20
- 4
mixer.h View File

{ {
#if defined(KINETISK) #if defined(KINETISK)
public: public:
AudioMixer4(void) : AudioStream(4, inputQueueArray) {
AudioMixer4(void) : AudioStream(4, inputQueueArray) {
for (int i=0; i<4; i++) multiplier[i] = 65536; for (int i=0; i<4; i++) multiplier[i] = 65536;
} }
virtual void update(void);
virtual void update(void);
void gain(unsigned int channel, float gain) { void gain(unsigned int channel, float gain) {
if (channel >= 4) return; if (channel >= 4) return;
if (gain > 32767.0f) gain = 32767.0f; if (gain > 32767.0f) gain = 32767.0f;


#elif defined(KINETISL) #elif defined(KINETISL)
public: public:
AudioMixer4(void) : AudioStream(4, inputQueueArray) {
AudioMixer4(void) : AudioStream(4, inputQueueArray) {
for (int i=0; i<4; i++) multiplier[i] = 256; for (int i=0; i<4; i++) multiplier[i] = 256;
} }
virtual void update(void);
virtual void update(void);
void gain(unsigned int channel, float gain) { void gain(unsigned int channel, float gain) {
if (channel >= 4) return; if (channel >= 4) return;
if (gain > 127.0f) gain = 127.0f; if (gain > 127.0f) gain = 127.0f;
#endif #endif
}; };


class AudioAmplifier : public AudioStream
{
public:
AudioAmplifier(void) : AudioStream(1, inputQueueArray), multiplier(65536) {
}
virtual void update(void);
void gain(float n) {
if (n > 32767.0f) n = 32767.0f;
else if (n < -32767.0f) n = -32767.0f;
multiplier = n * 65536.0f;
}
private:
int32_t multiplier;
audio_block_t *inputQueueArray[1];
};

#endif #endif

Loading…
Cancel
Save