The Python API to the HANCE engine, which offers realtime audio enhancement.
Project description
Audio Enhancement in Python with HANCE
HANCE is an audio enhancement engine that delivers impressive performance. With its Python wrapper, you can easily integrate HANCE into your Python projects to improve the quality of audio signals.
To learn more about HANCE, visit Hance.ai.
Installation
To install the Python wrapper for HANCE, use 'pip':
python -m pip install hance
HANCE is compatible with Python 3 and later.
How to Use
The HANCE Python API is a wrapper around the C++ library. Here are a few ways you can use it.
To use the API, import it and list the available models:
import hance
models = hance.list_models()
print(models)
Process a file
To process a file with HANCE, you can use the process_file function as follows:
import hance
models = hance.list_models()
hance.process_file(models[0], input_file_path, output_file_path)
This will apply the enhancement model specified by models[0] to the input file located at input_file_path, and save the enhanced audio to the output file at output_file_path. Please note that in this example, we are using PySoundFile to read and write audio files. While PySoundFile is not a requirement for using HANCE, it is a convenient library for handling audio files in Python. If you wish to use the process_file function as shown here, you will need to install PySoundFile.
Process a stream
In addition to processing audio files, HANCE can also be used on audio streams in real-time. Here is an example using pyaudio to record the microphone, process it in real time, and output it to headphones.
import pyaudio
engine = hance.HanceEngine()
p = pyaudio.PyAudio()
FORMAT = pyaudio.paFloat32
CHANNELS = 1
RATE = 44100
CHUNK = 512
print("\nRecord audio from a microphone and process it in realtime with HANCE.")
print("To prevent feedback, make sure you are wearing headphones.")
print("PyAudio will induce some latency with the roundtrip to the soundcard,\nbut the HANCE engine runs in realtime.")
print("")
# Get a list of available input devices
input_devices = []
for i in range(p.get_device_count()):
device_info = p.get_device_info_by_index(i)
if device_info["maxInputChannels"] > 0:
input_devices.append(device_info)
# Print the list of available input devices and ask the user to select one
print("Available input devices:")
for i, device in enumerate(input_devices):
print(f"{i}: {device['name']}")
input_device_index = int(input("\nSelect an input device by entering its number: "))
input_device_info = input_devices[input_device_index]
# Get a list of available output devices
output_devices = []
for i in range(p.get_device_count()):
device_info = p.get_device_info_by_index(i)
if device_info["maxOutputChannels"] > 0:
output_devices.append(device_info)
# Print the list of available output devices and ask the user to select one
print("\nAvailable output devices:")
for i, device in enumerate(output_devices):
print(f"{i}: {device['name']}")
output_device_index = int(input("\nSelect an output device by entering its number: "))
output_device_info = output_devices[output_device_index]
models = hance.list_models()
processor = engine.create_processor(models[0], CHANNELS, RATE)
stop_thread = False
processor_active = True
def record_and_playback_thread():
stream_record = p.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
input_device_index=input_device_info['index'],
frames_per_buffer=CHUNK)
stream_play = p.open(format=pyaudio.paFloat32,
channels=1,
rate=RATE,
frames_per_buffer=CHUNK,
output=True,
output_device_index=output_device_info['index']
)
while not stop_thread:
data = stream_record.read(CHUNK, exception_on_overflow = False)
audio_buffer = np.frombuffer(data, dtype=np.float32)
if processor_active:
audio_buffer = processor.process(audio_buffer)
stream_play.write(audio_buffer.astype(np.float32).tobytes())
# stop Recording
stream_record.stop_stream()
stream_record.close()
stream_play.stop_stream()
stream_play.close()
t = threading.Thread(target=record_and_playback_thread)
t.start()
print("\nThe microphone and processing is active")
while True:
user_input = input("Enter 'p' to toggle processing on and off or 'x' to exit the thread: ")
if user_input.lower() == "p":
# Bypass processing and continue the loop
if processor_active:
processor_active = False
print("The processing is bypassed")
else:
processor_active = True
print("The processing is active")
elif user_input.lower() == "x":
# Stop the thread
stop_thread = True
break
t.join()
p.terminate()
For more information and examples on using HANCE, see the HANCE documentation.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
File details
Details for the file hance-1.0.5-py3-none-win_amd64.whl
.
File metadata
- Download URL: hance-1.0.5-py3-none-win_amd64.whl
- Upload date:
- Size: 8.9 MB
- Tags: Python 3, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2492a76766c607f689a6726991ac629bbf90d85a5708ab22aed29a3bc26a468f |
|
MD5 | 1f511ee74f61cac42e33b9e67b6be148 |
|
BLAKE2b-256 | 867b36a7fff86413c96004bb3e65475550d2a9d8c2e079ca873aa244f1fba3cd |
File details
Details for the file hance-1.0.5-py3-none-win32.whl
.
File metadata
- Download URL: hance-1.0.5-py3-none-win32.whl
- Upload date:
- Size: 7.0 MB
- Tags: Python 3, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40505acfb7c5abac8b4fb3d49a01f4234168416859ab6f8032646dc8f797c578 |
|
MD5 | 374733b81c2ea17b9a2ba763c4668203 |
|
BLAKE2b-256 | 6097dbb56914f11f86544a4c766cf682815bc96bdcc46c513975d537f66d5a44 |
File details
Details for the file hance-1.0.5-py3-none-manylinux1_x86_64.whl
.
File metadata
- Download URL: hance-1.0.5-py3-none-manylinux1_x86_64.whl
- Upload date:
- Size: 13.5 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 927998b3b78e717bce05542959909806f8df6be8c3dee70671f554646ef30bb1 |
|
MD5 | 4d496a4150fa3fca991ca6cc425c7360 |
|
BLAKE2b-256 | d0b75198077e354d728f192e56b1cd1005c9875f96d5993619033ad9ae906ee2 |
File details
Details for the file hance-1.0.5-py3-none-macosx_11_0_arm64.whl
.
File metadata
- Download URL: hance-1.0.5-py3-none-macosx_11_0_arm64.whl
- Upload date:
- Size: 9.8 MB
- Tags: Python 3, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d2e3d8532d54090c346c4434612dc1117cff00b3c9a807e70e3d0917f0531362 |
|
MD5 | c4bc258da810f0323611a596495571f8 |
|
BLAKE2b-256 | bc353e6c857d7938ae2c1b0c07618ca1dc22c6d8dbe2193592a83dde416b7e78 |
File details
Details for the file hance-1.0.5-py3-none-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: hance-1.0.5-py3-none-macosx_10_9_x86_64.whl
- Upload date:
- Size: 9.8 MB
- Tags: Python 3, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 657c27327d58fbf5d2a453e2fa37d5481c074196f48da879f0641602285bf7ec |
|
MD5 | 7c960f78f36a8626e085f3b5dfd43083 |
|
BLAKE2b-256 | 3ca76164af1e16d04f79bb5b39e37728e447911d63b984f9830edd3cedd0c24d |