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

Commit

Permalink
swap lines in that ctypes.POINTER(AudioData) data type can be set in …
Browse files Browse the repository at this point in the history
…subsequent method input args
  • Loading branch information
MGTheTrain committed Apr 17, 2024
1 parent 128886d commit b57f074
Showing 1 changed file with 54 additions and 55 deletions.
109 changes: 54 additions & 55 deletions bindings/python/audio_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,59 @@
import ctypes
import pyaudio


class SF_INFO(ctypes.Structure):
"""
Structure to hold information about an audio file.
Attributes:
frames (c_int64): 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_int64),
("samplerate", ctypes.c_int),
("channels", ctypes.c_int),
("format", ctypes.c_int),
("sections", ctypes.c_int),
("seekable", ctypes.c_int)
]

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 AudioWrapper:
"""
A wrapper class for an audio library implemented in C/C++ using ctypes.
Expand Down Expand Up @@ -101,7 +154,7 @@ def start_playback(self, audio_data: ctypes.POINTER(AudioData)) -> bool:
bool: True if playback was successfully started, False otherwise.
"""
stream = self.pyaudio.open(
format=self.pyaudio.get_format_from_width(4), # 4 bytes per float
format=self.pyaudio.get_format_from_width(4),
channels=audio_data.info.channels,
rate=audio_data.info.samplerate,
output=True,
Expand All @@ -117,64 +170,10 @@ def close_audio_file(self, audio_data: ctypes.POINTER(AudioData)):
Args:
audio_data (AudioData): The AudioData structure containing the audio data to close.
"""
# Close the PyAudio stream
self.pyaudio.terminate()
# Close the audio file using the audio library
self.audio_lib.closeAudioFile(ctypes.byref(audio_data))


class SF_INFO(ctypes.Structure):
"""
Structure to hold information about an audio file.
Attributes:
frames (c_int64): 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_int64),
("samplerate", ctypes.c_int),
("channels", ctypes.c_int),
("format", ctypes.c_int),
("sections", ctypes.c_int),
("seekable", ctypes.c_int)
]

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)
]

def playback_callback(input, output, frame_count, time_info, status_flags, user_data):
"""
Callback function for audio playback.
Expand Down

0 comments on commit b57f074

Please sign in to comment.