public void ReceiveInfo(BASS_SAMPLE localInfo) { frequency = localInfo.freq; channels = localInfo.chans; Debug.Log("freq = " + frequency + "| channels = " + channels); Draw(); }
internal static long FindSilence(int sample) { if (sample == -1 || sample == 0) { return(0); } long count; if (SilenceCache.TryGetValue(sample, out count)) { return(count); } int threshold = 500; BASS_SAMPLE bs = Bass.BASS_SampleGetInfo(sample); if (bs == null) { return(0); } short[] buffer = new short[bs.length]; Bass.BASS_SampleGetData(sample, buffer); int b = buffer.Length; int a; b = b / 2; // bytes -> samples (16-bit!) for (a = 0; a < b; a++) { // count silent samples if (Math.Abs(buffer[a]) >= threshold) { break; } } // now 'a' points to the sample position count += a * 2; // since 'a' was the 16-bit index, this is now the BYTE offset! if (a < b) { // sound has begun! // move back to a quieter sample (to avoid "click") while (a > 0 && Math.Abs(buffer[a]) >= threshold / 4) { a--; count -= 2; } } SilenceCache[sample] = count; return(count); }
/// <summary> /// Initializes a <see cref="BASSSoundEffect"/> instance from the specified data. /// </summary> private static void InitializeSampleData(Byte[] fileData, out UInt32 sample, out BASS_SAMPLE sampleInfo, out IntPtr sampleData) { sample = BASS_SampleLoad(fileData, 0, (UInt32)fileData.Length, UInt16.MaxValue, 0); if (!BASSUtil.IsValidHandle(sample)) { throw new BASSException(); } if (!BASS_SampleGetInfo(sample, out sampleInfo)) { throw new BASSException(); } sampleData = Marshal.AllocHGlobal((Int32)sampleInfo.length); if (!BASS_SampleGetData(sample, sampleData)) { throw new BASSException(); } }
/// <summary> /// Gets the sound effect's sample information. /// </summary> /// <param name="data">The sound effect's raw PCM sample data.</param> /// <param name="info">The sound effect's sample info.</param> /// <returns>The handle to the sound effect's BASS sample.</returns> public UInt32 GetSampleInfo(out IntPtr data, out BASS_SAMPLE info) { data = this.sampleData; info = this.sampleInfo; return(sample); }
/// <summary> /// Imports an mp3 file. Only the start of a file is actually imported. /// The remaining part of the file will be imported bit by bit to speed things up. /// </summary> /// <returns> /// Audioclip containing the song. /// </returns> /// <param name='filePath'> /// Path to mp3 file. /// </param> /// public AudioClip ImportFile(string filePath) { //get license from http://bass.radio42.com/bass_purchase.html //Un4seen.Bass.BassNet.Registration ("email", "key"); if (Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero)) { sample = Bass.BASS_SampleLoad(filePath, 0, 0, 1, BASSFlag.BASS_SAMPLE_FLOAT); BASS_SAMPLE info = Bass.BASS_SampleGetInfo(sample); int lengthSamples = (int)(info.length / sizeof(float)); audioClip = AudioClip.Create(Path.GetFileNameWithoutExtension(filePath), lengthSamples / info.chans, info.chans, info.freq, false, false); float[] data = new float[lengthSamples]; Bass.BASS_SampleGetData(sample, data); //Debug.Log("Sample Length: " + lengthSamples); TAG_INFO tagInfo = new TAG_INFO(filePath); //SendMessage("ReceiveTagInfo", tagInfo, SendMessageOptions.RequireReceiver); //SendMessage("ReceiveSamples", data, SendMessageOptions.RequireReceiver); //SendMessage("ReceiveInfo", info, SendMessageOptions.RequireReceiver); GlobalData.samples = data; GlobalData.tagInfo = tagInfo; GlobalData.sinfo = info; audioClip.SetData(data, 0); /*-------------------------------------!-!-!-!------------------------------------------*/ Debug.Log("Data Length: " + data.Length); string tempS = " "; //for (int i = 0; i < data.Length; i += 2048) //{ // tempS += data[i].ToString() + Environment.NewLine; //} //System.IO.File.WriteAllText(@"C:\Users\Rafatdin\Desktop\Bass\Samples.txt", tempS); float[] arrayOfAverages = new float[data.Length / 88200 + 1]; for (int i = 1; i < data.Length / 88200; i++) { float average = 0; for (int ii = i * 88200; ii < i * 88200 + 88200; ii++) { average += data[ii]; } average /= 44100; arrayOfAverages[i] = average * 10; tempS += average.ToString() + Environment.NewLine; } GlobalData.processedSamples = arrayOfAverages; //System.IO.File.WriteAllText(@"C:\Users\Rafatdin\Desktop\Bass\Samples.txt", tempS); /*-------------------------------------!-!-!-!------------------------------------------*/ // free the Sample Bass.BASS_SampleFree(sample); // free BASS Bass.BASS_Free(); Application.LoadLevel("3LaneWay"); } else { Debug.Log(Bass.BASS_ErrorGetCode()); if (Bass.BASS_ErrorGetCode().ToString().Equals("BASS_ERROR_ALREADY")) { Bass.BASS_Free(); ImportFile(filePath); } } return(audioClip); }
/// <summary> /// Gets the sound effect's sample data. /// </summary> /// <param name="data">The sound effect's raw PCM sample data.</param> /// <param name="info">The sound effect's sample info.</param> /// <returns>The handle to the sound effect's BASS sample.</returns> internal UInt32 GetSampleData(out IntPtr data, out BASS_SAMPLE info) { data = this.data; info = this.sampleInfo; return(sample); }
public void ReceiveInfo(BASS_SAMPLE localInfo) { frequency = localInfo.freq; channels = localInfo.chans; GlobalData.sinfo = localInfo; }
public bool SampleSetInfo(int hand, BASS_SAMPLE bSample) { return(Native.NativeMethods.BASS_SampleSetInfo(hand, bSample)); }