Working on menus
This commit is contained in:
@ -32,6 +32,7 @@ export class CsoundEngine {
|
||||
private scopeNode: AnalyserNode | null = null;
|
||||
private audioNode: AudioNode | null = null;
|
||||
private audioContext: AudioContext | null = null;
|
||||
private gainNode: GainNode | null = null;
|
||||
private useCsound7: boolean;
|
||||
|
||||
constructor(options: CsoundEngineOptions = {}) {
|
||||
@ -139,6 +140,7 @@ export class CsoundEngine {
|
||||
this.csound.on('onAudioNodeCreated', (node: AudioNode) => {
|
||||
this.audioNode = node;
|
||||
this.audioContext = node.context as AudioContext;
|
||||
this.setupGainNode();
|
||||
this.log('Audio node created and captured');
|
||||
});
|
||||
|
||||
@ -510,6 +512,26 @@ export class CsoundEngine {
|
||||
return this.audioContext;
|
||||
}
|
||||
|
||||
private setupGainNode(): void {
|
||||
if (!this.audioNode || !this.audioContext || this.gainNode) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
this.gainNode = this.audioContext.createGain();
|
||||
this.gainNode.gain.value = 1.0;
|
||||
|
||||
this.audioNode.disconnect();
|
||||
this.audioNode.connect(this.gainNode);
|
||||
this.gainNode.connect(this.audioContext.destination);
|
||||
|
||||
this.log('Gain node created and connected');
|
||||
} catch (error) {
|
||||
console.error('Failed to setup gain node:', error);
|
||||
this.log('Error setting up gain node: ' + error);
|
||||
}
|
||||
}
|
||||
|
||||
private setupAnalyser(): void {
|
||||
if (!this.audioNode || !this.audioContext) {
|
||||
this.log('Warning: Audio node not available yet');
|
||||
@ -517,13 +539,19 @@ export class CsoundEngine {
|
||||
}
|
||||
|
||||
try {
|
||||
if (!this.gainNode) {
|
||||
this.setupGainNode();
|
||||
}
|
||||
|
||||
this.scopeNode = this.audioContext.createAnalyser();
|
||||
this.scopeNode.fftSize = 2048;
|
||||
this.scopeNode.smoothingTimeConstant = 0.3;
|
||||
|
||||
this.audioNode.disconnect();
|
||||
this.audioNode.connect(this.scopeNode);
|
||||
this.scopeNode.connect(this.audioContext.destination);
|
||||
if (this.gainNode) {
|
||||
this.gainNode.disconnect();
|
||||
this.gainNode.connect(this.scopeNode);
|
||||
this.scopeNode.connect(this.audioContext.destination);
|
||||
}
|
||||
|
||||
this.log('Analyser node created and connected');
|
||||
this.options.onAnalyserNodeCreated?.(this.scopeNode);
|
||||
@ -537,6 +565,20 @@ export class CsoundEngine {
|
||||
return this.scopeNode;
|
||||
}
|
||||
|
||||
setVolume(value: number): void {
|
||||
if (!this.gainNode) {
|
||||
return;
|
||||
}
|
||||
this.gainNode.gain.value = Math.max(0, Math.min(1, value));
|
||||
}
|
||||
|
||||
getVolume(): number {
|
||||
if (!this.gainNode) {
|
||||
return 1.0;
|
||||
}
|
||||
return this.gainNode.gain.value;
|
||||
}
|
||||
|
||||
private log(message: string): void {
|
||||
this.options.onMessage?.(message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user