{"id":1778,"date":"2017-07-15T22:22:13","date_gmt":"2017-07-15T20:22:13","guid":{"rendered":"https:\/\/alax.info\/blog\/?p=1778"},"modified":"2017-07-13T21:49:48","modified_gmt":"2017-07-13T19:49:48","slug":"reference-signal-source-audio-as-a-media-foundation-source","status":"publish","type":"post","link":"https:\/\/alax.info\/blog\/1778","title":{"rendered":"Reference Signal Source: audio as Media Foundation source"},"content":{"rendered":"<p>Reference signal source for DirectShow in its video part already <a href=\"https:\/\/alax.info\/blog\/1656\">received Media Foundation Source interface earlier<\/a>.<\/p>\n<p>This time, the update implements a separate Media Foundation source for audio. <a href=\"https:\/\/www.alax.info\/trac\/public\/browser\/trunk\/Utilities\/DirectShowReferenceSource\/Sample\/MfGenerate2\/Application.cpp#L73\">MfGenerate2 sample<\/a> code gives an idea on how to initialize the source:<\/p>\n<pre><code>using namespace AlaxInfoDirectShowReferenceSource;\r\nCComPtr&lt;IAudioMediaSource&gt; pSource;\r\n__C(pSource.CoCreateInstance(__uuidof(AudioMediaSource)));\r\n__C(pSource-&gt;SetMediaType(NULL, g_nSampleRate, g_nChannelCount, g_nBitDepth));\r\n__C(pSource-&gt;put_Duration((DOUBLE) g_nDuration));\r\nCComPtr&lt;IMFMediaSource&gt; pAudioMediaSource = pSource;\r\n<\/code><\/pre>\n<p>The source can be given specific format using Media Foundation stream descriptor&#8217;s media type handler, or set up via private COM interface.<\/p>\n<p>The source can be given sampling rate, channel count (all channels receive the same signal) and bit depth for PCM audio formats (8, 16..32), or 32-bit IEEE floating point format.<\/p>\n<p>Video and audio streams can also be combined into aggregate source (video+audio) to produce a multi-track output. The <a href=\"https:\/\/www.alax.info\/trac\/public\/browser\/trunk\/Utilities\/DirectShowReferenceSource\/Sample\/MfGenerate2\/Application.cpp#L73\">MfGenerate2 sample<\/a> shows the approach as well:<\/p>\n<pre><code>__D(pVideoMediaSource || pAudioMediaSource, E_UNNAMED);\r\nif(pVideoMediaSource &amp;&amp; pAudioMediaSource)\r\n{\r\n    CComPtr&lt;IMFCollection&gt; pCollection;\r\n    __C(MFCreateCollection(&amp;pCollection));\r\n    __C(pCollection-&gt;AddElement(pVideoMediaSource));\r\n    __C(pCollection-&gt;AddElement(pAudioMediaSource));\r\n    __C(MFCreateAggregateSource(pCollection, &amp;pMediaSource));\r\n} else\r\n    pMediaSource = pVideoMediaSource ? pVideoMediaSource : pAudioMediaSource;\r\n_A(pMediaSource);<\/code><\/pre>\n<p>The sample project is capable to produce output of this kind:<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/Wmqg7_6v1TY\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<h3>Download links<\/h3>\n<ul>\n<li>Binaries:\n<ul>\n<li>32-bit: <a href=\"https:\/\/www.alax.info\/svn\/public\/trunk\/Utilities\/DirectShowReferenceSource\/_Bin\/DirectShowReferenceSource-Win32.dll\">DirectShowReferenceSource-Win32.dll<\/a><\/li>\n<li>64-bit: <a href=\"https:\/\/www.alax.info\/svn\/public\/trunk\/Utilities\/DirectShowReferenceSource\/_Bin\/DirectShowReferenceSource-x64.dll\">DirectShowReferenceSource-x64.dll<\/a><\/li>\n<li>Sample code: <a href=\"https:\/\/www.alax.info\/svn\/public\/trunk\/Utilities\/DirectShowReferenceSource\/Sample\/\">SVN<\/a>, <a href=\"https:\/\/www.alax.info\/trac\/public\/browser\/trunk\/Utilities\/DirectShowReferenceSource\/Sample\/\">Trac<\/a><\/li>\n<li>License: This software is free to use<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Reference signal source for DirectShow in its video part already received Media Foundation Source interface earlier. This time, the update implements a separate Media Foundation source for audio. MfGenerate2 sample code gives an idea on how to initialize the source: using namespace AlaxInfoDirectShowReferenceSource; CComPtr&lt;IAudioMediaSource&gt; pSource; __C(pSource.CoCreateInstance(__uuidof(AudioMediaSource))); __C(pSource-&gt;SetMediaType(NULL, g_nSampleRate, g_nChannelCount, g_nBitDepth)); __C(pSource-&gt;put_Duration((DOUBLE) g_nDuration)); CComPtr&lt;IMFMediaSource&gt; pAudioMediaSource =&hellip; <\/p>\n<p><a class=\"moretag\" href=\"https:\/\/alax.info\/blog\/1778\">Read the full article<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[485,424,70],"class_list":["post-1778","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-audio","tag-media-foundation","tag-winapi"],"_links":{"self":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/1778","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/comments?post=1778"}],"version-history":[{"count":0,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/1778\/revisions"}],"wp:attachment":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/media?parent=1778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/categories?post=1778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/tags?post=1778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}