@azure/communication-react
Version:
React library for building modern communication user experiences utilizing Azure Communication Services
105 lines • 2.56 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* Class for playing individual DTMF Tones.
*
* @internal
*/
export class Tone {
constructor(context, frequency1, frequency2) {
this.isPlaying = false;
/**
* Function to play the tone. will create new ocillators because they are one use objects so we need to make
* new ones every time we play a tone.
*/
this.play = () => {
if (this.isPlaying) {
return;
}
const gainNode = this.context.createGain();
gainNode.gain.value = 0.1;
gainNode.connect(this.context.destination);
this.oscillatorNode1 = this.context.createOscillator();
this.oscillatorNode1.frequency.value = this.frequency1;
this.oscillatorNode1.connect(gainNode);
this.oscillatorNode2 = this.context.createOscillator();
this.oscillatorNode2.frequency.value = this.frequency2;
this.oscillatorNode2.connect(gainNode);
this.oscillatorNode1.start();
this.oscillatorNode2.start();
this.isPlaying = true;
};
/**
* Function to stop the tone.
*/
this.stop = () => {
if (this.oscillatorNode1 && this.oscillatorNode2) {
this.oscillatorNode1.stop();
this.oscillatorNode2.stop();
this.oscillatorNode1.disconnect();
this.oscillatorNode2.disconnect();
this.isPlaying = false;
}
};
this.context = context;
this.frequency1 = frequency1;
this.frequency2 = frequency2;
}
}
/**
* Mapping of the different dtmf frequencies that are needed for the creation of sound that
* matches the dtmf tones.
*
* @internal
*/
export const dtmfFrequencies = {
'1': {
f1: 697,
f2: 1209
},
'2': {
f1: 697,
f2: 1336
},
'3': {
f1: 697,
f2: 1477
},
'4': {
f1: 770,
f2: 1209
},
'5': {
f1: 770,
f2: 1336
},
'6': {
f1: 770,
f2: 1477
},
'7': {
f1: 852,
f2: 1209
},
'8': {
f1: 852,
f2: 1336
},
'9': {
f1: 852,
f2: 1477
},
'*': {
f1: 941,
f2: 1209
},
'0': {
f1: 941,
f2: 1336
},
'#': {
f1: 941,
f2: 1477
}
};
//# sourceMappingURL=DTMFToneGenerator.js.map