ciao, ti allego il codice completo.....
ma come ripeto con il button registra e stop e poi faccio genera , non mi da nessun errore. invece mettendolo con il timer mi da quell'errore lì:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
namespace kitt
{
public partial class kitt : Form
{
[DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);
public bool drag = false;
public bool enab = false;
private int opacity = 100;
private int alpha;
private Int32[] wave;
public kitt()
{
InitializeComponent();
}
public Int32[] Wave
{
get
{
return this.wave;
}
set
{
this.wave = value;
this.WaveAverage = (int)Math.Round(wave.Average());
if (this.Parent != null) Parent.Invalidate(this.Bounds, true);
}
}
public int WaveAverage { get; private set; }
private void GenerateSampleWave()
{
Random r = new Random();
wave = new Int32[400];
for (int i = 0; i < wave.Length; i++)
{
wave[i] = r.Next();
}
this.WaveAverage = (int)Math.Round(wave.Average());
}
private void button1_Click(object sender, EventArgs e)
{/*
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\";
openFileDialog1.Filter = "wave files (*.wave)|*.wave|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = false;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
}
*/
Genera_Spettro("c:\record.wav");
mciSendString("play c:\record.wav", null, 0, 0);
mciSendString("stop myDevice", null, 0, 0);
}
private void Genera_Spettro(string filename)
{
FileStream inStream = File.OpenRead(filename);
WaveReader wr = new WaveReader(inStream);
this.Wave = wr.Data[0];
if (this.wave == null)
{
this.GenerateSampleWave();
}
if (this.wave.Length > 0)
{
Bitmap bmp = new Bitmap(pictureBox1.Width - 1, pictureBox1.Height - 1);
Graphics g = Graphics.FromImage(bmp);
Rectangle bounds = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
Color frmColor = this.BackColor;
Brush bckColor;
alpha = (opacity * 255) / 100;
if (drag)
{
Color dragBckColor;
if (BackColor != Color.Transparent)
{
int Rb = BackColor.R * alpha / 255 + frmColor.R * (255 - alpha) / 255;
int Gb = BackColor.G * alpha / 255 + frmColor.G * (255 - alpha) / 255;
int Bb = BackColor.B * alpha / 255 + frmColor.B * (255 - alpha) / 255;
dragBckColor = Color.FromArgb(Rb, Gb, Bb);
}
else dragBckColor = frmColor;
alpha = 255;
bckColor = new SolidBrush(Color.FromArgb(alpha, dragBckColor));
}
else
{
bckColor = new SolidBrush(Color.FromArgb(alpha, this.BackColor));
}
Pen pen = new Pen(this.ForeColor);
if (this.BackColor != Color.Transparent | drag)
{
g.FillRectangle(bckColor, bounds);
}
int middle = bounds.Top + (int)Math.Round((double)bounds.Height / 2d);
double factor = Math.Floor(((double)bounds.Height) / 2d) / ((double)this.wave.Max(i => Math.Abs(i - WaveAverage)));
double dataPerPixel = ((double)this.wave.Length) / ((double)(bounds.Width + 1));
int length = (int)Math.Ceiling(dataPerPixel);
int posA = 0;
if (length == 1)
{
int yA = middle + (int)Math.Round((double)(this.wave[0] - WaveAverage) * factor);
int yB = yA;
for (int x = bounds.Left + 1; x <= bounds.Right; x++)
{
int posB = (int)Math.Floor(((double)(x - bounds.Left)) * dataPerPixel);
if (posB > posA)
{
posA = posB;
yB = middle + (int)Math.Round((double)(this.wave[posB] - WaveAverage) * factor);
}
g.DrawLine(pen, new Point(x - 1, yA), new Point(x, yB));
yA = yB;
}
}
else
{
int yA = middle + (int)Math.Round((double)(this.wave[0] - WaveAverage) * factor);
int yB = yA;
for (int x = bounds.Left + 1; x <= bounds.Right; x++)
{
int posB = (int)Math.Floor(((double)(x - bounds.Left)) * dataPerPixel);
yB = middle + (int)Math.Round((double)(this.wave[posB] - WaveAverage) * factor);
if ((posB - posA) > 1)
{
Int32[] values = new Int32[posB - posA];
Array.Copy(wave, posA + 1, values, 0, posB - posA);
int max = (int)Math.Round((double)(values.Max() - WaveAverage) * factor);
int min = (int)Math.Round((double)(values.Min() - WaveAverage) * factor);
if (max == min)
g.DrawLine(pen, new Point(x - 1, yA), new Point(x, middle + max));
else
g.DrawLine(pen, new Point(x, middle + min), new Point(x, middle + max));
}
else
{
g.DrawLine(pen, new Point(x - 1, yA), new Point(x, yB));
}
posA = posB;
yA = yB;
}
}
pictureBox1.Image = bmp;
pen.Dispose();
bckColor.Dispose();
g.Dispose();
}
}
private void btnSalva_Click(object sender, EventArgs e)
{
pictureBox1.Image.Save("d:\provawave.png", ImageFormat.Png);
}
private void btnRegistra_Click(object sender, EventArgs e)
{
// record from microphone
mciSendString("open new Type waveaudio Alias recsound", "", 0, 0);
mciSendString("record recsound", "", 0, 0);
}
private void btnRegistraStop_Click(object sender, EventArgs e)
{
// stop and save
mciSendString("save recsound c:\record.wav", "", 0, 0);
mciSendString("close recsound ", "", 0, 0);
}
private void timer1_Tick(object sender, EventArgs e)
{
mciSendString("open new Type waveaudio Alias recsound", "", 0, 0);
mciSendString("record recsound", "", 0, 0);
mciSendString("save recsound c:\record.wav", "", 0, 0);
mciSendString("close recsound ", "", 0, 0);
timer1.Stop();
Genera_Spettro("c:\record.wav");
mciSendString("play c:\record.wav", null, 0, 0);
mciSendString("stop myDevice", null, 0, 0);
}
private void kitt_Load(object sender, EventArgs e)
{
// Sets the timer interval to 5 seconds.
timer1.Interval = 5000;
timer1.Start();
}
}
}
ecco l'errore completo:
************** Testo dell'eccezione **************
System.InvalidOperationException: La sequenza non contiene elementi
in System.Linq.Enumerable.Average(IEnumerable`1 source)
in kitt.kitt.set_Wave(Int32[] value) in D:\pc-desktop\luigi\Desktop\kitt\kitt\kitt.cs:riga 38
in kitt.kitt.Genera_Spettro(String filename) in D:\pc-desktop\luigi\Desktop\kitt\kitt\kitt.cs:riga 79
in kitt.kitt.timer1_Tick(Object sender, EventArgs e) in D:\pc-desktop\luigi\Desktop\kitt\kitt\kitt.cs:riga 205
in System.Windows.Forms.Timer.OnTick(EventArgs e)
in System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
in System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Manca una classe perchè non posso metterla nel forum.. per proprietà mia.
Mi dici come risolvo?
Grazie.