I have a custom Audio DAC device. It has own controller inside and knows how to work with a certain byte stream. There is a library I'm trying to use: Audio Graph. I generate some data in memory and send it like so:
private unsafe AudioFrame GenerateAudioData(byte[] data)
{
uint bufferSize = 7000000;
AudioFrame frame = new Windows.Media.AudioFrame(bufferSize);
using (AudioBuffer buffer = frame.LockBuffer(AudioBufferAccessMode.Write))
using (IMemoryBufferReference reference = buffer.CreateReference())
{
byte* dataInBytes;
uint capacityInBytes;
((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacityInBytes);
for (int i = 0; i < bufferSize; i += 4)
{
dataInBytes[i] = data[i];
dataInBytes[i + 1] = data[i + 1];
dataInBytes[i + 2] = data[i + 2];
dataInBytes[i + 3] = data[i + 3];
}
}
return frame;
}
Here my audio graph settings:
AudioGraphSettings settings = new AudioGraphSettings(AudioRenderCategory.Other)
{
EncodingProperties = AudioEncodingProperties.CreatePcm(44100, 2, 16),
AudioRenderCategory = AudioRenderCategory.Other,
DesiredRenderDeviceAudioProcessing = AudioProcessing.Raw
};
The problem is that something modify my stream and physical device doesn't recieve exactly the same data. On sound it is not noticeable, but I need to deliver the same bytes to the endpoint device. Using WASAPI I don't have such problem. Also it would be better if I get an exclusive access to my device. It is highly undesirable that the system sounds/alerts/notifications are mixed with my audio stream.
Thanks in advance!
I notice that your audio data use a 44.1 kHz sampling rate. Many audio devices support 48 kHz and this is the sampling rate used as the device's "mix format". In that case, AudioGraph will resample your audio data to match the "mix format" so that your data can be mixed with system sounds and sounds from other apps.
AudioGraph cannot send the data as-is to the audio device because that would require opening the audio device in exclusive mode, which would block any system sounds, would interfere with Cortana, etc. AudioGraph does not support opening the audio device in exclusive mode.
As a possible work-around, if you could force your audio device to support 44.1 kHz only, then this would become the new "mix format" and AudioGraph would not resample the audio. Some audio devices support this through the property page that controls low-level audio settings. Another work-around is to use the IAudioClient API (WASAPI) to open the audio device in exclusive mode.