python通过ctypes混合调用c/c++封装开源音频引擎libsoundio (代码篇)

接上一篇文章 python通过ctypes调用c封装开源音频引擎libsoundio,因为python混合c封装了全部libsoundio函数,所以篇幅太长,下面直接贴代码

贴上代码,本文的代码仅是pythonc types封调,代码中并未做面向对象封装,请知晓

soundio.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__version__ = "1.0.3"
__author__ = "[email protected]"
import os
from ctypes import *
from enum import Enum, unique
 
"""
cross-platform audio input and output for real-time & consumer software
http://libsound.io
"""
 
"""
def method_find(act=""):
    def _wrap(func):
        def _param(*args, **kwargs):
            return func(*args,*kwargs)
        return _param
    return _wrap
"""
 
 
@unique
class SoundIoChannelId(Enum):
    SoundIoChannelIdInvalid = 0
    SoundIoChannelIdFrontLeft = 1
    SoundIoChannelIdFrontRight = 2
    SoundIoChannelIdFrontCenter = 3
    SoundIoChannelIdLfe = 4
    SoundIoChannelIdBackLeft = 5
    SoundIoChannelIdBackRight = 6
    SoundIoChannelIdFrontLeftCenter = 7
    SoundIoChannelIdFrontRightCenter = 8
    SoundIoChannelIdBackCenter = 9
    SoundIoChannelIdSideLeft = 10
    SoundIoChannelIdSideRight = 11
    SoundIoChannelIdTopCenter = 12
    SoundIoChannelIdTopFrontLeft = 13
    SoundIoChannelIdTopFrontCenter = 14
    SoundIoChannelIdTopFrontRight = 15
    SoundIoChannelIdTopBackLeft = 16
    SoundIoChannelIdTopBackCenter = 17
    SoundIoChannelIdTopBackRight = 18
 
    SoundIoChannelIdBackLeftCenter = 19
    SoundIoChannelIdBackRightCenter = 20
    SoundIoChannelIdFrontLeftWide = 21
    SoundIoChannelIdFrontRightWide = 22
    SoundIoChannelIdFrontLeftHigh = 23
    SoundIoChannelIdFrontCenterHigh = 24
    SoundIoChannelIdFrontRightHigh = 25
    SoundIoChannelIdTopFrontLeftCenter = 26
    SoundIoChannelIdTopFrontRightCenter = 27
    SoundIoChannelIdTopSideLeft = 28
    SoundIoChannelIdTopSideRight = 29
    SoundIoChannelIdLeftLfe = 30
    SoundIoChannelIdRightLfe = 31
    SoundIoChannelIdLfe2 = 32
    SoundIoChannelIdBottomCenter = 33
    SoundIoChannelIdBottomLeftCenter = 34
    SoundIoChannelIdBottomRightCenter = 35
 
    SoundIoChannelIdMsMid = 36
    SoundIoChannelIdMsSide = 37
 
    SoundIoChannelIdAmbisonicW = 38
    SoundIoChannelIdAmbisonicX = 39
    SoundIoChannelIdAmbisonicY = 40
    SoundIoChannelIdAmbisonicZ = 41
 
    SoundIoChannelIdXyX = 42
    SoundIoChannelIdXyY = 43
 
    SoundIoChannelIdHeadphonesLeft = 44
    SoundIoChannelIdHeadphonesRight = 45
    SoundIoChannelIdClickTrack = 46
    SoundIoChannelIdForeignLanguage = 47
    SoundIoChannelIdHearingImpaired = 48
    SoundIoChannelIdNarration = 49
    SoundIoChannelIdHaptic = 50
    SoundIoChannelIdDialogCentricMix = 51
    SoundIoChannelIdAux = 52
    SoundIoChannelIdAux0 = 53
    SoundIoChannelIdAux1 = 54
    SoundIoChannelIdAux2 = 55
    SoundIoChannelIdAux3 = 56
    SoundIoChannelIdAux4 = 57
    SoundIoChannelIdAux5 = 58
    SoundIoChannelIdAux6 = 59
    SoundIoChannelIdAux7 = 60
    SoundIoChannelIdAux8 = 70
    SoundIoChannelIdAux9 = 71
    SoundIoChannelIdAux10 = 72
    SoundIoChannelIdAux11 = 73
    SoundIoChannelIdAux12 = 74
    SoundIoChannelIdAux13 = 75
    SoundIoChannelIdAux14 = 76
    SoundIoChannelIdAux15 = 77
 
 
