mirror of
https://github.com/cnlohr/lolra.git
synced 2026-06-17 00:09:31 +00:00
Fixup audio - now working with javascript.
This commit is contained in:
@@ -65,7 +65,7 @@ SOFTWARE.
|
|||||||
|
|
||||||
#define SH1107_128x128
|
#define SH1107_128x128
|
||||||
#define SSD1306_REMAP_I2C
|
#define SSD1306_REMAP_I2C
|
||||||
#define PWM_OUTPUT
|
//#define PWM_OUTPUT
|
||||||
#define ENABLE_OLED
|
#define ENABLE_OLED
|
||||||
#define PROFILING_PIN PA0
|
#define PROFILING_PIN PA0
|
||||||
|
|
||||||
@@ -391,7 +391,7 @@ void DMA1_Channel1_IRQHandler( void )
|
|||||||
intensity = 1;
|
intensity = 1;
|
||||||
intensity = (intensity + s/intensity)/2;
|
intensity = (intensity + s/intensity)/2;
|
||||||
intensity = (intensity + s/intensity)/2;
|
intensity = (intensity + s/intensity)/2;
|
||||||
intensity_average = intensity_average - (intensity_average>>10) + (intensity);
|
intensity_average = intensity_average - (intensity_average>>12) + (intensity>>2);
|
||||||
|
|
||||||
|
|
||||||
#ifdef PWM_OUTPUT
|
#ifdef PWM_OUTPUT
|
||||||
@@ -649,7 +649,7 @@ int HandleHidUserGetReportSetup( struct _USBState * ctx, tusb_control_request_t
|
|||||||
|
|
||||||
((uint32_t*)scratchpad)[0] = (intensity_average<<8) | samps_to_send;
|
((uint32_t*)scratchpad)[0] = (intensity_average<<8) | samps_to_send;
|
||||||
((uint32_t*)scratchpad)[1] = (g_lastper<<16) | g_lastlen;
|
((uint32_t*)scratchpad)[1] = (g_lastper<<16) | g_lastlen;
|
||||||
((uint32_t*)scratchpad)[2] = 0; //Reserved.
|
((uint32_t*)scratchpad)[2] = (0<<16) | (((g_pwm_period+1)*g_goertzel_buffer)); //LSW = 144MHz / X
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
for( i = 3; i < samps_to_send + 3; i++ )
|
for( i = 3; i < samps_to_send + 3; i++ )
|
||||||
|
|||||||
@@ -227,7 +227,10 @@ function onLoad()
|
|||||||
<TD VALIGN=TOP>
|
<TD VALIGN=TOP>
|
||||||
|
|
||||||
Live Control:
|
Live Control:
|
||||||
<TABLE><TR><TD><INPUT TYPE=SUBMIT onClick="reqConnect()" VALUE="Open Device" ID=connectButton><INPUT TYPE=SUBMIT onClick="toggleAudio()" VALUE="Play Audio" ID="ToggleAudio"></TD><TD><DIV ID=STATUS></DIV></TD></TR></TABLE>
|
<TABLE><TR>
|
||||||
|
<TD><INPUT TYPE=SUBMIT onClick="reqConnect()" VALUE="Open Device" ID=connectButton><INPUT TYPE=SUBMIT onClick="toggleAudio()" VALUE="Play Audio" ID="ToggleAudioButton"></TD>
|
||||||
|
<TD><DIV ID=STATUS></DIV></TD>
|
||||||
|
</TR></TABLE>
|
||||||
<DIV ID="StatusPerf"></DIV>
|
<DIV ID="StatusPerf"></DIV>
|
||||||
</TD>
|
</TD>
|
||||||
|
|
||||||
|
|||||||
+82
-22
@@ -58,38 +58,83 @@ async function closeDeviceTool()
|
|||||||
setStatusError( "Disconnected" );
|
setStatusError( "Disconnected" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var playingAudioProcessor = null;
|
||||||
|
var audioContext = null;
|
||||||
|
|
||||||
async function toggleAudio()
|
async function toggleAudio()
|
||||||
|
{
|
||||||
|
if( playingAudioProcessor == null )
|
||||||
{
|
{
|
||||||
var bypass = '\
|
var bypass = '\
|
||||||
class PlayingAudioProcessor extends AudioWorkletProcessor {\
|
class PlayingAudioProcessor extends AudioWorkletProcessor {\
|
||||||
static get parameterDescriptors() {\
|
static get parameterDescriptors() {\
|
||||||
return [\
|
return [\
|
||||||
{\
|
{ name: "gain", defaultValue: 0, },\
|
||||||
name: "ingestRate",\
|
{ name: "lastIntensity", defaultValue: 1, },\
|
||||||
defaultValue: 25000,\
|
{ name: "sampleAdvance", defaultValue: 0.5, },\
|
||||||
minValue: 1,\
|
|
||||||
maxValue: 1000000\
|
|
||||||
},\
|
|
||||||
]\
|
]\
|
||||||
};\
|
};\
|
||||||
constructor() {\
|
constructor() {\
|
||||||
super();\
|
super();\
|
||||||
|
this.rbuffer = new Uint32Array(8192); \
|
||||||
|
this.rbufferhead = 0|0; \
|
||||||
|
this.rbuffertail = 0|0; \
|
||||||
|
this.sampleplace = 0.0; \
|
||||||
|
this.lastIntensity = 0.0; \
|
||||||
|
this.totalsampcount = 0|0; \
|
||||||
this.port.onmessage = (e) => { \
|
this.port.onmessage = (e) => { \
|
||||||
console.log(e.data);\
|
var mulcoeff = 500.0 / Math.fround( this.lastIntensity ); \
|
||||||
this.ingestData = e.data;\
|
for( var i = 0|0; i < e.data.length|0; i++ ) \
|
||||||
|
{ \
|
||||||
|
let n = (this.rbufferhead + (1|0))%(8192|0); \
|
||||||
|
if( n == this.rbuffertail ) \
|
||||||
|
{ \
|
||||||
|
this.rbuffertail = (this.rbuffertail + (1|0))%(8192|0); \
|
||||||
|
console.log( `Overflow` ); \
|
||||||
|
} \
|
||||||
|
let vv = e.data[i] | 0; \
|
||||||
|
let vi = vv >> 16; \
|
||||||
|
let vq = vv & 0xffff; \
|
||||||
|
if( vi >= 32768 ) vi = vi-65535; \
|
||||||
|
if( vq >= 32768 ) vq = vq-65535; \
|
||||||
|
let power = Math.sqrt( vi * vi + vq * vq ) * mulcoeff; \
|
||||||
|
this.rbuffer[this.rbufferhead] = power; \
|
||||||
|
this.rbufferhead = n; \
|
||||||
|
} \
|
||||||
}; \
|
}; \
|
||||||
this.ingestData = new ArrayBuffer(0);\
|
|
||||||
}\
|
}\
|
||||||
\
|
\
|
||||||
process(inputs, outputs, parameters) {\
|
process(inputs, outputs, parameters) {\
|
||||||
/*console.log( parameters.ingestRate[0] );*/ \
|
/*console.log( parameters.gain[0] );*/ \
|
||||||
/*console.log( this.ingestData );*/ \
|
/*console.log( this.ingestData );*/ \
|
||||||
|
this.lastIntensity = parameters.lastIntensity[0]; \
|
||||||
let len = outputs[0][0].length; \
|
let len = outputs[0][0].length; \
|
||||||
|
const sa = Math.fround( parameters.sampleAdvance[0] ); /*float*/ \
|
||||||
|
var s = Math.fround( this.sampleplace ); /*float*/ \
|
||||||
|
var tail = this.rbuffertail | 0; /* int*/ \
|
||||||
|
var tailnext = this.rbuffertail | 0; /* int*/ \
|
||||||
|
if( tail == this.rbufferhead ) { console.log( "Underflow " ); return true; }\
|
||||||
|
var tsamp = Math.fround( this.rbuffer[tail] ); \
|
||||||
|
var nsamp = Math.fround( this.rbuffer[tailnext] ); \
|
||||||
|
this.totalsampcount += len|0; \
|
||||||
for (let b = 0|0; b < len|0; b++) { \
|
for (let b = 0|0; b < len|0; b++) { \
|
||||||
outputs[0][0][b] = Math.random()*0.01; \
|
s += sa; \
|
||||||
|
var excess = Math.floor( s ) | 0; \
|
||||||
|
if( excess > 0 ) \
|
||||||
|
{ \
|
||||||
|
s -= excess; \
|
||||||
|
tail = ( tail + (excess|0) ) % (8192|0); \
|
||||||
|
tailnext = ( tail + (1|0) ) % (8192|0); \
|
||||||
|
if( tail == this.rbufferhead ) { console.log( "Underflow" ); break; } \
|
||||||
|
tsamp = Math.fround( this.rbuffer[tail] ); \
|
||||||
|
nsamp = Math.fround( this.rbuffer[tailnext] ); \
|
||||||
} \
|
} \
|
||||||
|
var valv = tsamp * (1.0-s) + nsamp * s; \
|
||||||
|
outputs[0][0][b] = 0.1*valv*parameters.gain[0]; \
|
||||||
|
} \
|
||||||
|
/*console.log( tail + " " + this.rbuffertail + " " + tsamp + " " + nsamp );*/ \
|
||||||
|
this.rbuffertail = tail; \
|
||||||
|
this.sampleplace = s; \
|
||||||
return true; \
|
return true; \
|
||||||
} \
|
} \
|
||||||
} \
|
} \
|
||||||
@@ -112,21 +157,26 @@ registerProcessor("playing-audio-processor", PlayingAudioProcessor);';
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var audioContext = new AudioContext();
|
audioContext = new AudioContext();
|
||||||
|
|
||||||
await audioContext.audioWorklet.addModule(dataURI);
|
await audioContext.audioWorklet.addModule(dataURI);
|
||||||
|
|
||||||
PlayingAudioProcessor = new AudioWorkletNode(
|
playingAudioProcessor = new AudioWorkletNode(
|
||||||
audioContext,
|
audioContext,
|
||||||
"playing-audio-processor"
|
"playing-audio-processor"
|
||||||
);
|
);
|
||||||
PlayingAudioProcessor.connect(audioContext.destination);
|
playingAudioProcessor.connect(audioContext.destination);
|
||||||
audioContext.resume();
|
audioContext.resume();
|
||||||
|
|
||||||
let testParam = PlayingAudioProcessor.parameters.get("ingestRate");
|
let gainParam = playingAudioProcessor.parameters.get( "gain" );
|
||||||
testParam.setValueAtTime(1000, audioContext.currentTime);
|
gainParam.setValueAtTime( 0, audioContext.currentTime );
|
||||||
|
}
|
||||||
|
|
||||||
PlayingAudioProcessor.port.postMessage( new ArrayBuffer(0) );
|
let gainParam = playingAudioProcessor.parameters.get("gain");
|
||||||
|
var newVal = 1 - gainParam.value;
|
||||||
|
|
||||||
|
gainParam.setValueAtTime( newVal, audioContext.currentTime);
|
||||||
|
document.getElementById( "ToggleAudioButton" ).value = ( newVal > 0.5 ) ? "Stop Audio" : "Start Audio";
|
||||||
}
|
}
|
||||||
|
|
||||||
function onLoadWebHidControl()
|
function onLoadWebHidControl()
|
||||||
@@ -220,9 +270,6 @@ function updateWebHidDeviceWithParameters( paramlist )
|
|||||||
arraySend[i*4+10] = (vv>>24)&0xff;
|
arraySend[i*4+10] = (vv>>24)&0xff;
|
||||||
}
|
}
|
||||||
arraySend[3] = paramlist.length | 0;
|
arraySend[3] = paramlist.length | 0;
|
||||||
|
|
||||||
console.log( arraySend );
|
|
||||||
|
|
||||||
sendReport = dev.sendFeatureReport( 0xAC, arraySend ).catch( sendLoopError );
|
sendReport = dev.sendFeatureReport( 0xAC, arraySend ).catch( sendLoopError );
|
||||||
if( !sendReport ) sendLoopError( "error creating sendFeatureReport" );
|
if( !sendReport ) sendLoopError( "error creating sendFeatureReport" );
|
||||||
}
|
}
|
||||||
@@ -287,6 +334,7 @@ async function sendLoop()
|
|||||||
ctx.fillStyle = `rgb( 255 255 255 )`;
|
ctx.fillStyle = `rgb( 255 255 255 )`;
|
||||||
|
|
||||||
let mulcoeff = 10000.0 / lastIntensity;
|
let mulcoeff = 10000.0 / lastIntensity;
|
||||||
|
|
||||||
var lot = 1.2;
|
var lot = 1.2;
|
||||||
var x = 253+IQHistoryLen;
|
var x = 253+IQHistoryLen;
|
||||||
for( var i = (IQHistoryHead+1) & (IQHistoryLen-1); i != IQHistoryHead|0; i = (i + 1) & (IQHistoryLen-1) )
|
for( var i = (IQHistoryHead+1) & (IQHistoryLen-1); i != IQHistoryHead|0; i = (i + 1) & (IQHistoryLen-1) )
|
||||||
@@ -335,7 +383,7 @@ async function sendLoop()
|
|||||||
let numq = data[0] & 0xff;
|
let numq = data[0] & 0xff;
|
||||||
let time_total = data[1]>>16;
|
let time_total = data[1]>>16;
|
||||||
let time_used = data[1]&0xffff;
|
let time_used = data[1]&0xffff;
|
||||||
//console.log( data );
|
let sample_divisor = data[2]&0xffff;
|
||||||
for( var i = 0|0; i < numq; i++ )
|
for( var i = 0|0; i < numq; i++ )
|
||||||
{
|
{
|
||||||
IQHistoryArray[IQHistoryHead++] = data[i+3];
|
IQHistoryArray[IQHistoryHead++] = data[i+3];
|
||||||
@@ -345,6 +393,18 @@ async function sendLoop()
|
|||||||
lastNumQ = numq;
|
lastNumQ = numq;
|
||||||
lastTotalTime = time_total;
|
lastTotalTime = time_total;
|
||||||
lastTimeUsed = time_used;
|
lastTimeUsed = time_used;
|
||||||
|
|
||||||
|
if( audioContext != null && playingAudioProcessor != null )
|
||||||
|
{
|
||||||
|
let lastIntensityParam = playingAudioProcessor.parameters.get("lastIntensity");
|
||||||
|
lastIntensityParam.setValueAtTime( lastIntensity, audioContext.currentTime);
|
||||||
|
|
||||||
|
let sampleAdvance = (144000000.0/sample_divisor) / audioContext.sampleRate;
|
||||||
|
let sampleAdvanceParam = playingAudioProcessor.parameters.get("sampleAdvance");
|
||||||
|
sampleAdvanceParam.setValueAtTime( sampleAdvance, audioContext.currentTime);
|
||||||
|
|
||||||
|
playingAudioProcessor.port.postMessage( new Uint32Array( data.buffer.slice( 3*4, numq*4 + 3*4 ) ) );
|
||||||
|
}
|
||||||
goodCount++;
|
goodCount++;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
Reference in New Issue
Block a user