Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
add python style comments
Browse files Browse the repository at this point in the history
  • Loading branch information
MGTheTrain committed Apr 17, 2024
1 parent 4804437 commit ee2c4a0
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 1 deletion.
87 changes: 86 additions & 1 deletion bindings/python/audio_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,20 @@
import ctypes

class AudioWrapper:
"""
A wrapper class for an audio library implemented in C/C++ using ctypes.
Attributes:
audio_lib (CDLL): The loaded audio library.
"""

def __init__(self, path: str):
"""
Initialize the AudioWrapper instance.
Args:
path (str): The path to the audio library.
"""
self.audio_lib = ctypes.cdll.LoadLibrary(path)

self.audio_lib.loadAudioFile.argtypes = [ctypes.c_char_p, ctypes.POINTER(AudioData)]
Expand All @@ -41,29 +54,84 @@ def __init__(self, path: str):
self.audio_lib.playbackCallback.restype = ctypes.c_int

def load_audio_file(self, filename, audio_data):
"""
Load an audio file using the audio library.
Args:
filename (str): The path to the audio file.
audio_data (AudioData): The AudioData structure to store the loaded audio data.
Returns:
bool: True if the audio file was successfully loaded, False otherwise.
"""
return self.audio_lib.loadAudioFile(filename.encode('utf-8'), ctypes.byref(audio_data))

def start_playback(self, audio_data):
"""
Start playback of the loaded audio file using the audio library.
Args:
audio_data (AudioData): The AudioData structure containing the loaded audio data.
Returns:
bool: True if playback was successfully started, False otherwise.
"""
return self.audio_lib.startPlayback(ctypes.byref(audio_data))

def close_audio_file(self, audio_data):
"""
Close the loaded audio file using the audio library.
Args:
audio_data (AudioData): The AudioData structure containing the audio data to close.
"""
return self.audio_lib.closeAudioFile(ctypes.byref(audio_data))


class AudioData(ctypes.Structure):
"""
Structure to hold audio data, including file handle, information, and playback stream.
Attributes:
file (c_void_p): File handle.
info (SF_INFO): Audio file information.
stream (c_void_p): Playback stream.
"""
_fields_ = [
("file", ctypes.c_void_p),
("info", SF_INFO),
("stream", ctypes.c_void_p)
]


class PaStreamCallbackTimeInfo(ctypes.Structure):
"""
Structure to hold timing information for the audio playback callback.
Attributes:
inputBufferAdcTime (c_double): Time of the input buffer.
currentTime (c_double): Current time.
outputBufferDacTime (c_double): Time of the output buffer.
"""
_fields_ = [
("inputBufferAdcTime", ctypes.c_double),
("currentTime", ctypes.c_double),
("outputBufferDacTime", ctypes.c_double)
]


class SF_INFO(ctypes.Structure):
"""
Structure to hold information about an audio file.
Attributes:
frames (c_int): Total frames.
samplerate (c_int): Sample rate.
channels (c_int): Number of channels.
format (c_int): Format of the audio data.
sections (c_int): Sections.
seekable (c_int): Seekable flag.
"""
_fields_ = [
("frames", ctypes.c_int),
("samplerate", ctypes.c_int),
Expand All @@ -73,5 +141,22 @@ class SF_INFO(ctypes.Structure):
("seekable", ctypes.c_int)
]


def playback_callback(input, output, frame_count, time_info, status_flags, user_data):
return AudioWrapper().audio_lib.playbackCallback(input, output, frame_count, ctypes.byref(time_info), status_flags, user_data)
"""
Callback function for audio playback.
This function is called by the audio library when it needs more audio data to play.
Args:
input: Pointer to the input buffer (not used in playback).
output: Pointer to the output buffer where audio data should be written.
frame_count (int): Number of frames requested for playback.
time_info (PaStreamCallbackTimeInfo): Timing information for the callback.
status_flags (int): Flags indicating possible errors or other information.
user_data: Pointer to user data provided when the stream was opened.
Returns:
int: paContinue if playback should continue, paComplete if playback is complete.
"""
return AudioWrapper().audio_lib.playbackCallback(input, output, frame_count, ctypes.byref(time_info), status_flags, user_data)
71 changes: 71 additions & 0 deletions bindings/python/core_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,20 @@
import ctypes

class CoreWrapper:
"""
A wrapper class for a core library implemented in C/C++ using ctypes.
Attributes:
core_lib (CDLL): The loaded core library.
"""

def __init__(self, path: str):
"""
Initialize the CoreWrapper instance.
Args:
path (str): The path to the core library.
"""
self.core_lib = ctypes.cdll.LoadLibrary(path)
self.core_lib.add.restype = ctypes.c_int32
self.core_lib.add.argtypes = [ctypes.c_int32, ctypes.c_int32]
Expand All @@ -44,19 +57,77 @@ def __init__(self, path: str):
self.core_lib.getCircleCircumference.argtypes = [ctypes.c_float]

def add(self, a, b):
"""
Add two integers using the core library.
Args:
a (int): The first integer.
b (int): The second integer.
Returns:
int: The result of the addition.
"""
return self.core_lib.add(a, b)

def subtract(self, a, b):
"""
Subtract two integers using the core library.
Args:
a (int): The first integer.
b (int): The second integer.
Returns:
int: The result of the subtraction.
"""
return self.core_lib.subtract(a, b)

def multiply(self, a, b):
"""
Multiply two integers using the core library.
Args:
a (int): The first integer.
b (int): The second integer.
Returns:
int: The result of the multiplication.
"""
return self.core_lib.multiply(a, b)

def divide(self, a, b):
"""
Divide two floats using the core library.
Args:
a (float): The numerator.
b (float): The denominator.
Returns:
float: The result of the division.
"""
return self.core_lib.divide(a, b)

def get_circle_area(self, radius):
"""
Calculate the area of a circle using the core library.
Args:
radius (float): The radius of the circle.
Returns:
float: The area of the circle.
"""
return self.core_lib.getCircleArea(radius)

def get_circle_circumference(self, radius):
"""
Calculate the circumference of a circle using the core library.
Args:
radius (float): The radius of the circle.
Returns:
float: The circumference of the circle.
"""
return self.core_lib.getCircleCircumference(radius)
8 changes: 8 additions & 0 deletions bindings/python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
from audio_wrapper import AudioWrapper

def main(path, wrapper_type, audio_file_path):
"""
Main function to demonstrate the usage of wrapper libraries.
Args:
path (str): Path to the wrapper library.
wrapper_type (str): Type of wrapper to use: 'core' or 'audio'.
audio_file_path (str): Path to the audio file (required if wrapper_type is 'audio').
"""
abs_path = os.path.realpath(path)

if wrapper_type == 'core':
Expand Down

0 comments on commit ee2c4a0

Please sign in to comment.