@unique
class SoundIoError(Enum):
    SoundIoErrorNone = 0
    SoundIoErrorNoMem = 1
    SoundIoErrorInitAudioBackend = 2
    SoundIoErrorSystemResources = 3
    SoundIoErrorOpeningDevice = 4
    SoundIoErrorNoSuchDevice = 5
    SoundIoErrorInvalid = 6
    SoundIoErrorBackendUnavailable = 7
    SoundIoErrorStreaming = 8
    SoundIoErrorIncompatibleDevice = 9
    SoundIoErrorNoSuchClient = 10
    SoundIoErrorIncompatibleBackend = 11
    SoundIoErrorBackendDisconnected = 12
    SoundIoErrorInterrupted = 13
    SoundIoErrorUnderflow = 14
    SoundIoErrorEncodingString = 15
 
 
@unique
class SoundIoFormat(Enum):
    SoundIoFormatInvalid = 0
    SoundIoFormatS8 = 1
    SoundIoFormatU8 = 2
    SoundIoFormatS16LE = 3
    SoundIoFormatS16BE = 4
    SoundIoFormatU16LE = 5
    SoundIoFormatU16BE = 6
    SoundIoFormatS24LE = 7
    SoundIoFormatS24BE = 8
    SoundIoFormatU24LE = 9
    SoundIoFormatU24BE = 10
    SoundIoFormatS32LE = 11
    SoundIoFormatS32BE = 12
    SoundIoFormatU32LE = 13
    SoundIoFormatU32BE = 14
    SoundIoFormatFloat32LE = 15
    SoundIoFormatFloat32BE = 16
    SoundIoFormatFloat64LE = 17
    SoundIoFormatFloat64BE = 18
 
 
class SoundIoSampleRateRangeStructure(Structure):
    _fields_ = [
        ("min", c_int),
        ("max", c_int)
    ]
 
 
class SoundIoStructure(Structure):
    # 结构体无法传入自己,所以采用后面的形式,全局来初始化对象
    pass
 
 
SoundIoStructure._fields_ = [
    ("userdata", c_void_p),
    ("on_devices_change", CFUNCTYPE(None, POINTER(SoundIoStructure))),
    ("on_backend_disconnect", CFUNCTYPE(None, c_void_p, c_int)),
    ("on_events_signal", CFUNCTYPE(None, c_void_p)),
    ("current_backend", c_int),
    ("app_name", c_char_p),
    ("emit_rtprio_warning", CFUNCTYPE(None)),
    ("jack_info_callback", CFUNCTYPE(None, c_char_p)),
    ("jack_error_callback", CFUNCTYPE(None, c_char_p))
]
 
SOUNDIO_MAX_CHANNELS = 24
 
 
class SoundIoChannelLayoutStructure(Structure):
    _fields_ = [
        ("name", c_char_p),
        ("channel_count", c_int),
        ("channels", c_int * SOUNDIO_MAX_CHANNELS)
    ]
 
 
class SoundIoDeviceStrcuture(Structure):
    """设备信息"""
    _fields_ = [
        ("soundio", POINTER(SoundIoStructure)),
        ("id", c_char_p),
        ("name", c_char_p),
        ("aim", c_int),
        ("layouts", POINTER(SoundIoChannelLayoutStructure)),
        ("layout_count", c_int),
        ("current_layout", SoundIoChannelLayoutStructure),
        ("formats", POINTER(c_int)),
        ("format_count", c_int),
        ("current_format", c_int),
        ("sample_rates", POINTER(SoundIoSampleRateRangeStructure)),
        ("sample_rate_count", c_int),
        ("sample_rate_current", c_int),
        ("software_latency_min", c_double),
        ("software_latency_max", c_double),
        ("software_latency_current", c_double),
        ("is_raw", c_bool),
        ("ref_count", c_int),
        ("probe_error", c_int),
    ]
 
 
class SoundIoOutStreamStructure(Structure):
    """输出流结构体(内含有指针函数,用到了自己,python无法在结构体中传递自己,从上往下执行,因此写在下面)"""
    pass
 
 
# 类变量赋值,防止写在里面报错
SoundIoOutStreamStructure._fields_ = [
    ("devices", POINTER(SoundIoDeviceStrcuture)),
    ("format", c_int),
    ("sample_rate", c_int),
    ("layout", SoundIoChannelLayoutStructure),
    ("software_latency", c_double),
    ("userdata", c_void_p),
    ("write_callback", CFUNCTYPE(None, POINTER(SoundIoOutStreamStructure), c_int, c_int)),
    ("underflow_callback", CFUNCTYPE(None, POINTER(SoundIoOutStreamStructure))),
    ("error_callback", CFUNCTYPE(None, POINTER(SoundIoOutStreamS
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值