-
Notifications
You must be signed in to change notification settings - Fork 2
OpenAI speech detect #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -298,6 +298,7 @@ class AudioStreamer { | |
| } else if(jsType && strcmp(jsType, "response.audio.delta") == 0) { | ||
| const char* jsonAudio = cJSON_GetObjectCstr(json, "delta"); | ||
| playback_clear_requested = false; | ||
| m_response_audio_done = false; | ||
|
|
||
| if(jsonAudio && strlen(jsonAudio) > 0) { | ||
| std::string rawAudio; | ||
|
|
@@ -336,7 +337,10 @@ class AudioStreamer { | |
| } else { | ||
| switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "(%s) processMessage - response.audio.delta no audio data\n", m_sessionId.c_str()); | ||
| } | ||
| } | ||
| } else if(jsType && strcmp(jsType, "response.audio.done") == 0) { | ||
| switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "(%s) processMessage - audio done\n", m_sessionId.c_str()); | ||
| m_response_audio_done = true; | ||
| } | ||
| cJSON_Delete(json); | ||
| return status; | ||
| } | ||
|
|
@@ -351,6 +355,7 @@ class AudioStreamer { | |
| void push_audio_queue(const std::vector<int16_t>& audio_data) { | ||
| std::lock_guard<std::mutex> lock(m_audio_queue_mutex); | ||
| m_audio_queue.push(audio_data); | ||
| switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "(%s) audio queue size: %zu\n", m_sessionId.c_str(), m_audio_queue.size()); | ||
| } | ||
|
|
||
| std::vector<int16_t> pop_audio_queue() { | ||
|
|
@@ -429,6 +434,33 @@ class AudioStreamer { | |
| return playback_clear_requested; | ||
| } | ||
|
|
||
| bool is_openai_speaking() { | ||
| return m_openai_speaking; | ||
| } | ||
|
|
||
| bool is_response_audio_done() { | ||
| return m_response_audio_done; | ||
| } | ||
|
|
||
| void openai_speech_started() { | ||
| m_openai_speaking = true; | ||
| switch_core_session_t* psession = switch_core_session_locate(m_sessionId.c_str()); | ||
| switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "(%s) Openai started speaking\n", m_sessionId.c_str()); | ||
| const char *payload = "{\"status\":\"started\"}"; | ||
| m_notify(psession, EVENT_OPENAI_SPEECH_STARTED, payload); | ||
| switch_core_session_rwunlock(psession); | ||
| } | ||
|
|
||
| void openai_speech_stopped() { | ||
| m_openai_speaking = false; | ||
| switch_core_session_t* psession = switch_core_session_locate(m_sessionId.c_str()); | ||
| switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "(%s) Openai stopped speaking\n", m_sessionId.c_str()); | ||
|
|
||
| const char *payload = "{\"status\":\"stopped\"}"; | ||
| m_notify(psession, EVENT_OPENAI_SPEECH_STOPPED, payload); | ||
| switch_core_session_rwunlock(psession); | ||
| } | ||
|
|
||
|
|
||
| private: | ||
| std::string m_sessionId; | ||
|
|
@@ -446,6 +478,8 @@ class AudioStreamer { | |
| std::mutex m_audio_queue_mutex; | ||
| bool playback_clear_requested = false; | ||
| bool m_disable_audiofiles = false; // disable saving audio files if true | ||
| bool m_openai_speaking = false; | ||
| bool m_response_audio_done = false; | ||
| }; | ||
|
|
||
|
|
||
|
|
@@ -983,11 +1017,16 @@ extern "C" { | |
|
|
||
| if (as->clear_requested()) { | ||
| switch_buffer_zero(tech_pvt->playback_buffer); | ||
| inuse = 0; | ||
| } | ||
| if (inuse < bytes_needed * 2 && !as->is_audio_queue_empty()) { | ||
| auto chunk = as->pop_audio_queue(); | ||
| switch_buffer_write(tech_pvt->playback_buffer, chunk.data(), chunk.size() * sizeof(int16_t)); | ||
| } else if (inuse == 0) { | ||
| // Openai just finished speaking for interruption or end of response | ||
| if(as->is_openai_speaking() && as->is_response_audio_done()) { | ||
|
||
| as->openai_speech_stopped(); | ||
| } | ||
| return SWITCH_TRUE; | ||
| } | ||
|
|
||
|
|
@@ -999,6 +1038,10 @@ extern "C" { | |
| switch_buffer_read(tech_pvt->playback_buffer, data, inuse); | ||
| } | ||
|
|
||
| if (!as->is_openai_speaking()) { | ||
| as->openai_speech_started(); | ||
| } | ||
|
|
||
| frame->datalen = inuse > bytes_needed ? bytes_needed : inuse; | ||
| frame->samples = frame->datalen / bytes_per_sample; | ||
|
|
||
|
|
@@ -1043,4 +1086,3 @@ extern "C" { | |
| return SWITCH_STATUS_FALSE; | ||
| } | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting
inuse = 0after clearing the buffer could cause incorrect behavior. Theinusevariable represents the actual bytes in the buffer, and afterswitch_buffer_zero()it should reflect the buffer's empty state, but this assignment might interfere with the subsequent logic that depends on the actual buffer size.