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

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

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

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

Q&A

2回答

3350閲覧

C#で音声の波形、周波数の表示を行う

okuda____

総合スコア0

C#

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

0グッド

0クリップ

投稿2021/11/10 06:26

編集2023/07/29 17:08

C#で音声を取得し波形の表示と周波数の表示

https://teratail.com/questions/296462?link=qa_related_pc
こちらの質問の回答者のように波形や周波数表示するプログラムを実行できるようにしたいです。

VScodeを使っています。
こちらのコンソールアプリケーションで実行を試みました。
[イメージ説明VScodeで利用しました。]

コピペをして実行しようとしたところエラーメッセージが出て実行できません

発生している問題・エラーメッセージ

下記のようなエラーメッセージが出ます

CS5001 プログラムは、エントリ ポイントに適切な静的'Main'メゾッドを含んでいません

該当のソースコード

C#

1using System; 2using System.Collections.Generic; 3using System.Data; 4using System.Linq; 5using System.Numerics; 6using System.Windows.Forms; 7using MathNet.Numerics; 8using MathNet.Numerics.IntegralTransforms; 9using NAudio.Wave; 10using OxyPlot; 11using OxyPlot.Axes; 12using OxyPlot.Series; 13 14namespace RT_fft 15{ 16 17 public partial class Form1 : Form 18 { 19 private readonly LineSeries _lineSeries1 = new LineSeries(); 20 private readonly LineSeries _lineSeries2 = new LineSeries(); 21 private readonly List<float> _recorded = new List<float>(); 22 private const int deviceNumber = 0; 23 24 public Form1() => InitializeComponent(); 25 26 private void Form1_Load(object sender, EventArgs e) 27 { 28 FormBorderStyle = FormBorderStyle.FixedSingle; 29 30 var info = WaveIn.GetCapabilities(deviceNumber); 31 micname.Text = $"Device {deviceNumber}: {info.ProductName}, {info.Channels} channels"; 32 33 InitPlot(); 34 } 35 36 private void InitPlot() 37 { 38 var model = new PlotModel(); 39 model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, }); 40 model.Axes.Add(new LinearAxis { Minimum = -1.0, Maximum = 1.0, Position = AxisPosition.Left, }); 41 model.Series.Add(_lineSeries1); 42 plotView1.Model = model; 43 44 model = new PlotModel(); 45 model.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, }); 46 model.Axes.Add(new LinearAxis { Minimum = 0, Maximum = 5.0, Position = AxisPosition.Left, }); 47 model.Series.Add(_lineSeries2); 48 plotView2.Model = model; 49 } 50 51 private void recording_Click(object sender, EventArgs e) 52 { 53 var waveIn = new WaveIn 54 { 55 DeviceNumber = deviceNumber, 56 WaveFormat = new WaveFormat(sampleRate: 8000, channels: 1), 57 }; 58 waveIn.DataAvailable += WaveIn_DataAvailable; 59 waveIn.StartRecording(); 60 } 61 62 private void finish_Click(object sender, EventArgs e) => Application.Exit(); 63 64 private void WaveIn_DataAvailable(object sender, WaveInEventArgs e) 65 { 66 for (var index = 0; index < e.BytesRecorded; index += 2) 67 { 68 var sample = (short)((e.Buffer[index + 1] << 8) | e.Buffer[index + 0]); 69 var sample32 = sample / 32768f; 70 ProcessSample(sample32); 71 } 72 } 73 74 private void ProcessSample(float sample) 75 { 76 _recorded.Add(sample); 77 78 if (_recorded.Count == 1024) 79 { 80 // plotView1 81 var points = _recorded.Select((v, i) => new DataPoint(i, v)); 82 _lineSeries1.Points.Clear(); 83 _lineSeries1.Points.AddRange(points); 84 plotView1.InvalidatePlot(true); 85 86 // plotView2 87 var windowsize = _recorded.Count; 88 var window = Window.Hamming(windowsize); 89 var complexData = _recorded.Select((v, i) => v * (float)window[i]) 90 .Select(v => new Complex(v, 0.0)).ToArray(); 91 Fourier.Forward(complexData, FourierOptions.Matlab); 92 93 var s = windowsize * (1.0 / 8000.0); 94 var point = complexData.Take(complexData.Count() / 2) 95 .Select((v, i) => new DataPoint(i / s, Math.Sqrt(v.Real * v.Real + v.Imaginary * v.Imaginary))) 96 .ToList(); 97 98 _lineSeries2.Points.Clear(); 99 _lineSeries2.Points.AddRange(point); 100 plotView2.InvalidatePlot(true); 101 102 // Hz 103 var max = point.Aggregate((r, n) => r.Y > n.Y ? r : n); 104 label1.Text = $"{point.IndexOf(max) / s:f0} Hz"; 105 106 _recorded.Clear(); 107 } 108 } 109 } 110} 111 112

試したこと

初めに上記のサイトの回答者のソースコードをコピペして実行しましたが、上記のようなエラーメッセージが出ました。
Mainメゾッドを含んでいないというエラーメッセージだったので、下記のようにclass program{ ~ }を追加しましたが
同じエラーメッセージができました。

namespace RT_fft { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); } } public partial class Form1 : Form { private readonly LineSeries _lineSeries1 = new LineSeries(); private readonly LineSeries _lineSeries2 = new LineSeries(); private readonly List<float> _recorded = new List<float>(); private const int deviceNumber = 0;

どうしたらエラーなく参考にしたような実行画面が表示できるでしょうか。
対処法を教えていただきたいです。
C#でのプログラミングが初めてのため開発環境に問題がある場合は、そちらも教えていただきたいです
参考にしたプログラム
https://teratail.com/questions/296462?link=qa_related_pc

回答を受けて

VisualStudioでWindowsフォームアプリケーションで実行を試みました。
ですが同じようなエラーメッセージが表示され、さらに追加でエラーメッセージが表示されました。
改善方法を教えていただきたいです。
ソースコードは変更していません。
参考先の方と同じコードにしてあります。
イメージ説明

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

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

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

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

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

退会済みユーザー

退会済みユーザー

2021/11/10 06:45

> こちらのコンソールアプリケーションで実行を試みました。 と言いながら、コードを見ると、 > public partial class Form1 : Form と Windows Forms アプリのもので、やってることがメチャクチャって感じがしますけど。 > C#でのプログラミングが初めてのため開発環境に問題がある場合は、そちらも教えていただきたいです Visual Studio Code は基本エディタです。Visual Studio を使いましょう。
退会済みユーザー

退会済みユーザー

2021/11/10 06:51 編集

VSCodeでC#開発をしてる人の割合は極めて少ないのでWeb上の情報も少ないし、初心者なら尚更やめといたほうが無難でしょう。参考先はWindowsフォームアプリなので、VisualStudioでWindowsフォームアプリケーションでプロジェクト作る所から始めましょう。
退会済みユーザー

退会済みユーザー

2021/11/10 07:27

> 改善方法を教えていただきたいです。 コンソールアプリと Windows Forms アプリ違いと言ったごくごく基本的な知識もお持ちでないように見えますが、そうだとするとここのような文章でやり取りする Q&A サイトではまず話が通じなくて難しいと思います。いきなり FFT を使ってスペクトラム表示なんて到底無理です。まず本を買って勉強するなどして、簡単な Windows Forms アプリを作って、基本的な知識を得てください。
guest

回答2

0

Windowsフォームアプリケーションのフォームは、

[フォーム名.cs]
フォームと、そのコントロールに対するイベント処理、独自処理のC#コードを記述するファイル

[フォーム名.resx]
フォームで使用するリソースやコンポーネント等の情報のファイル

[フォーム名.Designer.cs]
フォームデザイナの設定を元に自動生成されるC#コードのファイル

で構成されており、あなたがコピペしたのは "フォーム名.cs" に当たる部分のコードです。
フォームデザイナでフォームにコントロールを配置すると、"フォーム名.Designer.cs" にコントロールを使うためのコードが追加されて使えるようになりますが、エラーで表示されているのは、そこに存在しないコントロールと思われます。
元の Designer.cs があるなら、それも移植すれば多分使えるようになりますが、なければ自分でフォームデザイナでコントロールを追加する必要があります。

質問のタイトルにやりたい事が書いてありますが、C#を触ったばかりであれば、そこに到達するまでに先に超えるべきハードルがかなり沢山あると思います。こういった質問サイトで、1から10まで全部説明するのは無理なので、まず入門書を購入したり、マイクロソフトのドキュメントを読んだりしてC#やWindowsフォームアプリケーションの基本から勉強し、仕組みを理解したほうが近道になると思います。
C# で Windows フォーム アプリを作成する

投稿2021/11/10 07:55

編集2021/11/10 08:10
退会済みユーザー

退会済みユーザー

総合スコア0

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

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

okuda____

2021/11/10 09:51

回答ありがとうございます。ファイルの説明をしてくださったおかげで、最低限ほしい機能は実装することができました。ありがとうございました。
guest

0

こちらの質問の回答者のように波形や周波数表示するプログラムを実行できるようにしたいです。

言及されたので解説しますが回答の(質問も)コードは、「コンソールアプリケーション」ではありません。
画像の通りGUIアプリケーションです。
その中でもC#では一番古い歴史のある「Windows フォーム アプリケーション」という種類のものです。
Windows Forms - Wikipedia

見分けるポイントは

