Skip to content

Commit

Permalink
python wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
danemadsen committed Aug 5, 2024
1 parent 99d9d67 commit 14dcb49
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions wrappers/babylon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import ctypes
import os

# Load the shared library
if os.name == 'nt': # Windows
babylon_lib = ctypes.CDLL('babylon.dll')
elif os.name == 'mac': # macOS
babylon_lib = ctypes.CDLL('./babylon.dylib')
else: # Linux/Unix
babylon_lib = ctypes.CDLL('./babylon.so')

# Define the function prototypes
babylon_lib.babylon_g2p_init.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int]
babylon_lib.babylon_g2p_init.restype = ctypes.c_int

babylon_lib.babylon_g2p.argtypes = [ctypes.c_char_p]
babylon_lib.babylon_g2p.restype = ctypes.c_char_p

babylon_lib.babylon_g2p_free.argtypes = []
babylon_lib.babylon_g2p_free.restype = None

babylon_lib.babylon_tts_init.argtypes = [ctypes.c_char_p]
babylon_lib.babylon_tts_init.restype = ctypes.c_int

babylon_lib.babylon_tts.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
babylon_lib.babylon_tts.restype = None

babylon_lib.babylon_tts_free.argtypes = []
babylon_lib.babylon_tts_free.restype = None

# Initialize G2P
def init_g2p(model_path, language, use_punctuation):
return babylon_lib.babylon_g2p_init(model_path.encode('utf-8'), language.encode('utf-8'), use_punctuation)

# Use G2P
def g2p(text):
result = babylon_lib.babylon_g2p(text.encode('utf-8'))
return result.decode('utf-8')

# Free G2P resources
def free_g2p():
babylon_lib.babylon_g2p_free()

# Initialize TTS
def init_tts(model_path):
return babylon_lib.babylon_tts_init(model_path.encode('utf-8'))

# Use TTS
def tts(text, output_path):
babylon_lib.babylon_tts(text.encode('utf-8'), output_path.encode('utf-8'))

# Free TTS resources
def free_tts():
babylon_lib.babylon_tts_free()

# Example usage
if __name__ == '__main__':
model_path = 'path/to/model'
language = 'en'
use_punctuation = 1

if init_g2p(model_path, language, use_punctuation) == 0:
print('G2P initialized successfully')
phonemes = g2p('Hello world')
print(f'Phonemes: {phonemes}')
free_g2p()
else:
print('Failed to initialize G2P')

if init_tts(model_path) == 0:
print('TTS initialized successfully')
tts('Hello world', 'output.wav')
free_tts()
else:
print('Failed to initialize TTS')

0 comments on commit 14dcb49

Please sign in to comment.