Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception ignored after changing Audio input source #52

Open
drp0 opened this issue Nov 25, 2021 · 1 comment
Open

Exception ignored after changing Audio input source #52

drp0 opened this issue Nov 25, 2021 · 1 comment

Comments

@drp0
Copy link

drp0 commented Nov 25, 2021

I am writing an Audio oscilloscope using microphone / line/ loopback inputs.
I can switch the inputs using Pycaw.
On the 3rd switch and subsequentlty I get OSError: exception: access violation writing 0x0000000000000001, which is apparently ignored.
According to internet searches:
I am not the 1st to experience this
The error is occuring outside my code as comtypes try to release an object and fails- sending an untrappable warning to the standard out stream.

Exception ignored in: <function _compointer_base.__del__ at 0x000002AFB2F78EE0>
Traceback (most recent call last):
  File "C:\Python\Python39\lib\site-packages\comtypes\__init__.py", line 912, in __del__
    self.Release()
  File "C:\Python\Python39\lib\site-packages\comtypes\__init__.py", line 1166, in Release
    return self.__com_Release()
OSError: exception: access violation writing 0x0000000000000001

I have been unable to trap this error or suppress it. I can reproduce the error across two windows P.C.s.
I have tried:
Many try except conditions
Releasing the mixer volume handle before changing it using volume.release()
comtypes.CoUninitialize()
importing the warnings module and setting ignore warnings

The same error may be referred to here .
I am using:
windows 11
Python 3.9.1
pycaw 20181226
comtypes 1.1.10
cmake 3.18.4.post1 (from python install)
enum34 1.1.10

These are the relevant elements in a much larger program:


    # Compile a list of devices using PyAudio
    PA    = pyaudio.PyAudio()                       # get input devices
    ndev  = PA.get_device_count()
    devArray  = []
    isOk  = []
    n     = 0
    iList = ""
    nMax  = 0
    nMin  = -1
    while n < ndev:
        s = PA.get_device_info_by_index(n)
        #if printSources: print(n, s)
        idx = s['index']
        T = str(idx)
        if idx < 10: T = " " + T
        newval = T + ": " + s['name']
        devArray.append(newval)                     # add to device array
        if s['maxInputChannels'] > 0:               # test for input type
            if idx > nMax: nMax = idx               # reset upper selection limit
            if nMin == -1: nMin = idx               # lowest available input device
            iList += newval + "\n"                  # add to text for prompt
            isOk.append(True)                       # assign as input
        else: isOk.append(False)                    # not an input device
        n = n + 1
    PA.terminate()

    AUDIOdevin = 1
  
    pText  = "Select audio INPUT device:\nPress Cancel for Programs Default\n\n" + iList + "\n\nDefault (" + str(AUDIOdevin)+ ") : "
    retval = SELECTaudiodevice("Input Device", pText, nMin, nMax)
    if retval != None: AUDIOdevin = retval

# audio volume
class MyAudioUtilities(AudioUtilities):
    @staticmethod
    def GetDeviceId(id=None, default=1):
        device_enumerator = comtypes.CoCreateInstance(
            CLSID_MMDeviceEnumerator,
            IMMDeviceEnumerator,
            comtypes.CLSCTX_INPROC_SERVER)
        if id is not None:
            thisDevice = device_enumerator.GetDevice(id)
        else:
            if default == 0:
                # output
                thisDevice = device_enumerator.GetDefaultAudioEndpoint(EDataFlow.eRender.value, ERole.eMultimedia.value)
            else:
                # input
                thisDevice = device_enumerator.GetDefaultAudioEndpoint(EDataFlow.eCapture.value, ERole.eMultimedia.value)
        return thisDevice

def mixerSelect():
    target = devArray[AUDIOdevin]
    P = target.find(": ")                           # strip out id number
    target = target[P + 2:]
    deviceSel = -1
    global volume
    if volume is not None: volume.Release()

    mixer_output = None
    tmp = None
    devicelist = MyAudioUtilities.GetAllDevices()
    i = 0
    for device in devicelist:
        if target in str(device):
            deviceSel = i
            break
        i += 1
    if deviceSel > -1:
        try:
            mixer_output = devicelist[deviceSel]
            print("Selected mixer",mixer_output)
            devices = MyAudioUtilities.GetDeviceId(mixer_output.id)
            interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
            volume = cast(interface, POINTER(IAudioEndpointVolume))

            print("Muted:    ", volume.GetMute())      # set using volume.SetMute(1, None)
            print("Volume:    %s" % show_vol())
            chcount =  volume.GetChannelCount()
            print("Channels:  %s" % chcount)
            if chcount > 1:
                i = 0
                while i < chcount:
                    print("Channel-" + str(i) + ": %s" % int(0.5 + 100 * volume.GetChannelVolumeLevelScalar(i)))
                    i += 1
            # LevelmindB, LevelmaxdB, LevelIncrement dB
            print("Volume Range: (%s, %s, %s)" % volume.GetVolumeRange())       
        except Exception as e:
            print("Mixer volume Error", e)
        print()
    else:
        volume = None

def show_vol():
    return int(0.5 + 100.0 * volume.GetMasterVolumeLevelScalar())

def set_vol(newLevel):
    if newLevel < 0:  newLevel = 0.0
    if newLevel > 100: newLevel = 100.0
    volume.SetMasterVolumeLevelScalar(newLevel / 100.0, None)
    return show_vol()
@KillerBOSS2019
Copy link
Contributor

looks like something else is using that POINTER object. basically it means is that you were trying to access that object even though that object does not exist anymore because of Release()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants