public TrackInfo GetTrackInfo(string path) { MPGImport.mpg123_init(); handle_mpg = MPGImport.mpg123_new(null, errPtr); x = MPGImport.mpg123_open(handle_mpg, path); MPGImport.mpg123_getformat(handle_mpg, out rate, out channels, out encoding); intRate = rate.ToInt32(); intChannels = channels.ToInt32(); intEncoding = encoding.ToInt32(); MPGImport.mpg123_id3(handle_mpg, out id3v1, out id3v2); MPGImport.mpg123_format_none(handle_mpg); MPGImport.mpg123_format(handle_mpg, intRate, intChannels, 208); if (id3v1 != IntPtr.Zero) { MPGImport.mpg123_id3v1 v1 = (MPGImport.mpg123_id3v1)Marshal.PtrToStructure(id3v1, typeof(MPGImport.mpg123_id3v1)); return(new TrackInfo { Album = new String(v1.album), // "Runnin' Wild" Artist = new String(v1.artist), // "Airbourne" Title = new String(v1.title), // "Stand Up For Rock N Roll" Year = new String(v1.year), // "2007" Genre = (v1.genre < GenreText.Length ? GenreText[v1.genre] : string.Empty), // "Rock" Comment = new String(v1.comment), // "Comment" Tag = new String(v1.tag), // "Comment" Rate = intRate, Channels = intChannels, Encoding = intEncoding }); } MPGImport.mpg123_close(handle_mpg); // TODO: ID3v2 support is more complex. return(new TrackInfo()); }
protected void init() { MPGImport.mpg123_init(); handle_mpg = MPGImport.mpg123_new(null, errPtr); errorCheck = MPGImport.mpg123_open(handle_mpg, this.path); MPGImport.mpg123_getformat(handle_mpg, out frequency, out channels, out encoding); _frequency = frequency.ToInt32(); _channels = channels.ToInt32(); _encoding = encoding.ToInt32(); txtRate = _frequency.ToString(); txtChannels = _channels.ToString(); txtEnc = _encoding.ToString(); MPGImport.mpg123_id3(handle_mpg, out id3v1, out id3v2); MPGImport.mpg123_id3v1 MP3Data = new MPGImport.mpg123_id3v1(); MPGImport.mpg123_id3v2 MP3v2Data = new MPGImport.mpg123_id3v2(); // Try getting id3v2 data try { MP3v2Data = (MPGImport.mpg123_id3v2)Marshal.PtrToStructure(id3v2, (Type)typeof(MPGImport.mpg123_id3v2)); } catch (ArgumentNullException e) { Debug.Log("No ID3v2 Data found: " + e.Message); } // ID3v2: Artist try { MPGImport.mpg123_string ptrArtist = (MPGImport.mpg123_string)Marshal.PtrToStructure(MP3v2Data.artist, (Type)typeof(MPGImport.mpg123_string)); txtArtist = ptrArtist.p; } catch (ArgumentNullException e) { Debug.Log("No ID3v2 artist found: " + e.ToString()); } // ID3v2: Title try { MPGImport.mpg123_string ptrTitle = (MPGImport.mpg123_string)Marshal.PtrToStructure(MP3v2Data.title, (Type)typeof(MPGImport.mpg123_string)); txtTitle = ptrTitle.p; } catch (ArgumentNullException e) { Debug.Log("No ID3v2 title found: " + e.ToString()); } // Try getting id3v1 data if not all id3v2 data was found if (txtArtist == "Unknown" || txtTitle == "Unknown") { try { } catch (ArgumentNullException e) { Debug.Log("No ID3v1 Data found: " + e.Message); MP3Data = (MPGImport.mpg123_id3v1)Marshal.PtrToStructure(id3v1, (Type)typeof(MPGImport.mpg123_id3v1)); } } // ID3v1: Artist if (txtArtist == "Unknown") { try { txtArtist = new string(MP3Data.artist); if (txtArtist.Trim().Length < 1) txtArtist = "Unknown"; Debug.Log("ID3v1 Artist Found: " + txtArtist); } catch (ArgumentNullException e) { Debug.Log("No ID3v1 artist: " + e.ToString()); } } // ID3v1: Title if (txtTitle == "Unknown") { try { txtTitle = new string(MP3Data.title); if (txtTitle.Trim().Length < 1) txtTitle = "Unknown"; Debug.Log("ID3v1 Title Found: " + txtTitle); } catch (ArgumentNullException e) { Debug.Log("No ID3v1 title: " + e.ToString()); } } Debug.Log("Final id3 data: " + txtArtist + " - " + txtTitle); MPGImport.mpg123_format_none(handle_mpg); MPGImport.mpg123_format(handle_mpg, _frequency, _channels, _encoding); FrameSize = MPGImport.mpg123_outblock(handle_mpg); len = MPGImport.mpg123_length(handle_mpg); maxVal = (float)short.MaxValue; fChan = (float)_channels; Buffer = new byte[FrameSize]; overFlow = new byte[0]; audioLength = len / (float)_frequency; }
public AudioClip StartImport(string mPath) { MPGImport.mpg123_init(); handle_mpg = MPGImport.mpg123_new(null, errPtr); try { x = MPGImport.mpg123_open(handle_mpg, mPath); MPGImport.mpg123_getformat(handle_mpg, out rate, out channels, out encoding); intRate = rate.ToInt32(); intChannels = channels.ToInt32(); intEncoding = encoding.ToInt32(); MPGImport.mpg123_id3(handle_mpg, out id3v1, out id3v2); MPGImport.mpg123_format_none(handle_mpg); MPGImport.mpg123_format(handle_mpg, intRate, intChannels, 208); string title; if (id3v1 != IntPtr.Zero) { Debug.Log("Getting ID3 info"); MPGImport.mpg123_id3v1 v1 = (MPGImport.mpg123_id3v1)Marshal.PtrToStructure(id3v1, typeof(MPGImport.mpg123_id3v1)); title = new String(v1.title); } else { title = Path.GetFileNameWithoutExtension(mPath); } FrameSize = MPGImport.mpg123_outblock(handle_mpg); byte[] Buffer = new byte[FrameSize]; lengthSamples = MPGImport.mpg123_length(handle_mpg); Debug.Log("Creating audio clip"); myClip = AudioClip.Create(title, lengthSamples, intChannels, intRate, false); int importIndex = 0; while (0 == MPGImport.mpg123_read(handle_mpg, Buffer, FrameSize, out done)) { float[] fArray; fArray = ByteToFloat(Buffer); float offset = (importIndex * fArray.Length) / 2; if (offset > lengthSamples) { Debug.LogWarning("[STED] MP3 file " + mPath + " is of an unexpected length and was truncated."); break; // File was reported as shorter than it is. Salvage what we have and return. } myClip.SetData(fArray, (int)offset); importIndex++; } } catch (Exception ex) { // Attempt to dump any used memory before continuing. // TODO: Still holds onto memory when repeatedy failing. myClip.UnloadAudioData(); myClip = null; throw ex; } finally { MPGImport.mpg123_close(handle_mpg); } return(myClip); }