-
Notifications
You must be signed in to change notification settings - Fork 308
Expand file tree
/
Copy pathlive_audio_session.h
More file actions
101 lines (77 loc) · 3.49 KB
/
live_audio_session.h
File metadata and controls
101 lines (77 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
#include <string>
#include <memory>
#include <thread>
#include <mutex>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <vector>
#include <gsl/pointers>
#include "live_audio_types.h"
namespace foundry_local::Internal {
struct IFoundryLocalCore;
template <typename T> class ThreadSafeQueue;
} // namespace foundry_local::Internal
namespace foundry_local {
class ILogger;
class LiveAudioTranscriptionSession final {
public:
LiveAudioTranscriptionSession(gsl::not_null<Internal::IFoundryLocalCore*> core,
std::string modelId,
gsl::not_null<ILogger*> logger);
~LiveAudioTranscriptionSession() noexcept;
// Non-copyable, non-movable
LiveAudioTranscriptionSession(const LiveAudioTranscriptionSession&) = delete;
LiveAudioTranscriptionSession& operator=(const LiveAudioTranscriptionSession&) = delete;
LiveAudioTranscriptionSession(LiveAudioTranscriptionSession&&) = delete;
LiveAudioTranscriptionSession& operator=(LiveAudioTranscriptionSession&&) = delete;
/// Mutable settings reference; only effective before Start().
LiveAudioTranscriptionOptions& Settings() { return settings_; }
/// Read-only settings reference.
const LiveAudioTranscriptionOptions& Settings() const { return settings_; }
/// Settings that were active when Start() was called.
const LiveAudioTranscriptionOptions& ActiveSettings() const { return activeSettings_; }
/// Begin the streaming session. Must be called before Append/TryAppend.
void Start();
/// Enqueue PCM audio data. Blocks if the push queue is full.
void Append(const uint8_t* pcmData, size_t length);
/// Try to get the next transcription result within the given timeout.
TranscriptionStatus TryGetNext(LiveAudioTranscriptionResponse& result,
std::chrono::milliseconds timeout = std::chrono::seconds(5));
/// Signal the end of audio input and stop the session.
void Stop();
/// Returns the error message if the session is in an error state.
std::string GetErrorMessage() const;
/// Returns true if the session has been started.
bool IsStarted() const;
/// Returns true if the session has been stopped.
bool IsStopped() const;
private:
enum class SessionState {
Created,
Starting,
Started,
Stopped
};
void PushWorkerLoop();
void StopInternal(std::unique_lock<std::mutex>& lock);
gsl::not_null<Internal::IFoundryLocalCore*> core_;
std::string modelId_;
gsl::not_null<ILogger*> logger_;
LiveAudioTranscriptionOptions settings_;
LiveAudioTranscriptionOptions activeSettings_;
mutable std::mutex mutex_;
SessionState state_ = SessionState::Created;
std::string sessionHandle_;
using AudioChunk = std::vector<uint8_t>;
std::unique_ptr<Internal::ThreadSafeQueue<AudioChunk>> pushQueue_;
std::unique_ptr<Internal::ThreadSafeQueue<LiveAudioTranscriptionResponse>> resultQueue_;
std::thread pushThread_;
std::string errorMessage_;
LiveAudioTranscriptionResponse finalResult_;
bool hasFinalResult_ = false;
};
} // namespace foundry_local