public MemoryStream createWavMemory(Bitmap bitmap, string name, bool create_img, bool with_cue) { bitmap = managementGUI.resizeImage(bitmap, _scanWidth, _scanHeight, _myEyeMusic.ResizeMode); Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); System.Drawing.Imaging.BitmapData bmpData = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat); float[,] imArr = ReconstructColor(bmpData); bitmap.UnlockBits(bmpData); if (create_img) { Bitmap clusteredImage = generatePostClassifictionBitmap(imArr); // display image _myEyeMusic.setScanPictureBoxes(clusteredImage); } MemoryStreamEM sonifiedWAV = new MemoryStreamEM(); MemoryStream toREt = sonifiedWAV.Create(_myEyeMusic.OutDirectory + "\\" + name + ".wav", false, 44100, 16); double volume = _myEyeMusic.MyVolume; if (_myEyeMusic.MyCueType.Equals(eyeMusic2.CueType.NoCue)) { volume = 0.0; } if (with_cue) { short sample; WAVFile cueWAVFile = new WAVFile(); // adding cue cueWAVFile.Open(_myEyeMusic._mapFile + "\\Sounds\\" + _myEyeMusic.model.beep_noise, WAVFile.WAVFileMode.READ); for (int j = 0; j < cueWAVFile.NumSamples; j++) { sample = (short)(volume * cueWAVFile.GetNextSample_16bit()); sample = Math.Min(short.MaxValue, sample); sonifiedWAV.AddSample_16bit(sample); } cueWAVFile.Close(); } for (int i = 0; i < _scanWidth; i++) { createMixer(i, sonifiedWAV, imArr); } sonifiedWAV.addLength(); toREt = sonifiedWAV.mFileStream; return(toREt); }
public float[] GetVPFromWave(string wavefile) { string w = Path.GetDirectoryName(wavefile) + @"\n_" + Path.GetFileName(wavefile); Normalize(wavefile, w); WAVFile wave = new WAVFile(); wave.Open(w, WAVFile.WAVFileMode.READ); float[] samples = new float[wave.NumSamples]; for (int i = 0; i < wave.NumSamples; i++) { samples[i] = wave.GetNextSample_16bit(); } int cp = 0; int t = samples.Length / 1024; if (t > 0) { int r = samples.Length % 1024; for (int x = 1; x <= t; x++) { Exocortex.DSP.ComplexF[] complexData = new Exocortex.DSP.ComplexF[1024]; for (int i = cp; i <= 1023; i++) { complexData[i].Re = samples[cp + i]; complexData[i].Im = samples[cp + i] + 5; } Exocortex.DSP.Fourier.FFT(complexData, complexData.Length, FourierDirection.Forward); for (int i = 0; i <= 1023; i++) { samples[cp + i] = complexData[i].Re; } cp += 1024; } } wave.Close(); return(samples); }
public short[] GetSecondaryVPFromWave(string wavefile) { // string w = Path.GetDirectoryName(wavefile) + @"\n_" + Path.GetFileName(wavefile); // Normalize(wavefile, w); WAVFile wave = new WAVFile(); wave.Open(wavefile, WAVFile.WAVFileMode.READ); short[] samples = new short[wave.NumSamples]; for (int i = 0; i < wave.NumSamples; i++) { samples[i] = wave.GetNextSample_16bit(); } int cp = 0; int t = samples.Length / 1024; //if (t > 0) //{ // int r = samples.Length % 1024; // for (int x = 1; x <= t; x++) // { // Exocortex.DSP.ComplexF[] complexData = new Exocortex.DSP.ComplexF[1024]; // for (int i = cp; i <= 1023; i++) // { // complexData[i].Re = samples[cp + i]; // complexData[i].Im = samples[cp + i] + 5; // } // Exocortex.DSP.Fourier.FFT(complexData, complexData.Length, FourierDirection.Forward); // for (int i = 0; i <= 1023; i++) // samples[cp + i] = complexData[i].Re; // cp += 1024; // } //} wave.Close(); return(samples); }
/// <summary> /// loads the samples from WavFilePath to _samplesArray /// </summary> private void loadSamples() { //WaitForm waitForm = new WaitForm(); //ProgressBar pb = new ProgressBar(); //pb.Minimum = 0; //WAVFileMP3 currentWAVFile; WAVFile currentWAVFile; string[] fileNames = Directory.GetFiles(MyFileMap + "\\" + _wavFilePath); currentWAVFile = new WAVFile(); currentWAVFile.Open(fileNames[0], WAVFile.WAVFileMode.READ); //pb.Maximum = fileNames.Length * currentWAVFile.NumSamples; int seconds = (currentWAVFile.NumSamples - FirstSample); currentWAVFile.Close(); //pb.Value = 0; //pb.Location = new Point(40, 80); //waitForm.Show(); //waitForm.Controls.Add(pb); Application.DoEvents(); for (int i = 0; i < fileNames.Length; i++) { // Create the WAVFile object and open the current sample WAV file. currentWAVFile = new WAVFile(); currentWAVFile.Open(fileNames[i], WAVFile.WAVFileMode.READ); // Get the sample from the file and copy it to our array for (int j = 0; j < currentWAVFile.NumSamples; j++) { _samplesArray[i, j] = currentWAVFile.GetNextSample_16bit(); } //pb.Value += currentWAVFile.NumSamples; currentWAVFile.Close(); // Close the WAV file } //waitForm.Close(); }
/// <summary> /// Returns the highest sample value in a WAV file, as a 16-bit value, regardless of /// whether the file contains 8-bit or 16-bit audio. If the sample is coming from /// an 8-bit audio file, the sample will be scaled up from 8-bit to 16-bit. /// </summary> /// <param name="pFilename">The audio file name</param> /// <returns>The highest sample value from the file, as a 16-bit value</returns> public static short HighestSampleValueAs16Bit(String pFilename) { short highestSample = 0; try { WAVFile audioFile = new WAVFile(); if (audioFile.Open(pFilename, WAVFileMode.READ).Length == 0) { if (audioFile.BitsPerSample == 8) { short sample = 0; for (int i = 0; i < audioFile.NumSamples; ++i) { sample = ScaleByteToShort(audioFile.GetNextSample_8bit()); if (sample > highestSample) highestSample = sample; } } else if (audioFile.BitsPerSample == 16) { short sample = 0; for (int i = 0; i < audioFile.NumSamples; ++i) { sample = audioFile.GetNextSample_16bit(); if (sample > highestSample) highestSample = sample; } } audioFile.Close(); } } catch { // log exception } return highestSample; }
/// <summary> /// Returns the highest sample value in a WAV audio file. /// The return value is a byte array and will contain one /// byte if the file contains 8-bit audio or 2 bytes if the file /// contains 16-bit audio. The return value will be null if /// the file cannot be opened. If it is known that the audio /// file contains 16-bit samples, the byte array can be converted /// to a 16-bit integer using BitConverter.ToInt16(). /// </summary> /// <param name="pFilename">The name of the WAV file</param> /// <param name="pBitsPerSample">This will contain the number of bits per sample, or 0 if the file wasn't loaded.</param> /// <returns>A byte array containing the highest audio sample, or null if the file wasn't loaded.</returns> public static byte[] HighestSampleValue(String pFilename, out short pBitsPerSample) { pBitsPerSample = 0; byte[] highestSampleValue = null; WAVFile audioFile = new WAVFile(); try { if (audioFile.Open(pFilename, WAVFileMode.READ).Length == 0) { pBitsPerSample = audioFile.mBitsPerSample; if (audioFile.mBitsPerSample == 8) { byte sample = 0; byte highestSample = 0; for (int i = 0; i < audioFile.NumSamples; ++i) { sample = audioFile.GetNextSample_8bit(); if (sample > highestSample) highestSample = sample; } highestSampleValue = new byte[1]; highestSampleValue[0] = highestSample; } else if (audioFile.mBitsPerSample == 16) { short sample = 0; short highestSample = 0; for (int i = 0; i < audioFile.NumSamples; ++i) { sample = audioFile.GetNextSample_16bit(); if (sample > highestSample) highestSample = sample; } highestSampleValue = BitConverter.GetBytes(highestSample); if (!BitConverter.IsLittleEndian) Array.Reverse(highestSampleValue); } audioFile.Close(); } } catch { // log exception } return (highestSampleValue); }
/// <summary> /// Converts a WAV file's bits/sample and number of channels to a separate WAV file. /// </summary> /// <param name="pSrcFilename">The name of the file to convert</param> /// <param name="pDestFilename">The destination file name</param> /// <param name="pBitsPerSample">The destination's number of bits/sample</param> /// <param name="pStereo">Whether or not the destination should be stereo</param> /// <param name="pVolumeMultiplier">A multiplier that can be used to adjust the volume of the output audio file</param> public static void CopyAndConvert(String pSrcFilename, String pDestFilename, short pBitsPerSample, bool pStereo, double pVolumeMultiplier) { WAVFile srcFile = new WAVFile(); String retval = srcFile.Open(pSrcFilename, WAVFileMode.READ); if (retval.Length > 0) throw new WAVFileException(retval, "WAVFile.Convert_Copy()"); WAVFile destFile = new WAVFile(); destFile.Create(pDestFilename, pStereo, srcFile.SampleRateHz, pBitsPerSample); if ((srcFile.BitsPerSample == 8) && (pBitsPerSample == 8)) { byte sample = 0; if (srcFile.IsStereo && !pStereo) { // 8-bit to 8-bit, stereo to mono: Average each 2 samples while (srcFile.NumSamplesRemaining > 0) { sample = (byte)((short)((short)srcFile.GetNextSample_8bit() + (short)srcFile.GetNextSample_8bit()) / 2); if (pVolumeMultiplier != 1.0) sample = (byte)((double)sample * pVolumeMultiplier); destFile.AddSample_8bit(sample); } } else if ((srcFile.IsStereo && pStereo) || (!srcFile.IsStereo && !pStereo)) { // 8-bit to 8-bit, stereo to stereo or mono to mono while (srcFile.NumSamplesRemaining > 0) { sample = srcFile.GetNextSample_8bit(); if (pVolumeMultiplier != 1.0) sample = (byte)((double)sample * pVolumeMultiplier); destFile.AddSample_8bit(sample); } } else if (!srcFile.IsStereo && pStereo) { // 8-bit to 8-bit, mono to stereo: Write each sample twice while (srcFile.NumSamplesRemaining > 0) { sample = srcFile.GetNextSample_8bit(); if (pVolumeMultiplier != 1.0) sample = (byte)((double)sample * pVolumeMultiplier); destFile.AddSample_8bit(sample); destFile.AddSample_8bit(sample); } } } else if ((srcFile.BitsPerSample == 8) && (pBitsPerSample == 16)) { short sample = 0; if (srcFile.IsStereo && !pStereo) { // 8-bit to 16 bit, stereo to mono: Average each 2 samples while (srcFile.NumSamplesRemaining > 0) { sample = (short)((int)((int)srcFile.GetNextSampleAs16Bit() + (int)srcFile.GetNextSampleAs16Bit()) / 2); if (pVolumeMultiplier != 1.0) sample = (short)((double)sample * pVolumeMultiplier); destFile.AddSample_16bit(sample); } } else if ((srcFile.IsStereo && pStereo) || (!srcFile.IsStereo && !pStereo)) { // 8-bit to 16 bit, stereo to stereo or mono to mono while (srcFile.NumSamplesRemaining > 0) { sample = srcFile.GetNextSampleAs16Bit(); if (pVolumeMultiplier != 1.0) sample = (short)((double)sample * pVolumeMultiplier); destFile.AddSample_16bit(sample); } } else if (!srcFile.IsStereo && pStereo) { // 8-bit to 16 bit, mono to stereo: Write each sample twice while (srcFile.NumSamplesRemaining > 0) { sample = srcFile.GetNextSampleAs16Bit(); if (pVolumeMultiplier != 1.0) sample = (short)((double)sample * pVolumeMultiplier); destFile.AddSample_16bit(sample); destFile.AddSample_16bit(sample); } } } else if ((srcFile.BitsPerSample == 16) && (pBitsPerSample == 8)) { byte sample = 0; if (srcFile.IsStereo && !pStereo) { // 16-bit to 8-bit, stereo to mono: Average each 2 samples short sample_16bit = 0; while (srcFile.NumSamplesRemaining > 0) { sample_16bit = (short)((int)srcFile.GetNextSample_16bit() + (int)srcFile.GetNextSample_16bit() / 2); if (pVolumeMultiplier != 1.0) sample_16bit = (short)((double)sample_16bit * pVolumeMultiplier); sample = ScaleShortToByte(sample_16bit); destFile.AddSample_8bit(sample); } } else if ((srcFile.IsStereo && pStereo) || (!srcFile.IsStereo && !pStereo)) { // 16-bit to 8-bit, stereo to stereo or mono to mono while (srcFile.NumSamplesRemaining > 0) { sample = ScaleShortToByte(srcFile.GetNextSample_16bit()); if (pVolumeMultiplier != 1.0) sample = (byte)((double)sample * pVolumeMultiplier); destFile.AddSample_8bit(sample); } } else if (!srcFile.IsStereo && pStereo) { // 16-bit to 8-bit, mono to stereo: Write each sample twice while (srcFile.NumSamplesRemaining > 0) { sample = ScaleShortToByte(srcFile.GetNextSample_16bit()); if (pVolumeMultiplier != 1.0) sample = (byte)((double)sample * pVolumeMultiplier); destFile.AddSample_8bit(sample); destFile.AddSample_8bit(sample); } } } else if ((srcFile.BitsPerSample == 16) && (pBitsPerSample == 16)) { short sample = 0; if (srcFile.IsStereo && !pStereo) { // 16-bit to 16-bit, stereo to mono: Average each 2 samples while (srcFile.NumSamplesRemaining > 0) { sample = (short)((int)((int)srcFile.GetNextSample_16bit() + (int)srcFile.GetNextSample_16bit()) / 2); if (pVolumeMultiplier != 1.0) sample = (short)((double)sample * pVolumeMultiplier); destFile.AddSample_16bit(sample); } } else if ((srcFile.IsStereo && pStereo) || (!srcFile.IsStereo && !pStereo)) { // 16-bit to 16-bit, stereo to stereo or mono to mono while (srcFile.NumSamplesRemaining > 0) { sample = srcFile.GetNextSample_16bit(); if (pVolumeMultiplier != 1.0) sample = (short)((double)sample * pVolumeMultiplier); destFile.AddSample_16bit(sample); } } else if (!srcFile.IsStereo && pStereo) { // 16-bit to 16-bit, mono to stereo: Write each sample twice while (srcFile.NumSamplesRemaining > 0) { sample = srcFile.GetNextSample_16bit(); if (pVolumeMultiplier != 1.0) sample = (short)((double)sample * pVolumeMultiplier); destFile.AddSample_16bit(sample); destFile.AddSample_16bit(sample); } } } destFile.Close(); srcFile.Close(); }
/// <summary> /// Adjusts the volume level of a WAV file, saving the adjusted file as a separate file. /// </summary> /// <param name="pSrcFilename">The name of the WAV file to adjust</param> /// <param name="pDestFilename">The name to use for the volume-adjusted WAV file</param> /// <param name="pMultiplier">The value by which to multiply the audio samples</param> public static void AdjustVolume_Copy(String pSrcFilename, String pDestFilename, double pMultiplier) { // If an empty source or destination file were passed in, then throw an exception. if (pSrcFilename.Length == 0) throw new WAVFileReadException("Blank filename specified.", "WAVFile.AdjustVolume_Copy()"); if (pDestFilename.Length == 0) throw new WAVFileWriteException("Blank filename specified.", "WAVFile.AdjustVolume_Copy()"); // Open the srouce file WAVFile srcFile = new WAVFile(); String retval = srcFile.Open(pSrcFilename, WAVFileMode.READ); if (retval.Length == 0) { // Check to make sure the input file has a supported number of bits/sample and sample rate. If // not, then throw an exception. if (!SupportedBitsPerSample(srcFile.BitsPerSample)) { WAVFileBitsPerSampleException ex = new WAVFileBitsPerSampleException(pSrcFilename + " has unsupported bits/sample (" + srcFile.BitsPerSample.ToString() + ")", "WAVFile.AdjustVolume_Copy()", srcFile.BitsPerSample); srcFile.Close(); throw ex; } // Open the destination file and start copying the adjusted audio data to it. WAVFile destFile = new WAVFile(); destFile.Create(pDestFilename, srcFile.IsStereo, srcFile.SampleRateHz, srcFile.BitsPerSample); if (srcFile.BitsPerSample == 8) { byte sample = 0; for (int i = 0; i < srcFile.NumSamples; ++i) { // Note: Only multiply the sample if pMultiplier is not 1.0 (if the multiplier is // 1.0, then it would be good to avoid any binary roundoff error). sample = srcFile.GetNextSample_8bit(); if (pMultiplier != 1.0) sample = (byte)((double)sample * pMultiplier); destFile.AddSample_8bit(sample); } } else if (srcFile.BitsPerSample == 16) { short sample = 0; for (int i = 0; i < srcFile.NumSamples; ++i) { // Note: Only multiply the sample if pMultiplier is not 1.0 (if the multiplier is // 1.0, then it would be good to avoid any binary roundoff error). sample = srcFile.GetNextSample_16bit(); if (pMultiplier != 1.0) sample = (short)((double)sample * pMultiplier); destFile.AddSample_16bit(sample); } } srcFile.Close(); destFile.Close(); } else throw new WAVFileReadException(retval, "WAVFile.AdjustVolume_Copy()"); }
/// <summary> /// Changes the volume of a WAV file. /// </summary> /// <param name="pFilename">The name of the WAV file to adjust</param> /// <param name="pMultiplier">The volume multiplier</param> public static void AdjustVolumeInPlace(String pFilename, double pMultiplier) { // If pMultiplier is 1, then we don't need to do anything. if (pMultiplier == 1.0) return; // Open the file WAVFile audioFile = new WAVFile(); String retval = audioFile.Open(pFilename, WAVFileMode.READ_WRITE); if (retval.Length == 0) { // Check to make sure the input file has a supported number of bits/sample and sample rate. If // not, then throw an exception. if (!SupportedBitsPerSample(audioFile.BitsPerSample)) { short bitsPerSample = audioFile.BitsPerSample; audioFile.Close(); throw new WAVFileBitsPerSampleException(pFilename + " has unsupported bits/sample (" + bitsPerSample.ToString() + ")", "WAVFile.AdjustVolumeInPlace()", bitsPerSample); } if (!SupportedSampleRate(audioFile.SampleRateHz)) { int sampleRate = audioFile.SampleRateHz; audioFile.Close(); throw new WAVFileSampleRateException(pFilename + " has unsupported sample rate (" + sampleRate.ToString() + ")", "WAVFile.AdjustVolumeInPlace()", sampleRate); } // Adjust the file volume if (audioFile.BitsPerSample == 8) { byte sample = 0; for (int sampleNum = 0; sampleNum < audioFile.NumSamples; ++sampleNum) { sample = (byte)((double)audioFile.GetNextSample_8bit() * pMultiplier); audioFile.SeekToAudioSample(sampleNum); audioFile.AddSample_8bit(sample); } } else if (audioFile.BitsPerSample == 16) { short sample = 0; for (int sampleNum = 0; sampleNum < audioFile.NumSamples; ++sampleNum) { sample = (short)((double)audioFile.GetNextSample_16bit() * pMultiplier); audioFile.SeekToAudioSample(sampleNum); audioFile.AddSample_16bit(sample); } } audioFile.Close(); } else throw new WAVFileReadException(retval, "WAVFile.AdjustVolumeInPlace()"); }