Skip to content

Commit

Permalink
Merge pull request #35 from privateai/update-bleep-parameters
Browse files Browse the repository at this point in the history
Add support for bleep gain and frequency adjustments
  • Loading branch information
letmerecall authored Jan 18, 2024
2 parents fb44ccc + 1b8f780 commit bf0a028
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 16 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@

### Added

### Changed

### Fixed


## [3.6.3] - 2024-01-18

### Added
* Added support of 2 new paramters in `audio_options_obj` and `bleep_obj` objects:
* `bleep_gain` - Sets the gain level, in decibels (dB), for the bleep sound within the audio segment.
* `bleep_frequency` - Configures the frequency of the sine wave used for the bleep sound in an audio segment.

### Changed
* Updated formatting of CHANGELOG.md

Expand Down
2 changes: 1 addition & 1 deletion src/privateai_client/__about__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "3.6.2"
__version__ = "3.6.3"
48 changes: 46 additions & 2 deletions src/privateai_client/components/request_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,24 @@ def _fromdict(cls, values: dict):
class AudioOptions(BaseRequestObject):
default_bleep_start_padding: float = 0.5
default_bleep_end_padding: float = 0.5
default_bleep_frequency: Optional[int] = None
default_bleep_gain: Optional[int] = None

def __init__(
self,
bleep_start_padding: float = default_bleep_start_padding,
bleep_end_padding: float = default_bleep_end_padding,
bleep_frequency: Optional[int] = default_bleep_frequency,
bleep_gain: Optional[int] = default_bleep_gain
):
if self._bleep_start_padding_validator(bleep_start_padding):
self._bleep_start_padding = bleep_start_padding
if self._bleep_end_padding_validator(bleep_end_padding):
self._bleep_end_padding = bleep_end_padding
if self._bleep_frequency_validator(bleep_frequency):
self._bleep_frequency = bleep_frequency
if self._bleep_gain_validator(bleep_gain):
self._bleep_gain = bleep_gain

@property
def bleep_start_padding(self):
Expand All @@ -52,6 +60,14 @@ def bleep_start_padding(self):
def bleep_end_padding(self):
return self._bleep_end_padding

@property
def bleep_frequency(self):
return self._bleep_frequency

@property
def bleep_gain(self):
return self._bleep_gain

@bleep_start_padding.setter
def bleep_start_padding(self, var):
if self._bleep_start_padding_validator(var):
Expand All @@ -62,6 +78,16 @@ def bleep_end_padding(self, var):
if self._bleep_end_padding_validator(var):
self._bleep_end_padding = var

@bleep_frequency.setter
def bleep_frequency(self, var):
if self._bleep_frequency_validator(var):
self._bleep_frequency = var

@bleep_gain.setter
def bleep_gain(self, var):
if self._bleep_gain_validator(var):
self._bleep_gain = var

def _bleep_start_padding_validator(self, var):
if type(var) is not float:
raise ValueError(
Expand All @@ -80,13 +106,27 @@ def _bleep_end_padding_validator(self, var):
raise ValueError("AudioOptions.bleep_end_padding must be positive")
return True

def _bleep_frequency_validator(self, var):
if type(var) is not int and var is not None:
raise ValueError(
f"AudioOptions.bleep_frequency must be of type int or None, but got {type(var)}"
)
return True

def _bleep_gain_validator(self, var):
if type(var) is not int and var is not None:
raise ValueError(
f"AudioOptions.bleep_gain must be of type int or None, but got {type(var)}"
)
return True

@classmethod
def fromdict(cls, values: dict):
try:
return cls._fromdict(values)
except TypeError:
raise TypeError(
"ProcessedText can only accept the values 'bleep_start_padding' and 'bleep_end_padding'"
"ProcessedText can only accept the values 'bleep_start_padding', 'bleep_end_padding', 'bleep_frequency', and 'bleep_gain'"
)


Expand Down Expand Up @@ -802,9 +842,13 @@ def __init__(
self,
file: File,
timestamps: List,
bleep_frequency: Optional[int] = None,
bleep_gain: Optional[int] = None
):
self.file = file
self.timestamps = timestamps
self.bleep_frequency = bleep_frequency
self.bleep_gain = bleep_gain

@classmethod
def fromdict(cls, values: dict):
Expand All @@ -822,7 +866,7 @@ def fromdict(cls, values: dict):
return cls._fromdict(initializer_dict)
except TypeError:
raise TypeError(
"BleepRequest can only accept the values 'file'and 'timestamps'"
"BleepRequest can only accept the values 'file', 'timestamps', 'bleep_frequency', and 'bleep_gain'"
)


Expand Down
Binary file not shown.
38 changes: 38 additions & 0 deletions src/privateai_client/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,41 @@ def test_process_file_base64():
request_obj = rq.file_base64_obj(file=file_obj)
resp = client.process_files_base64(request_object=request_obj)
assert resp.ok

def test_process_audio_file_base64():
client = _get_client()

test_dir = "/".join(__file__.split("/")[:-1])
file_name = "test_audio.mp3"
filepath = os.path.join(f"{test_dir}", "test_files", file_name)
file_type= "audio/mp3"

with open(filepath, "rb") as b64_file:
file_data = base64.b64encode(b64_file.read())
file_data = file_data.decode("ascii")

file_obj = rq.file_obj(data=file_data, content_type=file_type)
audio_option_obj = rq.audio_options_obj(bleep_gain=-50, bleep_frequency=300)
request_obj = rq.file_base64_obj(file=file_obj, audio_options=audio_option_obj)
resp = client.process_files_base64(request_object=request_obj)
assert resp.ok

def test_bleep():
client = _get_client()

test_dir = "/".join(__file__.split("/")[:-1])
file_name = "test_audio.mp3"
filepath = os.path.join(f"{test_dir}", "test_files", file_name)
file_type= "audio/mp3"

with open(filepath, "rb") as b64_file:
file_data = base64.b64encode(b64_file.read())
file_data = file_data.decode("ascii")

file_obj = rq.file_obj(data=file_data, content_type=file_type)
timestamp = rq.timestamp_obj(start=1.0, end=2.0)

request_obj = rq.bleep_obj(file=file_obj, timestamps=[timestamp], bleep_frequency=500, bleep_gain=-30)
resp = client.bleep(request_object=request_obj)
assert resp.ok

Loading

0 comments on commit bf0a028

Please sign in to comment.