質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Windows Forms

Windows Forms(WinForms)はMicrosoft .NET フレームワークに含まれる視覚的なアプリケーションのプログラミングインターフェイス(API)です。WinFormsは管理されているコードの既存のWindowsのAPIをラップすることで元のMicrosoft Windowsのインターフェイスのエレメントにアクセスすることができます。

Q&A

解決済

1回答

2794閲覧

音声入力による声量の表示

wasteir

総合スコア13

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Windows Forms

Windows Forms(WinForms)はMicrosoft .NET フレームワークに含まれる視覚的なアプリケーションのプログラミングインターフェイス(API)です。WinFormsは管理されているコードの既存のWindowsのAPIをラップすることで元のMicrosoft Windowsのインターフェイスのエレメントにアクセスすることができます。

0グッド

0クリップ

投稿2017/05/10 06:22

###前提・実現したいこと
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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

FormがどうなっていようがWaveInがデータ取り続けているので

  1. フォーム閉じる
  2. pictureBox1がDispose
  3. WaveIn.DataAvailableが発火
  4. DisposeしたpictureBox1にアクセスして例外

というわけで、フォームを閉じる前にWaveInを止める必要があります。(StopRecording)

投稿2017/05/10 06:40

ozwk

総合スコア13521

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

wasteir

2017/05/10 07:06

ozwkさん FormClosingにwaveIn.StopRecording()を入れたらエラーが発生しなくなりました。 迅速な回答ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問