  • using System.Windows.Forms;
    これがあれば確定
  • public partial class Form1 : Form
    これがあればほぼ確定
  • Form1Form等の文字が出てくる
    9割がたそうだろうと思われる
  • GUIだがなんのヒントもなし
    まずそうだろうと思っていい(選択肢がそれしかなかった時代に書かれたもののことが多い)

名前の通りWindows上で動くGUIアプリケーションです。
okuda____さんがWindows環境をお持ちでなければ、動かすことはできません(Monoで動くのかもしれませんが詳しく知りません)

そのうえで回答コードでは、2つのコードが省略されています。

  • Program.cs
    Visual Studioが自動生成するエントリポイントで通常いじることはない。
  • Form1.Designer.cs
    通常デザイナ画面でマウス操作によりコントロール等を配置するが、それをもとにVisual Studioが自動生成する。
    かなり長くなるため画像から判断してもらったほうが早い。

さらに質問者が前提としている外部ライブラリが必要です(using OxyPlot;等から判断可能)
回答当時使用したライブラリはこの3つです。


省略したコードを上げるのはやぶさかではないですが、私はVisual Studio Codeでの実行方法は全く分かりません。

Visual Studioを入れてもらうのが一番いいと思います(ちょうどVisual Studio 2022も出たとこですし)


Visual Studioもあるんですね^^;
NuGetでライブラリを入れたうえで、画像のようにコントロールを配置してください(位置や大きさはあまり関係ないです)
Label2つ(micnamelabel1
Button1つ(Clickイベントrecording_Click
PlotView2つ(plotView1plotView2

念のため

cs:Form1.Designer.cs

1namespace Questions296462 2{ 3 partial class Form1 4 { 5 /// <summary> 6 /// 必要なデザイナー変数です。 7 /// </summary> 8 private System.ComponentModel.IContainer components = null; 9 10 /// <summary> 11 /// 使用中のリソースをすべてクリーンアップします。 12 /// </summary> 13 /// <param name="disposing">マネージド リソースを破棄する場合は true を指定し、その他の場合は false を指定します。</param> 14 protected override void Dispose(bool disposing) 15 { 16 if(disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region Windows フォーム デザイナーで生成されたコード 24 25 /// <summary> 26 /// デザイナー サポートに必要なメソッドです。このメソッドの内容を 27 /// コード エディターで変更しないでください。 28 /// </summary> 29 private void InitializeComponent() 30 { 31 this.micname = new System.Windows.Forms.Label(); 32 this.plotView1 = new OxyPlot.WindowsForms.PlotView(); 33 this.plotView2 = new OxyPlot.WindowsForms.PlotView(); 34 this.button1 = new System.Windows.Forms.Button(); 35 this.label1 = new System.Windows.Forms.Label(); 36 this.SuspendLayout(); 37 // 38 // micname 39 // 40 this.micname.AutoSize = true; 41 this.micname.Location = new System.Drawing.Point(12, 9); 42 this.micname.Name = "micname"; 43 this.micname.Size = new System.Drawing.Size(35, 12); 44 this.micname.TabIndex = 0; 45 this.micname.Text = "label1"; 46 // 47 // plotView1 48 // 49 this.plotView1.BackColor = System.Drawing.Color.White; 50 this.plotView1.Location = new System.Drawing.Point(14, 67); 51 this.plotView1.Name = "plotView1"; 52 this.plotView1.PanCursor = System.Windows.Forms.Cursors.Hand; 53 this.plotView1.Size = new System.Drawing.Size(316, 161); 54 this.plotView1.TabIndex = 1; 55 this.plotView1.Text = "plotView1"; 56 this.plotView1.ZoomHorizontalCursor = System.Windows.Forms.Cursors.SizeWE; 57 this.plotView1.ZoomRectangleCursor = System.Windows.Forms.Cursors.SizeNWSE; 58 this.plotView1.ZoomVerticalCursor = System.Windows.Forms.Cursors.SizeNS; 59 // 60 // plotView2 61 // 62 this.plotView2.BackColor = System.Drawing.Color.White; 63 this.plotView2.Location = new System.Drawing.Point(14, 243); 64 this.plotView2.Name = "plotView2"; 65 this.plotView2.PanCursor = System.Windows.Forms.Cursors.Hand; 66 this.plotView2.Size = new System.Drawing.Size(316, 161); 67 this.plotView2.TabIndex = 2; 68 this.plotView2.Text = "plotView2"; 69 this.plotView2.ZoomHorizontalCursor = System.Windows.Forms.Cursors.SizeWE; 70 this.plotView2.ZoomRectangleCursor = System.Windows.Forms.Cursors.SizeNWSE; 71 this.plotView2.ZoomVerticalCursor = System.Windows.Forms.Cursors.SizeNS; 72 // 73 // button1 74 // 75 this.button1.Location = new System.Drawing.Point(255, 38); 76 this.button1.Name = "button1"; 77 this.button1.Size = new System.Drawing.Size(75, 23); 78 this.button1.TabIndex = 3; 79 this.button1.Text = "button1"; 80 this.button1.UseVisualStyleBackColor = true; 81 this.button1.Click += new System.EventHandler(this.recording_Click); 82 // 83 // label1 84 // 85 this.label1.AutoSize = true; 86 this.label1.Location = new System.Drawing.Point(14, 49); 87 this.label1.Name = "label1"; 88 this.label1.Size = new System.Drawing.Size(35, 12); 89 this.label1.TabIndex = 4; 90 this.label1.Text = "label1"; 91 // 92 // Form1 93 // 94 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 95 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 96 this.ClientSize = new System.Drawing.Size(342, 414); 97 this.Controls.Add(this.label1); 98 this.Controls.Add(this.button1); 99 this.Controls.Add(this.plotView2); 100 this.Controls.Add(this.plotView1); 101 this.Controls.Add(this.micname); 102 this.Name = "Form1"; 103 this.Text = "Form1"; 104 this.Load += new System.EventHandler(this.Form1_Load); 105 this.ResumeLayout(false); 106 this.PerformLayout(); 107 108 } 109 110 #endregion 111 112 private System.Windows.Forms.Label micname; 113 private OxyPlot.WindowsForms.PlotView plotView1; 114 private OxyPlot.WindowsForms.PlotView plotView2; 115 private System.Windows.Forms.Button button1; 116 private System.Windows.Forms.Label label1; 117 } 118}

投稿2021/11/10 08:37

編集2023/07/29 08:08
TN8001

総合スコア9242

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

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

okuda____

2021/11/10 09:53

とても親切な回答をありがとうございます。周波数の解析と表示は正常に動くことが確認できました。 デバイス名と、波形の表示がいまだできていない状況です。なにが考えらえる原因はありますでしょうか
TN8001

2021/11/10 10:15

> 周波数の解析と表示は正常に動くことが確認できました。 label1.Textには何かHzが出ているという意味ですか? であれば処理の大半は通っているはずですが。 > デバイス名と、波形の表示がいまだできていない状況です。 micname.Textに名前が出ないという意味ですか? 「Device 0: , channels」のような plotView1・plotView2ともになにも線が出ないですか? > なにが考えらえる原因はありますでしょうか deviceNumberが0決め打ちなので、入力ソースが複数あると思ったものではないかもしれません。 あるいは波形にならないくらい音が小さいとか??
okuda____

2021/11/10 10:34

>label1.Textには何かHzが出ているという意味ですか? その通りです。Hzの表示はうまくいきました。ありがとうございます。 >micname.Textに名前が出ないという意味ですか? 「Device 0: , channels」のような表示はされず、「label1」と表示されています。 >plotView1・plotView2ともになにも線が出ないですか? はい波形の表示がされません。また波形を表示する際の縦軸や横軸などの表示もありません。これは自分でどうにかして設定するものなのでしょうか。 >あるいは波形にならないくらい音が小さいとか?? もしかするとその可能性はあるかもしれません。 次回検証します。
TN8001

2021/11/10 10:56

わかりました。 button1.Clickと同じように、Form1のLoadイベントにForm1_Loadを付けてください。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問