generated from deepgram/oss-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
dvonthenen
committed
Nov 22, 2023
1 parent
bd398fc
commit 4113b70
Showing
30 changed files
with
860 additions
and
259 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,8 +1,2 @@ | ||
DG_API_KEY="" | ||
DG_API_KEY_MANAGE="" | ||
DG_PROJECT_ID="85bb84f9-02d8-742a-df82-" | ||
DG_API_KEY_ID="" | ||
DG_MEMBER_ID="" | ||
DG_REQUEST_ID="" | ||
DG_BALANCE_ID="" | ||
EMAIL="" | ||
DG_PROJECT_ID="" |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright 2023 Deepgram SDK contributors. All Rights Reserved. | ||
# Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
# SPDX-License-Identifier: MIT | ||
|
||
class DeepgramMicrophoneError(Exception): | ||
""" | ||
Exception raised for known errors related to Microphone library. | ||
Attributes: | ||
message (str): The error message describing the exception. | ||
status (str): The HTTP status associated with the API error. | ||
original_error (str - json): The original error that was raised. | ||
""" | ||
def __init__(self, message: str): | ||
super().__init__(message) | ||
self.name = "DeepgramMicrophoneError" | ||
self.message = message | ||
|
||
def __str__(self): | ||
return f"{self.name}: {self.message}" |
This file contains 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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Copyright 2023 Deepgram SDK contributors. All Rights Reserved. | ||
# Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
# SPDX-License-Identifier: MIT | ||
|
||
import inspect | ||
import asyncio | ||
import threading | ||
import pyaudio | ||
from array import array | ||
from sys import byteorder | ||
|
||
from .errors import DeepgramMicrophoneError | ||
|
||
FORMAT = pyaudio.paInt16 | ||
CHANNELS = 1 | ||
RATE = 16000 | ||
CHUNK = 8000 | ||
|
||
class Microphone: | ||
""" | ||
TODO | ||
""" | ||
def __init__(self, push_callback, format=FORMAT, rate=RATE, chunk=CHUNK, channels=CHANNELS): | ||
self.audio = pyaudio.PyAudio() | ||
self.chunk = chunk | ||
self.rate = rate | ||
self.format = format | ||
self.channels = channels | ||
self.push_callback = push_callback | ||
self.stream = None | ||
|
||
def is_active(self): | ||
if self.stream is None: | ||
return False | ||
return self.stream.is_active() | ||
|
||
def start(self): | ||
if self.stream is not None: | ||
raise DeepgramMicrophoneError("Microphone already started") | ||
|
||
self.stream = self.audio.open( | ||
format=self.format, | ||
channels=self.channels, | ||
rate=self.rate, | ||
input=True, | ||
frames_per_buffer=CHUNK, | ||
) | ||
|
||
self.exit = False | ||
self.lock = threading.Lock() | ||
|
||
self.stream.start_stream() | ||
self.thread = threading.Thread(target=self.processing) | ||
self.thread.start() | ||
|
||
def processing(self): | ||
try: | ||
while True: | ||
data = self.stream.read(self.chunk) | ||
|
||
self.lock.acquire() | ||
localExit = self.exit | ||
self.lock.release() | ||
if localExit: | ||
break | ||
if data is None: | ||
continue | ||
|
||
if inspect.iscoroutinefunction(self.push_callback): | ||
asyncio.run(self.push_callback(data)) | ||
else: | ||
self.push_callback(data) | ||
|
||
except Exception as e: | ||
print(f"Error while sending: {str(e)}") | ||
raise | ||
|
||
def finish(self): | ||
self.lock.acquire() | ||
self.exit = True | ||
self.lock.release() | ||
|
||
self.thread.join() | ||
self.thread = None | ||
|
||
self.stream.stop_stream() | ||
self.stream.close() | ||
self.stream = None | ||
|
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Copyright 2023 Deepgram SDK contributors. All Rights Reserved. | ||
# Use of this source code is governed by a MIT license that can be found in the LICENSE file. | ||
# SPDX-License-Identifier: MIT | ||
|
||
class DeepgramError(Exception): | ||
""" | ||
Exception raised for unknown errors related to the Deepgram API. | ||
Attributes: | ||
message (str): The error message describing the exception. | ||
status (str): The HTTP status associated with the API error. | ||
""" | ||
def __init__(self, message: str): | ||
super().__init__(message) | ||
self.name = "DeepgramError" | ||
self.message = message | ||
|
||
def __str__(self): | ||
return f"{self.name}: {self.message}" | ||
|
||
class DeepgramApiError(Exception): | ||
""" | ||
Exception raised for known errors (in json response format) related to the Deepgram API. | ||
Attributes: | ||
message (str): The error message describing the exception. | ||
status (str): The HTTP status associated with the API error. | ||
original_error (str - json): The original error that was raised. | ||
""" | ||
def __init__(self, message: str, status: str, original_error = None): | ||
super().__init__(message) | ||
self.name = "DeepgramApiError" | ||
self.status = status | ||
self.message = message | ||
self.original_error = original_error | ||
|
||
def __str__(self): | ||
return f"{self.name}: {self.message} (Status: {self.status})" | ||
|
||
class DeepgramUnknownApiError(Exception): | ||
""" | ||
Exception raised for unknown errors related to the Deepgram API. | ||
Attributes: | ||
message (str): The error message describing the exception. | ||
status (str): The HTTP status associated with the API error. | ||
""" | ||
def __init__(self, message: str, status: str): | ||
super().__init__(message, status) | ||
self.name = "DeepgramUnknownApiError" | ||
self.status = status | ||
self.message = message | ||
|
||
def __str__(self): | ||
return f"{self.name}: {self.message} (Status: {self.status})" |
This file contains 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
Oops, something went wrong.