public static TimeSpan AudioLength(this Wavefile wave, float level) { if (wave == null) { throw new ArgumentNullException("wave"); } if (level < 0 || level > 1) { throw new ArgumentOutOfRangeException("level", level, " "); } wave.WaitUntilLoaded(); short shLevel = (short)(32767 * level); int lastPoint = 0; int i = 0; foreach (var sample in wave.m_Samples) { if (sample.Left <= -shLevel || sample.Left >= shLevel || sample.Right <= -shLevel || sample.Right >= shLevel) { lastPoint = i; } i++; } return(TimeSpan.FromSeconds(lastPoint / (double)wave.SampleRate)); }
void DoLoading(object obj) { Wavefile wave = m_Wavefile; Mp3Stream stream = (Mp3Stream)obj; BlockArray <Sample> array = wave.m_Samples; //SafeStream safeStream = new SafeStream(stream); Sample[] sample = new Sample[10000]; byte[] buffer = new byte[sample.Length * 4]; Thread.Sleep(1000); while (true) //safeStream.Position < safeStream.Length) { //int count; //int countMax = (int)Math.Min(1024, (safeStream.Length - safeStream.Position)/4); //for (count=0; count <countMax; count ++) //{ // //sample[count] = new Sample(ReadI2(safeStream), ReadI2(safeStream)); // sample[count] = ReadSI2(safeStream); //} int byteCount = stream.Read(buffer, 0, buffer.Length); if (byteCount == 0) { break; } int sampleCount = byteCount / 4; for (int i = 0, j = 0; i < sampleCount; i++, j += 4) { sample[i] = new Sample((short)(buffer[j] | buffer[j + 1] << 8), (short)(buffer[j + 2] | buffer[j + 3] << 8)); } array.Write(sample, 0, sampleCount); Thread.Sleep(0); } stream.Dispose(); wave.FinishedLoading = true; }
public void PlayWavefile(Wavefile wavefile, TimeSpan delay) { if (wavefile == null) { return; } var playing = new NowPlaying { wavefile = wavefile, through = -(int)(delay.TotalSeconds * SampleRate) }; lock (m_lock) { nowPlaying = nowPlaying.AddHead(playing); } }
public static Wavefile Normalise(this Wavefile wave) { if (wave == null) { throw new ArgumentNullException("wave"); } Wavefile newWave = new Wavefile(); newWave.m_SampleRate = wave.SampleRate; newWave.m_Samples = new BlockArray <Sample>(12); ThreadPool.QueueUserWorkItem(new WaitCallback(delegate { wave.WaitUntilLoaded(); int peak = 0; foreach (var sample in wave.m_Samples) { peak = Math.Max(peak, Math.Max(Math.Abs(sample.Left), Math.Abs(sample.Right))); } int factor = 256 * 32767 / peak; Sample[] newSample = new Sample[1]; foreach (var sample in wave.m_Samples) { newSample[0] = new Sample((short)((sample.Left * factor) >> 8), (short)((sample.Right * factor) >> 8)); newWave.m_Samples.Write(newSample); } newWave.FinishedLoading = true; })); //thread.Start(); return(newWave); }
public override Wavefile Load(Stream fileStream) { Mp3Stream mp3Stream = new Mp3Stream(fileStream); if (mp3Stream.Frequency < 0) { mp3Stream.DecodeFrames(1); } if (!(mp3Stream.Frequency > 0 && mp3Stream.ChannelCount > 0)) { throw new InvalidFileFormatException("No frequency/channel information"); } if (mp3Stream.Format != SoundFormat.Pcm16BitStereo) { throw new NotImplementedException("Only stereo MP3 supported"); } Wavefile wave = new Wavefile(); wave.m_SampleRate = mp3Stream.Frequency; wave.m_Samples = new BlockArray <Sample>(12); m_Wavefile = wave; //new DoLoadingDelegate(DoLoading).BeginInvoke(wave, mp3Stream, new AsyncCallback(FinishedLoadingCallback), wave); Thread loadThread = new Thread(new ParameterizedThreadStart(DoLoading)); loadThread.IsBackground = true; loadThread.Start(mp3Stream); //DoLoading(wave, mp3Stream); return(wave); }
public override Wavefile Load(Stream fileStream) { SafeStream safeStream = new SafeStream(fileStream); try { ChunkHeader riffHeader = new ChunkHeader(safeStream); if (!riffHeader.IsType(RiffId)) { throw new InvalidFileFormatException("WAV-file does not begin with `RIFF'."); } SafeStream stream = riffHeader.GetSubStream(); try { FourByteId waveHeader = new FourByteId(stream); if (!waveHeader.Equals(WaveId)) { throw new InvalidFileFormatException("Riff file is not a waveform."); } while (true) //!m_Done) { ChunkHeader chunk = new ChunkHeader(stream); if (chunk.IsType(FormatId)) { using (SafeStream formatStream = chunk.GetSubStream()) { int formatTag = ReadI2(formatStream); if (formatTag != 1) { throw new InvalidFileFormatException("Wavefile is not PCM."); } fmt_Channels = ReadI2(formatStream); //if (fmt_channels != 1) throw "Wavefile must be mono." ; //^^^ Fixed 19.Mar.2001 fmt_SampleRate = ReadI4(formatStream); ReadI4(formatStream); // <-- average bytes per second (rubbish) ReadI2(formatStream); // <-- block align (who knows) fmt_BitsPerSample = ReadI2(formatStream); m_DoneFormat = true; } } else if (chunk.IsType(DataId)) { if (!m_DoneFormat) { throw new InvalidFileFormatException("No format block before data!"); } Wavefile wave = new Wavefile(); wave.m_SampleRate = (float)fmt_SampleRate; switch (fmt_BitsPerSample) { case 8: { if (fmt_Channels == 2) { //read_pcm_8bit_st (f, chunk.len/2, wave) ; //read_pcm = read_pcm_8bit_st ; m_Length = chunk.Length / 2; //start() ; } else { //read_pcm_8bit (f, chunk.len, wave) ; //read_pcm = read_pcm_8bit ; m_Length = chunk.Length; //start() ; } } break; case 16: { if (fmt_Channels == 2) { //read_pcm_16bit_i_st (f, chunk.len/2/2, wave) ; //read_pcm = read_pcm_16bit_i_st ; m_Length = chunk.Length / 2 / 2; //start() ; } else { //read_pcm_16bit_i (f, chunk.len/2, wave) ; //read_pcm = read_pcm_16bit_i ; m_Length = chunk.Length / 2; //start() ; } } break; default: throw new InvalidFileFormatException("Must be 8- or 16-bit."); } ThreadPool.QueueUserWorkItem(delegate { using (SafeStream dataStream = chunk.GetSubStream()) { ReadPcm(wave, dataStream, m_Length, fmt_BitsPerSample, fmt_Channels, true); } //m_Done = true; wave.FinishedLoading = true; }); safeStream = null; stream = null; return(wave); } else { chunk.GetSubStream().Dispose(); Trace.WriteLine(String.Format("Warning: Unknown chunk `{0}'", chunk.TypeId));; } } } finally { if (stream != null) { stream.Dispose(); } } } finally { if (safeStream != null) { safeStream.Dispose(); } } }
protected void ReadPcm(Wavefile wave, SafeStream stream, int len, int bits, int channels, bool intel) { //if (!wave->dontdelete_) sample_free(wave->idata_) ; //wave.m_Samples = new List<Sample>(len); wave.m_Samples = new BlockArray <Sample>(12); //int (*readfun)(Wavefile::Sample *, Stream *, int) ; ReadSample readFunction; if (bits == 16) { if (intel) // Intel byte order { if (channels == 1) { //readfun = f_read_pcm_16bit_i ; readFunction = new ReadSample(ReadPcmI16Mono); } else if (channels == 2) { //readfun = f_read_pcm_16bit_i_st ; readFunction = new ReadSample(ReadPcmI16Stereo); } else { throw new InvalidOperationException(); } } else // Motorola byte order { if (channels == 1) { //readfun = f_read_pcm_16bit_m ; throw new NotImplementedException(); } else if (channels == 2) { //readfun = f_read_pcm_16bit_m_st ; throw new NotImplementedException(); } else { throw new InvalidOperationException(); } } } else if (bits == 8) { if (channels == 1) { //readfun = f_read_pcm_8bit ; throw new NotImplementedException(); } else if (channels == 2) { //readfun = f_read_pcm_8bit_st ; throw new NotImplementedException(); } else { throw new InvalidOperationException(); } } else { throw new InvalidOperationException(); } //int a = 0 ; //while (a<len) { // //int numread = readfun(wave->idata_+a, file, (len-a) <? BufSize) ; // //if (numread==0) break ; // a += numread ; //} Sample[] sample = new Sample[1]; for (int i = 0; i < len; i++) { sample[0] = readFunction(stream); wave.m_Samples.Write(sample); } }
public void PlayWavefile(Wavefile wavefile) { PlayWavefile(wavefile, TimeSpan.Zero); }