LoginSignup
0
0

cscの作法 その396

Last updated at Posted at 2023-07-09

概要

cscの作法、調べてみた。
練習問題、やってみた。

練習問題

directshowlibでマイクをキャプチャしてwmaファイルを作成せよ。

サンプルコード



using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;
using System.Threading;
using DirectShowLib;
using System.ComponentModel;
using System.Data;

namespace Write
{
	internal class Capture: IDisposable {
		private IFilterGraph2 m_FilterGraph = null;
		IMediaControl m_mediaCtrl = null;
		private bool m_bRunning = false;
		DsROTEntry m_rot = null;
		public void Dispose() {
			GC.SuppressFinalize(this);
			CloseInterfaces();
		}
		~Capture() {
			Dispose();
		}
		public Capture(int iDeviceNum, string szOutputFileName) {
			DsDevice [] capDevices;
			capDevices = DsDevice.GetDevicesOfCat(FilterCategory.AudioInputDevice);
			if (iDeviceNum + 1 > capDevices.Length)
			{
				throw new Exception("No video capture devices found at that index!");
			}
			try
			{
				SetupGraph(capDevices[iDeviceNum], szOutputFileName);
				m_bRunning = false;
			}
			catch
			{
				Dispose();
				throw;
			}
		}
		public void Start() {
			if (!m_bRunning)
			{
				int hr = m_mediaCtrl.Run();
				Marshal.ThrowExceptionForHR(hr);
				m_bRunning = true;
			}
		}
		public void Pause() {
			if (m_bRunning)
			{
				IMediaControl mediaCtrl = m_FilterGraph as IMediaControl;
				int hr = mediaCtrl.Pause();
				Marshal.ThrowExceptionForHR(hr);
				m_bRunning = false;
			}
		}
		private void SetupGraph(DsDevice dev, string szOutputFileName) {
			int hr;
			IBaseFilter capFilter = null;
			IBaseFilter asfWriter = null;
			ICaptureGraphBuilder2 capGraph = null;
			m_FilterGraph = (IFilterGraph2) new FilterGraph();
			m_rot = new DsROTEntry(m_FilterGraph);
			try
			{
				capGraph = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();
				hr = capGraph.SetFiltergraph(m_FilterGraph);
				Marshal.ThrowExceptionForHR(hr);
				hr = m_FilterGraph.AddSourceFilterForMoniker(dev.Mon, null, dev.Name, out capFilter);
				Marshal.ThrowExceptionForHR(hr);
				asfWriter = ConfigAsf(capGraph, szOutputFileName);
				hr = capGraph.RenderStream(null, null, capFilter, null, asfWriter);
				Marshal.ThrowExceptionForHR(hr);
				m_mediaCtrl = m_FilterGraph as IMediaControl;
			}
			finally
			{
				if (capFilter != null)
				{
					Marshal.ReleaseComObject(capFilter);
					capFilter = null;
				}
				if (asfWriter != null)
				{
					Marshal.ReleaseComObject(asfWriter);
					asfWriter = null;
				}
				if (capGraph != null)
				{
					Marshal.ReleaseComObject(capGraph);
					capGraph = null;
				}
			}
		}
		private IBaseFilter ConfigAsf(ICaptureGraphBuilder2 capGraph, string szOutputFileName) {
			IFileSinkFilter pTmpSink = null;
			IBaseFilter asfWriter = null;
			int hr = capGraph.SetOutputFileName(MediaSubType.Asf, szOutputFileName, out asfWriter, out pTmpSink);
			Marshal.ThrowExceptionForHR(hr);
			try
			{
				IConfigAsfWriter lConfig = asfWriter as IConfigAsfWriter;
				Guid cat = new Guid("7EA3126D-E1BA-4716-89AF-F65CEE0C0C67");
				hr = lConfig.ConfigureFilterUsingProfileGuid(cat);
				Marshal.ThrowExceptionForHR(hr);
			}
			finally
			{
				Marshal.ReleaseComObject(pTmpSink);
			}
			return asfWriter;
		}
		private void CloseInterfaces() {
			int hr;
			try
			{
				if (m_mediaCtrl != null)
				{
					hr = m_mediaCtrl.Stop();
					m_bRunning = false;
				}
			}
			catch
			{
			}
			if (m_rot != null)
			{
				m_rot.Dispose();
				m_rot = null;
			}
			if (m_FilterGraph != null)
			{
				Marshal.ReleaseComObject(m_FilterGraph);
				m_FilterGraph = null;
			}
		}
	}
	public class Form1: Form {
		private Button button1;
		private TextBox textBox1;
		private Label label1;
		private System.ComponentModel.Container components = null;
		Capture cam = null;
		public Form1() {
			InitializeComponent();
		}
		protected override void Dispose(bool disposing) {
			if (disposing)
			{
				if (cam != null)
				{
					cam.Dispose();
					cam = null;
				}
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent() {
			this.button1 = new Button();
			this.textBox1 = new TextBox();
			this.label1 = new Label();
			this.SuspendLayout();
			this.button1.Location = new Point(88, 80);
			this.button1.Name = "button1";
			this.button1.Size = new Size(75, 48);
			this.button1.TabIndex = 0;
			this.button1.Text = "Start";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			this.textBox1.Location = new Point(16, 40);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new Size(160, 20);
			this.textBox1.TabIndex = 1;
			this.textBox1.Text = "test0.wma";
			this.label1.Location = new Point(16, 24);
			this.label1.Name = "label1";
			this.label1.Size = new Size(56, 16);
			this.label1.TabIndex = 2;
			this.label1.Text = "Output file";
			this.AcceptButton = this.button1;
			this.AutoScaleBaseSize = new Size(5, 13);
			this.ClientSize = new Size(248, 162);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.button1);
			this.FormBorderStyle = FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.Name = "Form1";
			this.Text = "AsfFilter";
			this.ResumeLayout(false);
			this.PerformLayout();
		}
		private void button1_Click(object sender, System.EventArgs e) {
			const int VIDEODEVICE = 0;
			Cursor.Current = Cursors.WaitCursor;
			if (cam == null)
			{
				cam = new Capture(VIDEODEVICE, textBox1.Text);
				cam.Start();
				button1.Text = "Stop";
				textBox1.ReadOnly = true;
			}
			else
			{
				button1.Text = "Start";
				textBox1.ReadOnly = false;
				cam.Pause();
				cam.Dispose();
				cam = null;
			}
			Cursor.Current = Cursors.Default;
		}
		[STAThread]
		static void Main() {
			Application.Run(new Form1());
		}
	}
}







以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0