###前提・実現したいこと
C#(Windows Forms)を用いて音声入力時の声量を表示する。
###発生している問題・エラーメッセージ
プログラムは正常に動作します。
しかし、Formを閉じると下記のようなエラーが発生してしまいます。
型 'System.ObjectDisposedException' のハンドルされていない例外が System.Windows.Forms.dll で発生しました 追加情報:破棄されたオブジェクトにアクセスできません。
###該当のソースコード
C#
1using NAudio.Wave; 2using OxyPlot; 3using OxyPlot.Axes; 4using OxyPlot.Series; 5using System; 6using System.Collections.Generic; 7using System.Data; 8using System.Drawing; 9using System.Linq; 10using System.Windows.Forms; 11 12namespace sougen 13{ 14 public partial class Form1 : Form 15 { 16 public Form1() 17 { 18 InitializeComponent(); 19 } 20 21 private void Form1_Load(object sender, EventArgs e) 22 { 23 for (int i = 0; i < WaveIn.DeviceCount; i++) 24 { 25 var deviceInfo = WaveIn.GetCapabilities(i); 26 this.label1.Text = String.Format("Device {0}: {1}, {2} channels", 27 i, deviceInfo.ProductName, deviceInfo.Channels); 28 } 29 30 var waveIn = new WaveIn() 31 { 32 DeviceNumber = 0, // Default 33 }; 34 35 waveIn.DataAvailable += WaveIn_DataAvailable; 36 waveIn.WaveFormat = new WaveFormat(sampleRate: 8000, channels: 1); 37 waveIn.StartRecording(); 38 39 InitPlot(); 40 } 41 42 private void WaveIn_DataAvailable(object sender, WaveInEventArgs e) 43 { 44 // 32bitで最大値1.0fにする 45 for (int index = 0; index < e.BytesRecorded; index += 2) 46 { 47 short sample = (short)((e.Buffer[index + 1] << 8) | e.Buffer[index + 0]); 48 49 float sample32 = sample / 32768f; 50 ProcessSample(sample32); 51 52 var g = pictureBox1.CreateGraphics(); 53 54 if (sample32 > 0.05) 55 { 56 g.FillRectangle(Brushes.Red, 70, 130, 30, 20); 57 } 58 if (sample32 > 0.1) 59 { 60 g.FillRectangle(Brushes.LightPink, 60, 100, 50, 20); 61 } 62 if (sample32 > 0.2) 63 { 64 g.FillRectangle(Brushes.LightBlue, 45, 70, 80, 20); 65 } 66 if (sample32 > 0.3) 67 { 68 g.FillRectangle(Brushes.Blue, 30, 40, 110, 20); 69 } 70 71 if (sample32 == 0) 72 { 73 g.FillRectangle(Brushes.LightGray, 70, 130, 30, 20); 74 g.FillRectangle(Brushes.LightGray, 60, 100, 50, 20); 75 g.FillRectangle(Brushes.LightGray, 45, 70, 80, 20); 76 g.FillRectangle(Brushes.LightGray, 30, 40, 110, 20); 77 } 78 79 g.Dispose(); 80 } 81 } 82 83 private PlotModel _plotmodel = new PlotModel(); 84 private LinearAxis _linearaxis1 = new LinearAxis 85 86 { 87 Position = AxisPosition.Bottom 88 }; 89 private LinearAxis _linearaxis2 = new LinearAxis 90 { 91 Minimum = -1.0, 92 Maximum = 1.0, 93 Position = AxisPosition.Left 94 }; 95 private LineSeries _lineSeries = new LineSeries(); 96 List<float> _recorded = new List<float>(); // 音声データ 97 98 private void InitPlot() 99 { 100 _plotmodel.Axes.Add(_linearaxis1); 101 _plotmodel.Axes.Add(_linearaxis2); 102 _plotmodel.Series.Add(_lineSeries); 103 this.plotView1.Model = _plotmodel; 104 } 105 106 private void ProcessSample(float sample) 107 { 108 _recorded.Add(sample); 109 110 if (_recorded.Count == 1024) 111 { 112 var points = _recorded.Select((v, index) => 113 new DataPoint((double)index, v) 114 ).ToList(); 115 _lineSeries.Points.Clear(); 116 _lineSeries.Points.AddRange(points); 117 118 this.plotView1.InvalidatePlot(true); 119 _recorded.Clear(); 120 } 121 } 122 } 123}
###試したこと
g.Dispose()を試して見ましたが、それらしき部分に配置してもエラーが発生してしまいます。
よろしければご教授お願い致します。
###補足情報(言語/FW/ツール等のバージョンなど)
C#(Visual Studio 2015 : Windows Form Application)
NuGetを用いてダウンロード
・NAudio ver1.8.0
・OxyPlot.Corever ver2.0.0-unstable0952
・OxyPlot.windowsForms ver2.0.0-unstable0952
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/10 07:06