naudio / NAudio

Audio and MIDI library for .NET
MIT License
5.56k stars 1.1k forks source link

System.FormatException: "Not a WAVE file - no RIFF header" #430

Closed kabanpunk closed 5 years ago

kabanpunk commented 5 years ago

When I use AudioFileReader for WAV file I get this error "System.FormatException: "Not a WAVE file - no RIFF header"". Why?

p.s. error in 120 line reader = new AudioFileReader(file);

using System;
using System.Windows.Forms;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
using VarispeedDemo.SoundTouch;

namespace VarispeedDemo
{
    public partial class MainForm : Form
    {
        private IWavePlayer wavePlayer;
        private VarispeedSampleProvider speedControl;
        private SmbPitchShiftingSampleProvider pitchControl;
        private AudioFileReader reader;

        public MainForm()
        {
            InitializeComponent();
            timer1.Interval = 500;
            timer1.Start();
            Closing += OnMainFormClosing;

            comboBoxModes.Items.Add("Speed");
            comboBoxModes.Items.Add("Tempo");
            comboBoxModes.SelectedIndex = 0;

            EnableControls(false);

        }

        private void OnMainFormClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            wavePlayer?.Dispose();
            speedControl?.Dispose();
            reader?.Dispose();
        }

        private void OnButtonPlayClick(object sender, EventArgs e)
        {
            if (wavePlayer == null)
            {
                wavePlayer = new WaveOutEvent();
                wavePlayer.PlaybackStopped += WavePlayerOnPlaybackStopped;
            }
            if (speedControl == null)
            {
                LoadFile();
                if (speedControl == null) return;
            }

            var resampler = new WdlResamplingSampleProvider(pitchControl, 25000);

            wavePlayer.Init(resampler);

            wavePlayer.Play();
            EnableControls(true);
        }

        private void WavePlayerOnPlaybackStopped(object sender, StoppedEventArgs stoppedEventArgs)
        {
            if (stoppedEventArgs.Exception != null)
            {
                MessageBox.Show(stoppedEventArgs.Exception.Message, "Playback Stopped Unexpectedly");
            }
            EnableControls(false);
        }

        private void EnableControls(bool isPlaying)
        {
            buttonPlay.Enabled = !isPlaying;
            buttonLoad.Enabled = !isPlaying;
            buttonStop.Enabled = isPlaying;
            comboBoxModes.Enabled = !isPlaying;
        }

        private string SelectFile()
        {
            var ofd = new OpenFileDialog();
            ofd.Filter = "ALL Files (*.*)|*.*";
            return ofd.ShowDialog() == DialogResult.OK ? ofd.FileName : null;
        }

        private void OnButtonStopClick(object sender, EventArgs e)
        {
            wavePlayer?.Stop();
        }

        private void OnButtonLoadClick(object sender, EventArgs e)
        {
            LoadFile();
        }

        private void LoadFile()
        {
            reader?.Dispose();
            speedControl?.Dispose();
            reader = null;
            speedControl = null;

            var file = SelectFile();
            if (file == null) return;
            reader = new AudioFileReader(file);
            DisplayPosition();
            trackBarPlaybackPosition.Value = 0;
            trackBarPlaybackPosition.Maximum = (int) (reader.TotalTime.TotalSeconds + 0.5);
            var useTempo = comboBoxModes.SelectedIndex == 1;
            speedControl = new VarispeedSampleProvider(reader, 100, new SoundTouchProfile(useTempo, false));
            pitchControl = new SmbPitchShiftingSampleProvider(speedControl);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (reader != null)
            {
                trackBarPlaybackPosition.Value = (int) reader.CurrentTime.TotalSeconds;
                DisplayPosition();
            }
        }

        private void DisplayPosition()
        {
            labelPosition.Text = reader.CurrentTime.ToString("mm\\:ss");
            if (reader.TotalTime == reader.CurrentTime){
                reader.CurrentTime = TimeSpan.FromSeconds(0);
            }
        }

        private void trackBarPlaybackPosition_Scroll(object sender, EventArgs e)
        {
            if (reader != null)
            {
                reader.CurrentTime = TimeSpan.FromSeconds(trackBarPlaybackPosition.Value);
                speedControl.Reposition();
            }
        }

        private void comboBoxModes_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (speedControl != null)
            {
                var useTempo = comboBoxModes.SelectedIndex == 1;
                speedControl.SetSoundTouchProfile(new SoundTouchProfile(useTempo, false));
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void labelPosition_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            var semitone = Math.Pow(2, 1.0 / 12);
            float f1 = (float)Convert.ToDouble(textBox1.Text);
            float f2 = (float)Convert.ToDouble(textBox2.Text);
            pitchControl.PitchFactor = f2 / f1;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            pitchControl.PitchFactor = 1;
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            try
            {
                float t = (float)Convert.ToDouble(textBox3.Text);
                speedControl.PlaybackRate =  t ;
                labelPlaybackSpeed.Text = $"x{speedControl.PlaybackRate:F2}";
            }
            catch { }

        }
    }

}
kabanpunk commented 5 years ago

sorry the problem was in the audio file