This project has moved. For the latest updates, please go here.

savefile

May 11, 2014 at 7:52 PM
I am trying to save a Wav file... but the program just stops on me ...
  private void saveFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (mixer2 == null)
            {
                MessageBox.Show("there is no information to genarate a file");
                return;
            }

            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "WAV file (*.wav)|*.wav;";
            if (save.ShowDialog() != DialogResult.OK)
            {
                MessageBox.Show("File not written");
                return;

            }
             WaveFileWriter.CreateWaveFile( save.FileName, mixer2);          
        
        }
what am i doing wrong?
May 12, 2014 at 8:17 PM
does anyone know what I am doing wrong?
Coordinator
May 12, 2014 at 8:18 PM
are you getting an error?
May 12, 2014 at 8:22 PM
no, it just stops at the point.. the program freezes up
Coordinator
May 12, 2014 at 8:24 PM
well you probably have a never-ending stream. You'll just fill up your hard disk. I'd recommend only passing something that actually ends in.
Or don't use CreateWaveFile, actually write audio to a WaveFileWriter yourself.
May 12, 2014 at 8:25 PM
I cannot click on anymore buttons
May 12, 2014 at 8:36 PM
i am making my own tones, and mix then with other files...
is that a way to add a timer ? so it will stop.
this is what I am doing now:
{
    public class BinauralBeats : WaveProvider32
    {
        int sample;
        Boolean counter;
       public BinauralBeats()
        {
            RightF = 500;
            LeftF = 410; 
            Amplitude = 0.25f; // let's not hurt our ears  
            counter = true;
        }
       public float RightF { get; set; }
       public float Amplitude { get; set; }
       public float LeftF { get; set; }
       public override int Read(float[] buffer, int offset, int sampleCount)
       {
           int sampleRate = WaveFormat.SampleRate;
           for (int n = 0; n < sampleCount; n++)
           {
               if (counter == true)
               {
                   buffer[n + offset] = (float)(Amplitude * Math.Sin((2 * Math.PI * sample * RightF) / sampleRate));
                   sample++;
                   counter = false; 
                   if (sample >= sampleRate) sample = 0;
               }
               else
               {
                   counter = true;
                   buffer[n + offset] = (float)(Amplitude * Math.Sin((2 * Math.PI * sample * LeftF) / sampleRate));
                   sample++;
                   if (sample >= sampleRate) sample = 0;

               }
           }
           return sampleCount;
          
       }
ok is works if I mex the files without adding a tone... so you are right the tones are not stop ...
how can i add a time for them?
like if I only want them to play for 10s?
May 13, 2014 at 12:14 PM
markheath,

how do i use WaveFileWriter ? I do not see any examples ?
I need to have my tones stop a set time, how do i add that?
May 14, 2014 at 3:08 AM
markheath,

I am trying to save a file of the tone before I mix with the other files but I can seem to do it
this is what i have now

                SaveFileDialog save1 = new SaveFileDialog();
                save1.Filter = "Wave file (*.wav)|*.wav;";
                if(save1.ShowDialog() != DialogResult.OK) return;

                WaveFileWriter wavefile = new WaveFileWriter(save1.FileName, sineWaveProvider.WaveFormat);
                for(int i =0; i< sineWaveProvider.buffer1.Length; i++)
                {
                wavefile.WriteSample(sineWaveProvider.buffer1[i]); 
                }
                wavefile.Flush();
but the file empty
and so is the buffer...

I make the tone before hand in sinewaveProvider
and i can play sinewaveProvider and I can mix it, but I can seem to write in to a file
why?
I made buffer public
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NAudio.Wave;
using NAudio;

namespace Isochronic_tones_generator
{
    public class SineWaveProvider32 : WaveProvider32
{
    int sample;
    double f2;
    public SineWaveProvider32()
    {
        Frequency = 1000;
        Amplitude = 0.25f; // let's not hurt our ears     
        Beat = 0;
    }
 
    public float Frequency { get; set; }
    public float Amplitude { get; set; }
    public float Beat { get; set; }
    public float[] buffer1 { get; set; } 

    public override int Read(float[] buffer, int offset, int sampleCount)
    {
        int sampleRate = WaveFormat.SampleRate;
        if (Beat == 0)
        {
            f2 = 0;
        }
        else
        {
            f2 = Frequency - Beat;
        }
        for (int n = 0; n < sampleCount; n++)
        {
            buffer[n + offset] = (float)(Amplitude * Math.Sin((2 * Math.PI * sample * Frequency) / sampleRate)) + (float)(Amplitude * Math.Sin((2 * Math.PI * sample * f2) / sampleRate));
            sample++;
            if (sample >= sampleRate) sample = 0;
        }
        buffer1 = buffer;
        return sampleCount;
    }
}
}
that should work right ?