kdschlosser / pyWinCoreAudio

Python Windows Core Audio API
GNU General Public License v2.0
35 stars 9 forks source link

How to use? #3

Open Lizzit opened 2 years ago

Lizzit commented 2 years ago

Hi! I'm a new python programmer and I just discovered this library But I can't find any tutorial or similar to learn how to use it. Can someone help me please?

kdschlosser commented 2 years ago

Oh boy!

How new are we talking?? LOL.

Just to let you know that the library only works with Python 2.7. I have not spent the time to make it 3.6+ compatible. I wanted to let you know that out of the gate.

I never wrote a setup program for it so you have to manually copy the pyWinCoreAudio folder into your {PYTHON_ROOT}/}Libs/site-packages folder.

you will need to have comtypes installed into your python installation

pip install comtypes

you can do a lot with this library. If the script loops and continues to run you can register callbacks for when things change. for example if the default renderer (output endpoint) changes you can get a notification that it has changed, or if the volume changes. you can also use this library to detect when sound is is being output

lets start with the basics before we get into the notifications. The script below shows you how to enumerate (list) all devices and all endpoints for those devices.

from __future__ import print_function
import pyWinCoreAudio

default_render = pyWinCoreAudio.AudioDevices.default_render_endpoint
print('default render device:', default_render.device.name)
print('default render endpoint:', default_render.name)
print('default render endpoint volume:', default_render.volume.master_scalar * 100, '%')

default_capture = pyWinCoreAudio.AudioDevices.default_capture_endpoint
print('default capture device:', default_capture.device.name)
print('default capture endpoint:', default_capture.name)
print('default capture endpoint volume:', default_capture.volume.master_scalar * 100, '%')

for device in pyWinCoreAudio.AudioDevices:
    print('device name:', device.name)
    print('device id:', device.id)
    print('device state:', device.state)
    print('device icon:', device.icon)
    print('endpoints:')
    for endpoint in device:
        print('    name:', endpoint.name)
        print('    id:', endpoint.id)
        print('    guid:', endpoint.guid)
        print('    data_flow:', endpoint.data_flow)
        print('    description:', endpoint.description)
        print('    form_factor:', endpoint.form_factor)
        print('    type:', endpoint.type)
        print('    icon:', endpoint.icon)
        print('    full_range_speakers:', endpoint.full_range_speakers)
        print('    physical_speakers', endpoint.physical_speakers)
        print('    system_effects:', endpoint.system_effects)
        print('    auto gain control:', endpoint.auto_gain_control)
        print('    channel config:', endpoint.channel_config.GetChannelConfig())
        print('    loudness:', endpoint.loudness.GetEnabled())
        print('    is default:', endpoint.is_default)
        print('    jacks:')
        for jack in endpoint.jack_descriptions:
            print('        color:', jack.color)
            print('        channel mapping:', jack.channel_mapping)
            print('        type:', jack.type)
            print('        location:', jack.location)
            print('        port:', jack.port)
            print('        is connected:', jack.is_connected)
            try:
                presence_detection = jack.presence_detection
                print('        presence detection:', presence_detection)
            except AttributeError:
                pass
            try:
                dynamic_format_change = jack.dynamic_format_change
                print('        dynamic format change:', dynamic_format_change)
            except AttributeError:
                pass
            print()
        print()

        print('    jack information:')
        jack_information = endpoint.jack_information

        print('        port id:', jack_information.port_id)
        print('        description:', jack_information.description)
        print('        manufacturer id:', jack_information.manufacturer_id)
        print('        product id:', jack_information.product_id)
        print('        audio latency:', jack_information.audio_latency)
        print('        hdcp capable:', jack_information.hdcp_capable)
        print('        ai capable:', jack_information.ai_capable)
        print('        connection type:', jack_information.connection_type)
        print()

        def print_eq(obj):
            num_chans = obj.GetChannelCount()
            print('        channel count:', num_chans)
            for i in range(num_chans):
                min_level, max_level, stp = obj.GetLevelRange(i)
                level = obj.GetLevel(i)
                print('        channel ' + str(i) + ':')
                print('            min level:', min_level)
                print('            max level:', max_level)
                print('            step:', stp)
                print('            current level:', level)
                print()
            print()

        print('    bass:')
        print_eq(endpoint.bass)

        print('    midrange:')
        print_eq(endpoint.midrange)

        print('    treble:')
        print_eq(endpoint.treble)

        try:
            volume = endpoint.volume
            channel_count = volume.channel_count
            channel_levels = volume.channel_levels
            channel_levels_scalar = volume.channel_levels_scalar
            channel_ranges = volume.channel_ranges

            min_vol, max_vol = volume.range
            step, num_steps = volume.step

            peak_meter = volume.peak_meter

            channel_peak_values = peak_meter.channel_peak_values

            print('    volume:')
            print('        channel count:', channel_count)
            print('        min volume:', min_vol)
            print('        max volume:', max_vol)
            print('        step:', step)
            print('        number of steps:', num_steps)

            print('        master volume:', volume.master)
            print('        master scalar:', volume.master_scalar)
            print('        mute:', volume.mute)
            print('        peak volume:', peak_meter.peak_value)

            for j in range(channel_count):
                min_vol, max_vol, step = channel_ranges[j]

                print('        channel ' + str(j) + ':')
                print('            min volume:', min_vol)
                print('            max volume:', max_vol)
                print('            step:', step)
                print('            level:', channel_levels[j])
                print('            level scalar:', channel_levels_scalar[j])
                print('            peak level:', channel_peak_values[j])
                print()
            print()
        except AttributeError:
            pass

pyWinCoreAudio.stop()
GastonMontiel commented 2 years ago

Hi, i see "only works with Python 2.7". Now work in 3.6+? Thanks! 😁

kdschlosser commented 2 years ago

here is the version that works for Python 3

https://github.com/kdschlosser/pyWinCoreAudio/tree/develop