Skip to content

Commit

Permalink
fix: vad can trigger voice activity (#112)
Browse files Browse the repository at this point in the history
Co-authored-by: Yohan Totting <[email protected]>
  • Loading branch information
tyohan and tyohan authored Apr 23, 2024
1 parent 9ad8c4a commit 560ddfe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
9 changes: 6 additions & 3 deletions packages/room/peer/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -806,10 +806,13 @@ export const createPeer = ({ api, createStream, event, streams, config }) => {
}

internalChannel.addEventListener('message', (event) => {
const data = JSON.parse(event.data)
const jsonData = JSON.parse(event.data)

if (data.type === 'vad_started' || data.type === 'vad_ended') {
this._onVoiceActivity(data)
if (
jsonData.type === 'vad_started' ||
jsonData.type === 'vad_ended'
) {
this._onVoiceActivity(jsonData.data)
}
})
}
Expand Down
17 changes: 4 additions & 13 deletions packages/room/stream/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const createStream = () => {
source
mediaStream
audioLevel
lastVoiceActivity

/**
* @param {import('./stream-types.js').RoomStreamType.StreamParameters} streamParameters
Expand All @@ -20,6 +21,7 @@ export const createStream = () => {
this.source = source
this.mediaStream = mediaStream
this.audioLevel = 0
this.lastVoiceActivity = 0
}

/**
Expand Down Expand Up @@ -49,6 +51,7 @@ export const createStream = () => {
for (const level of activity.audioLevels) {
this.audioLevel = level.audioLevel
this.#triggerVoiceActivityEvent(this.audioLevel)
this.lastVoiceActivity = Date.now()
}
} else {
this.audioLevel = 0
Expand Down Expand Up @@ -79,19 +82,7 @@ export const createStream = () => {
createInstance: (data) => {
const stream = new Stream(data)

return Object.freeze({
id: stream.id,
clientId: stream.clientId,
name: stream.name,
origin: stream.origin,
source: stream.source,
mediaStream: stream.mediaStream,
audioLevel: 0,
replaceTrack: stream.replaceTrack,
addVoiceActivity: stream.addVoiceActivity,
addEventListener: stream.addEventListener,
removeEventListener: stream.removeEventListener,
})
return stream
},
}
}

0 comments on commit 560ddfe

Please sign in to comment.