public static AudioClip LoadMp3(string targetFile, long SongLength) { // ISSUE: method pointer mp3AudioClip.SongReadLoop = new AudioClip.PCMReaderCallback((object)null, __methodptr(Song_Stream_Loop)); Stream stream = (Stream)File.OpenRead(targetFile); if (mp3AudioClip.readFullyStream != null) { ((Stream)mp3AudioClip.readFullyStream).Dispose(); } mp3AudioClip.readFullyStream = new ReadFullyStream(stream); mp3AudioClip.readFullyStream.stream_CanSeek = (__Null)1; byte[] numArray = new byte[1024]; MpegFile mpegFile = new MpegFile((Stream)mp3AudioClip.readFullyStream, true); mpegFile.ReadSamples(numArray, 0, numArray.Length); mp3AudioClip.playbackDevice = mpegFile; long num1 = SongLength; int num2; if (num1 > (long)int.MaxValue) { Debug.LogWarning((object)"uAudioPlayer - Song size over size on int #4sgh54h45h45"); num2 = int.MaxValue; } else { num2 = (int)num1; } return(AudioClip.Create("uAudio_song", num2, mpegFile.get_WaveFormat().get_Channels(), mpegFile.get_WaveFormat().get_SampleRate(), true, mp3AudioClip.SongReadLoop)); }
public IEnumerator RequestSong(string path, bool userMusic, int temp) { if (userMusic) { if (_audioSource.clip != null && musicController.position >= 4) { Destroy(_audioSource.clip); } musicController.position = temp; var mp3Clip = new MpegFile(path); var clip = AudioClip.Create(path, (int)mp3Clip.Length / sizeof(float) / mp3Clip.Channels, mp3Clip.Channels, mp3Clip.SampleRate, true, data => { mp3Clip.ReadSamples(data, 0, data.Length); }, position => { mp3Clip = new MpegFile(path); }); yield return(clip); clip.name = Path.GetFileNameWithoutExtension(path) ?? throw new Exception(); _audioSource.clip = clip; _musicManager.Play(clip); StopAllCoroutines(); } if (!userMusic) { Debug.Log("Click user--"); //Got click by user var clip = UnityEngine.Resources.Load <AudioClip>( $"Audio/Songs/Menu/{Path.GetFileNameWithoutExtension(musicController.listedMusic[temp])}"); _musicManager.Play(clip); StopAllCoroutines(); } }
/// <summary> /// Initializes a new instance of the <see cref="AudioCuesheetEditor.AudioBackend.SFML.Mp3StreamSFML"/> class. /// </summary> /// <param name="_filename">Full path to the file</param> public Mp3StreamSFML(String _filename) { log.debug("Constructor called with " + _filename); this.mp3file = new MpegFile(_filename); this.Initialize((uint)this.mp3file.Channels, (uint)this.mp3file.SampleRate); this.mutex = new Mutex(); }
public ManagedMpegStream(Stream source, bool closeOnDispose) { this._source = source; this._closeOnDispose = closeOnDispose; this._fileDecoder = new MpegFile(this._source); this._waveFormat = WaveFormat.CreateIeeeFloatWaveFormat(this._fileDecoder.SampleRate, this._fileDecoder.Channels); }
private void DecodeMpeg(MpegFile mpegFile, AudioClip audioClip) { var samplesCount = (int)mpegFile.SampleCount * mpegFile.Channels; var samples = new float[samplesCount]; mpegFile.ReadSamples(samples, 0, samplesCount); audioClip.SetData(samples, 0); }
private async UniTask DecodeMpegAsync(MpegFile mpegFile, AudioClip audioClip) { var samplesCount = (int)mpegFile.SampleCount * mpegFile.Channels; var samples = new float[samplesCount]; await UniTask.Run(() => mpegFile.ReadSamples(samples, 0, samplesCount)); audioClip.SetData(samples, 0); }
public NLayerLoader(string filePath) { filePath = filePath.Replace("file://", ""); this.filePath = filePath; filename = Path.GetFileNameWithoutExtension(filePath); file = new MpegFile(filePath); createdFiles.Add(file); }
public Mp4PspTagger(string inputFile) { mpegFile = new MpegFile(inputFile); foreach (MpegAtom ma in mpegFile.AtomTree) { getPspTags(ma, ref mp4PspAtoms); } }
public Mp3Decoder(Stream s) { _mp3Stream = new MpegFile(s); _audioFormat.Channels = _mp3Stream.Channels; _audioFormat.BitsPerSample = 16; _audioFormat.SampleRate = _mp3Stream.SampleRate; _numSamples = (int)_mp3Stream.Length / sizeof(float); }
public void Mp3ToPcmConversionTest() { var mpegFile = new MpegFile(AudioFiles.Mp3Filename); var samples = new float[mpegFile.SampleRate]; mpegFile.ReadSamples(samples, 0, mpegFile.SampleRate); AssertExtensions.AnyNotZero(samples); Assert.True(mpegFile.Duration.TotalMilliseconds > 0); }
public NLayerSource(Stream stream) { if (stream == null) { throw new ArgumentNullException("stream"); } if (!stream.CanRead) { throw new ArgumentException("Stream is not readable.", "stream"); } _stream = stream; _mpegFile = new MpegFile(stream); _waveFormat = new WaveFormat(_mpegFile.SampleRate, 32, _mpegFile.Channels, AudioEncoding.IeeeFloat); }
public AudioClip LoadAudioClip() { return(AudioClip.Create(filename, (int)(file.Length / sizeof(float) / file.Channels), file.Channels, file.SampleRate, true, data => file.ReadSamples(data, 0, data.Length), position => { var f = new MpegFile(filePath); createdFiles.Add(f); f.Time = TimeSpan.FromSeconds(position * 1.0f / f.SampleRate); file = f; })); }
public static AudioClip LoadMp3(string filePath) { string filename = System.IO.Path.GetFileNameWithoutExtension(filePath); MpegFile mpegFile = new MpegFile(filePath); // assign samples into AudioClip AudioClip ac = AudioClip.Create(filename, (int)(mpegFile.Length / sizeof(float) / mpegFile.Channels), mpegFile.Channels, mpegFile.SampleRate, true, data => { int actualReadCount = mpegFile.ReadSamples(data, 0, data.Length); }, position => { mpegFile = new MpegFile(filePath); }); return(ac); }
public AudioClip Convert(byte[] obj) { var mpegFile = new MpegFile(new MemoryStream(obj)); var audioClip = AudioClip.Create("Generated MP3 Audio", (int)mpegFile.SampleCount, mpegFile.Channels, mpegFile.SampleRate, false); // AudioClip.SetData with offset is not supported on WebGL, thus we can't use buffering while decoding. // Issue: https://trello.com/c/iWL6eBrV/82-webgl-audio-resources-limitation #if UNITY_WEBGL && !UNITY_EDITOR DecodeMpeg(mpegFile, audioClip); #else DecodeMpegBuffered(mpegFile, audioClip); #endif mpegFile.Dispose(); return(audioClip); }
private static AudioClip LoadMp3(string path) { string filename = System.IO.Path.GetFileNameWithoutExtension(path); MpegFile mpegFile = new MpegFile(path); if (mpegFile == null || mpegFile.Length < 1) { Debug.LogWarning($"Failed to load mp3 audio file: {path}"); return(null); } return(AudioClip.Create(filename, (int)(mpegFile.Length / sizeof(float) / mpegFile.Channels), mpegFile.Channels, mpegFile.SampleRate, true, data => OnReadMp3(data, mpegFile), position => OnClipPositionSet(position, mpegFile))); }
public IEnumerator LoadSong(string path, int countOf, List <string> audioList) { int index = 0; if (!musicController.devicemode) { string[] allsongs = Directory.GetFiles(path); bool loaded = false; foreach (string song in allsongs) { if (Path.GetExtension(song) == ".mp3" || Path.GetExtension(song) == ".ogg" || Path.GetExtension(song) == ".wav") { // try // { audioList.Add(song); Debug.Log(song); var mpeg = new MpegFile(song); InitializeSong(song, index, audioList, mpeg.Duration); debug.text = "Not null"; if (debug.gameObject.activeInHierarchy && !loaded) { debug.gameObject.SetActive(false); } index++; // } // catch (Exception ex) // { // debug.text = ex.ToString(); // Debug.Log(ex); // } } else { Debug.LogError("Is not a song " + Path.GetExtension(song)); continue; } musicController.devicemode = true; yield return(new WaitForFixedUpdate()); } StopAllCoroutines(); } }
private void DecodeMpegBuffered(MpegFile mpegFile, AudioClip audioClip) { var bufferLength = mpegFile.SampleRate; var samplesBuffer = new float[bufferLength]; var sampleOffset = 0; while (mpegFile.Position < mpegFile.Length) { var samplesRead = mpegFile.ReadSamples(samplesBuffer, 0, bufferLength); if (samplesRead < bufferLength) { Array.Resize(ref samplesBuffer, samplesRead); } audioClip.SetData(samplesBuffer, (sampleOffset / sizeof(float)) * mpegFile.Channels); if (samplesRead < bufferLength) { break; } sampleOffset += samplesRead; } }
public async UniTask <AudioClip> LoadAsync() { var relativePath = $"song3_{commonResourceProperties.audioResourceName}.mp3"; var fullPath = Path.Combine(Application.streamingAssetsPath, relativePath); var uri = new Uri(fullPath); AudioClip clip; #if UNITY_EDITOR || UNITY_STANDALONE using (var www = new UnityWebRequest(uri)) { www.downloadHandler = new DownloadHandlerBuffer(); await www.SendWebRequest(); var data = www.downloadHandler.data; using (var memoryStream = new MemoryStream(data, false)) { using (var mpeg = new MpegFile(memoryStream)) { var samples = new float[mpeg.Length]; mpeg.ReadSamples(samples, 0, samples.Length); clip = AudioClip.Create(relativePath, samples.Length, mpeg.Channels, mpeg.SampleRate, false); clip.SetData(samples, 0); } } } #else using (var www = new UnityWebRequest(uri)) { www.downloadHandler = new DownloadHandlerAudioClip(uri, AudioType.MPEG); await www.SendWebRequest(); clip = DownloadHandlerAudioClip.GetContent(www); } clip.name = relativePath; #endif return(clip); }
IEnumerator GetAudioClipMP3(string path) { MpegFile mpegFile = new MpegFile(path); float[] samples = new float[mpegFile.Length / sizeof(float)]; mpegFile.ReadSamples(samples, 0, (int)(mpegFile.Length / sizeof(float))); AudioClip audioClip = AudioClip.Create(Path.GetFileNameWithoutExtension(path), (int)(mpegFile.Duration.TotalSeconds * mpegFile.SampleRate), mpegFile.Channels, mpegFile.SampleRate, false); audioClip.SetData(samples, 0); audioSource.clip = audioClip; Texture2D tex = PaintWaveformSpectrum(audioSource.clip, 0.5F, (int)waveformTexture.rectTransform.rect.width, (int)waveformTexture.rectTransform.rect.width, Color.white); waveformTexture.texture = tex; Sprite mySprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f); waveformCurrentTexture.sprite = mySprite; audioSource.Stop(); waveformTexture.color = new Color(waveformTexture.color.r, waveformTexture.color.g, waveformTexture.color.b, 1); waveformCurrentTexture.color = new Color(waveformCurrentTexture.color.r, waveformCurrentTexture.color.g, waveformCurrentTexture.color.b, 1); mpegFile.Dispose(); yield return(null); }
public static AudioClip LoadMp3(Stream stream) { MP3Info info = MP3Helper.ReadMP3Info(stream); string musicName = "DefaultName"; if (!string.IsNullOrEmpty(info.Title)) { musicName = info.Title; } MpegFile mpegFile = new MpegFile(stream); // assign samples into AudioClip AudioClip ac = AudioClip.Create(musicName, (int)(mpegFile.Length / sizeof(float) / mpegFile.Channels), mpegFile.Channels, mpegFile.SampleRate, true, data => { int actualReadCount = mpegFile.ReadSamples(data, 0, data.Length); } //p=> { Debug.Log(p.ToString()); }//position => { mpegFile = new MpegFile(filePath); } ); return(ac); }
// This is used to load audio files that supported by NLayer like mp3 public static AudioClip LoadMp3AudioFile(string path) { float[] audioData = null; int channels = 0; int sampleRate = 0; try { using (MpegFile file = new MpegFile(path)) { //Note: to be simple, we do not load large file with samples count larger than int limit //This will be enough for most file, especially the sound effects in the skin folder if (file.Length > 0x7FFFFFFFL * sizeof(float)) { return(null); } float[] data = new float[file.Length / sizeof(float)]; file.ReadSamples(data, 0, (int)(file.Length / sizeof(float))); channels = file.Channels; sampleRate = file.SampleRate; audioData = data; } } catch { return(null); } if (audioData == null) { return(null); } AudioDataHost dataHost = new AudioDataHost(audioData, channels); AudioClip clip = AudioClip.Create(path, audioData.Length / channels, channels, sampleRate, true, dataHost.PCMReaderCallback, dataHost.PCMSetPositionCallback); return(clip); }
public void Dispose() { createdFiles.ForEach(it => it.Dispose()); file = null; }
private static void OnClipPositionSet(int position, MpegFile mpegFile) { mpegFile.Position = position * sizeof(float) * mpegFile.Channels; }
private static void OnReadMp3(float[] data, MpegFile mpegFile) { mpegFile.ReadSamples(data, 0, data.Length); }