kdschlosser / pyWinCoreAudio

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

How to Access WAVEFORMATEX? #1

Open cjunekim opened 5 years ago

cjunekim commented 5 years ago

Hello

I want to read the sample rate and bit depth for the current audio endpoint. It seems like the structure is called WAVEFORMATEX and it is in the library, but not utilized. How do I get this?

cjunekim commented 5 years ago

I could get the sample rate via,

>>> audiodevice.render_endpoints[0].audio_client.GetMixFormat().contents.nSamplesPerSec
48000

However, the bit depth is strange:

>>> audiodevice.render_endpoints[0].audio_client.GetMixFormat().contents.wBitsPerSample
5

The bit depth should be multiples of 8. The device's bit depth was set as 24 bit. However, wBitsPerSample value was 5.

kdschlosser commented 5 years ago

you may want to cast the pointer that is returned by GetMixFormat

so here would be an example.


import ctypes
from pyWinCoreAudio.__core_audio.audioclient import WAVEFORMATEEX

waveformatex = ctypes.cast(audiodevice.render_endpoints[0].audio_client.GetMixFormat(), ctypes.POINTER(WAVEFORMATEEX))

# I do not remember off hand which way you are going to be able to access the information
waveformatex.contents.wBitsPerSample

# or this
waveformatex.wBitsPerSample

You can give that a try.

Now I have not messed with this library in some time. but if memory serves the returned waveformatex structure is going to be the audio stream data. I would have to double check that.

The easiest way to be 100% sure is if you use Media Player to play an MP3 that you know the bitrate for (this can be found out by checking in the Windows Explorer (File Explorer) and look at the metadata for the MP3. and check to see if the returned value from GeetMixFormat is the same.

I would have to check and see if Microsoft used some kind of funky math for the bitarte they may have.

kdschlosser commented 5 years ago

ok never mind. I just realized you are trying to get the bit depth not the bit rate. what kind of an audio format are you playing???

if is is not a lossless audio format you will get an incorrect value. during the encoding process alot of the time the bit depth data gets removed. see the link below.

https://stackoverflow.com/questions/27652123/how-to-know-the-bit-depth-of-a-mp3-file

also on Microsoft's API white papers it states

The GetMixFormat method retrieves the stream format that the audio engine uses for its internal processing of shared-mode streams.

the keyword in that is stream. not device. https://docs.microsoft.com/en-us/windows/desktop/api/audioclient/nf-audioclient-iaudioclient-getmixformat