I’m using this code to get the name of various audio devices:
//id is the device ID
Var c As new CoreAudioMBS
Var m As MemoryBlock=c.AudioDeviceGetPropertyMemory(id,0,false,"name")
Var DeviceName As String
if m<>nil then deviceName=m.CString(0).DefineEncoding(Encodings.UTF8)
When the device name contains characters higher than asc(127), like in my french-localised OS (“Écouteurs externes”), the result is truncated:
And the content of the MemoryBlock indeed isn’t complete:
Well, the problem is about the length, even in the memory block. Notice “externes” normally ends with “s”, but that “s” doesn’t appear even in the memory block.
I don’t think it’s an issue about DefineEncoding.
Dim c As New CoreAudioMBS
Dim DefaultInputDeviceMem As MemoryBlock = c.AudioHardwareGetPropertyMemory(c.kAudioHardwarePropertyDefaultInputDevice)
Dim DefaultInputDeviceID As Integer = DefaultInputDeviceMem.Long(0)
Dim DefaultInputDeviceName As String = c.AudioDeviceGetPropertyString(DefaultInputDeviceID, 0, True, c.kAudioDevicePropertyDeviceName)
MessageBox DefaultInputDeviceName
Dim DefaultOutputDeviceMem As MemoryBlock = c.AudioHardwareGetPropertyMemory(c.kAudioHardwarePropertyDefaultOutputDevice)
Dim DefaultOutputDeviceID As Integer = DefaultOutputDeviceMem.Long(0)
Dim DefaultOutputDeviceName As String = c.AudioDeviceGetPropertyString(DefaultOutputDeviceID, 0, True, c.kAudioDevicePropertyDeviceName)
MessageBox DefaultOutputDeviceName
Break
I’m seeing a big improvement: c.AudioDeviceGetPropertyString now works (wouldn’t in previous plugin). Can you also fix AudioDeviceGetPropertyMemory, still broken, that I’m currently using, please?
Also, note sure whether it’s intended, but I’m getting a trailing chr(0) in the string version.
In C the trailing C is normal and you better use newer CF Properties if possible:
Dim c As New CoreAudioMBS
Dim DefaultInputDeviceMem As MemoryBlock = c.AudioHardwareGetPropertyMemory(c.kAudioHardwarePropertyDefaultInputDevice)
Dim DefaultInputDeviceID As Integer = DefaultInputDeviceMem.Long(0)
Dim DefaultInputDeviceCFName As CFStringMBS = c.AudioDeviceGetPropertyCFString(DefaultInputDeviceID, 0, True, c.kAudioDevicePropertyDeviceNameCFString)
Dim DefaultInputDeviceName As String = DefaultInputDeviceCFName.Str
MessageBox DefaultInputDeviceName
and the AudioDeviceGetPropertyMemory works here:
Dim c As New CoreAudioMBS
Dim DefaultOutputDeviceMem As MemoryBlock = c.AudioHardwareGetPropertyMemory(c.kAudioHardwarePropertyDefaultOutputDevice)
Dim DefaultOutputDeviceID As Integer = DefaultOutputDeviceMem.Long(0)
Dim mem1 As MemoryBlock = c.AudioDeviceGetPropertyMemory(DefaultOutputDeviceID, 0, False, c.kAudioDevicePropertyVolumeDecibels)
Dim VolumeDecibels As Single = mem1.SingleValue(0)
Dim mem2 As MemoryBlock = c.AudioDeviceGetPropertyMemory(DefaultOutputDeviceID, 0, False, c.kAudioDevicePropertyVolumeScalar)
Dim VolumeScalar As Single = mem2.SingleValue(0)
Break
PS: On the way, I added 20 new example snippets to the documentation.
I was aware of the trailing 0 in C, but was not sure about what CoreFoundation provides (whether it would be different than the MemoryBlock way). Thanks for telling me.
In your examples, yes. For getting the name (or other strings), it’s truncated:
Var DeviceName As String=c.AudioDeviceGetPropertyString(id,0,true,c.kAudioDevicePropertyDeviceName).DefineEncoding(Encodings.UTF8).ReplaceAll(Encodings.UTF8.Chr(0),"")
(I know I could use the String version, but for whatever reason (which I don’t remember right now), I’m using the MemoryBlock way at most places (I believe it’s because I’m storing raw values somewhere). I was just reporting that the MemoryBlock way misses one character).