void alphabetizeByAlbum() { GameObject[] audioHolders = new GameObject[10000]; //temporary placeholder max size print (musicFiles.Count); foreach(FileInfo f in musicFiles) { //print("Loading "+f.FullName); GameObject audioHolder = new GameObject(f.Name); audioHolders[songIndex] = audioHolder; songIndex++; ID3v1 tagger = new ID3v1(); FileStream mp3Stream = new FileStream(f.FullName, FileMode.Open); Mp3File currentMP3 = new Mp3File(f); currentMP3.Update(); tagger.Deserialize(mp3Stream); mp3Stream.Close(); string album = tagger.Album; //print ("album: " + album); for (int i = 0; i < alphabeticalTitles.Length; i++) { string firstChar = alphabeticalTitles.GetValue(i).ToString().Substring(5,1); if (album.StartsWith(firstChar)) { audioHolder.transform.parent = alphabeticalTitles[i].transform; } } } return; }
/// <summary> /// Construct from file info; parse ID3 tags from stream and calculate where the audio must be /// </summary> /// <param name="fileinfo"></param> public Mp3FileData(FileInfo fileinfo) { _sourceFileInfo = fileinfo; // create an empty frame model, to use if we don't parse anything better TagModel tagModel = new TagModel(); // don't know how big the audio is until we've parsed the tags UInt32 audioNumBytes; using( FileStream sourceStream = fileinfo.Open( FileMode.Open, FileAccess.Read, FileShare.Read ) ) { // all the header calculations use UInt32; // this guarantees all the file offsets we have to deal with fit in a UInt32 if( sourceStream.Length > UInt32.MaxValue ) throw new InvalidAudioFrameException( "MP3 file can't be bigger than 4gb" ); // in the absence of any recognised tags, // audio starts at the start _audioStart = 0; // audio is entire file length audioNumBytes = (UInt32)sourceStream.Length; // try to read an ID3v1 block. // If ID3v2 block exists, its values overwrite these // Otherwise, if ID3V1 block exists, its values are used // The audio is anything that's left after all the tags are excluded. try { ID3v1 id3v1 = new ID3v1(); id3v1.Deserialize( sourceStream ); // fill in ID3v2 block from the ID3v1 data tagModel = id3v1.FrameModel; // audio is shorter by the length of the id3v1 tag audioNumBytes -= ID3v1.TagLength; } catch( TagNotFoundException ) { // ignore "no ID3v1 block" // everything else isn't caught here, and throws out to the caller } try { sourceStream.Seek( 0, SeekOrigin.Begin ); tagModel = TagManager.Deserialize( sourceStream ); // audio starts after the tag _audioStart = (uint)sourceStream.Position; // audio is shorter by the length of the id3v2 tag audioNumBytes -= _audioStart; } catch( TagNotFoundException ) { // ignore "no ID3v2 block" // everything else isn't caught here, and throws out to the caller } // create a taghandler to hold the tagmodel we've parsed, if any _tagHandler = new TagHandler( tagModel ); } // closes sourceStream // save the location of the audio in the original file // passing in audio size and id3 length tag (if any) to help with bitrate calculations _audio = new AudioFile( fileinfo, _audioStart, audioNumBytes, _tagHandler.Length ); _audioReplaced = false; }
// Update is called once per frame void Update() { if (boxIsMoving) { moveBox(interactable.transform.parent.gameObject, boxEndPosition); } if (folderIsMoving && !foldersOut) { int folderIndex = 0; foreach (Transform artistFolder in artistFolderContainer.transform) { moveFolder(artistFolder.gameObject, folderEndPositions[folderIndex]); folderIndex++; } } if (folderIsMoving && foldersOut) { int folderIndex = 0; foreach (Transform artistFolder in artistFolderContainer.transform) { moveFolder(artistFolder.gameObject, folderOriginalPositions[folderIndex]); folderIndex++; } } if (Input.GetKeyDown ("x") && !boxIsMoving && waitingForArtistSelection && !artistSelected) { boxIsMoving = true; boxEndPosition = boxOriginalPosition; foreach (Transform artistFolder in artistFolderContainer.transform) { Destroy(artistFolder.gameObject); } waitingForAlbumSelection = false; selectedFolder = 0; } if (interactable.transform.parent.transform.localPosition == boxOriginalPosition && waitingForArtistSelection) { raycaster.enabled = true; waitingForArtistSelection = false; boxIsMoving = false; boxIsOpen = false; } /*Artist selection*/ try { if (foldersOut) { artistFolderContainer.transform.GetChild(selectedFolder).GetChild(2).GetComponent<MeshRenderer>().material.color = ToColor(16761477); selectedArtist = artistFolderContainer.transform.GetChild(selectedFolder).transform; if (Input.GetKeyDown ("s") && selectedFolder+1 < artistFolderContainer.transform.childCount && !waitingForAlbumSelection) { artistFolderContainer.transform.GetChild(selectedFolder).GetChild(2).GetComponent<MeshRenderer>().material.color = folderDefaultColor; selectedFolder++; } if (Input.GetKeyDown ("w") && selectedFolder-1 >= 0) { artistFolderContainer.transform.GetChild(selectedFolder).GetChild(2).GetComponent<MeshRenderer>().material.color = folderDefaultColor; selectedFolder--; } if (Input.GetKeyDown ("f") && !waitingForAlbumSelection) { //Pull folders out of view, make appropriate albums visible, wait for album selection folderIsMoving = true; foldersLifted = false; foldersOut = false; artistSelected = true; foreach (Transform artist in alphabeticalContainer.transform) { if (selectedArtist.gameObject.name == artist.gameObject.name) { selectedArtistContainer = artist; foreach (Transform album in artist) { album.gameObject.SetActive(true); album.position = new Vector3(0, -7, 0); } } } } } } catch (UnityException) { //There are no artists under that letter } /*Album selection*/ try { if (artistSelected && !albumSelected) { selectedAlbum = selectedArtistContainer.transform.GetChild(selectedAlbumIndex); selectedAlbum.GetChild(0).GetComponent<MeshRenderer>().material.shader = GUI3DTextShader; selectedAlbum.GetChild(0).gameObject.SetActive(true); selectedAlbum.GetChild(0).GetComponent<TextMesh>().text = selectedAlbum.name; if (!foldersLifted || !waitingForAlbumSelection) { foreach (Transform artistFolder in artistFolderContainer.transform) { liftFolder(artistFolder.gameObject, artistFolder.position + new Vector3(0, 20, 0)); } //Move albums into view int albumIndex = 0; Vector3 currentOffset; float currentY = selectedArtistContainer.position.y; if (!waitingForAlbumSelection) { foreach (Transform album in selectedArtistContainer) { currentOffset = new Vector3(folderEndPositions[0].x * -0.2f, 0, folderEndPositions[0].z * -0.2f); albumEndPositions[albumIndex] = (new Vector3(folderEndPositions[0].x * 0.8f, currentY, folderEndPositions[0].z * 0.8f) - currentOffset*(currentY/-0.5f)); positionAlbum(album.gameObject, albumEndPositions[albumIndex]); albumIndex++; currentY -= 0.1f; } } } if (Input.GetKeyDown("x")) { folderIsMoving = true; artistSelected = false; waitingForAlbumSelection = false; //Move albums back, deactivate foreach (Transform album in selectedArtistContainer) { album.gameObject.SetActive(false); } albumsOut = false; foldersLifted = false; } if (Input.GetKeyDown("w")) { waitingForAlbumSelection = true; if (selectedAlbumIndex > 0) { selectedAlbum.GetChild(0).gameObject.SetActive(false); selectedAlbumIndex--; //Move albums down albumLiftingDown = true; previouslySelectedAlbum = selectedAlbum; previouslySelectedAlbumIndex = previouslySelectedAlbum.GetSiblingIndex(); scrollTarget = selectedAlbum.parent.GetChild(previouslySelectedAlbumIndex-1).localPosition + new Vector3(0, -3, 0); } } if (Input.GetKeyDown("s")) { waitingForAlbumSelection = true; if (selectedAlbumIndex < selectedArtistContainer.childCount-1) { selectedAlbum.GetChild(0).gameObject.SetActive(false); selectedAlbumIndex++; //Move albums up albumLiftingUp = true; previouslySelectedAlbum = selectedAlbum; previouslySelectedAlbumIndex = previouslySelectedAlbum.GetSiblingIndex(); scrollTarget = selectedAlbum.parent.GetChild(previouslySelectedAlbumIndex+1).localPosition + new Vector3(0, 3, 0); } } if (Input.GetKeyDown("f") && albumsOut && !albumSelected) { albumSelected = true; } selectedAlbum = selectedArtistContainer.transform.GetChild(selectedAlbumIndex); if (albumLiftingUp) { liftAlbumUp(previouslySelectedAlbum.gameObject, scrollTarget); } if (albumLiftingDown) { liftAlbumDown(selectedAlbum.gameObject, albumEndPositions[selectedAlbumIndex]); } } } catch (UnityException) { } /*Song selection*/ try { if (albumSelected && !songSelected) { //print ("Album selected"); //Generate song names folderIsMoving = false; if (!songsOut) { songIndex = 0; foreach (Transform song in selectedAlbum) { selectedAlbum.gameObject.transform.GetChild(0).gameObject.SetActive(false); if (song.gameObject.name != "AlbumTitle" && (song.gameObject.name.EndsWith(".mp3") || song.gameObject.name.EndsWith(".wav") || song.gameObject.name.EndsWith(".ogg"))) { FileInfo f = new FileInfo(song.gameObject.name); ID3v1 tagger = new ID3v1(); FileStream mp3Stream = new FileStream(f.FullName, FileMode.Open, FileAccess.Read, FileShare.None); Mp3File currentMP3 = new Mp3File(f); currentMP3.Update(); string songName = ""; try { tagger.Deserialize(mp3Stream); mp3Stream.Close(); songName = tagger.Song; } catch (Id3Lib.Exceptions.TagNotFoundException ex) { songName = "[Unknown track title]"; print(ex); } songHolders[songIndex] = new GameObject(song.gameObject.name); songHolders[songIndex].transform.position = selectedAlbum.transform.GetChild(0).transform.position; songHolders[songIndex].transform.eulerAngles = selectedAlbum.transform.GetChild(0).transform.eulerAngles; //songHolders[songIndex].transform.eulerAngles -= new Vector3(0, -90, 0); songHolders[songIndex].transform.position -= new Vector3(0, songIndex * 0.2f - 0.7f, 0); songHolders[songIndex].AddComponent<TextMesh>(); songHolders[songIndex].GetComponent<TextMesh>().text = songName; songHolders[songIndex].GetComponent<TextMesh>().fontSize = 60 - songName.Length; songHolders[songIndex].GetComponent<TextMesh>().characterSize = .03f; songHolders[songIndex].GetComponent<MeshRenderer>().material.shader = GUI3DTextShader; songIndex++; } } songHolders[0].GetComponent<MeshRenderer>().material.color = Color.yellow; songsOut = true; } else { songHolders[selectedSong].GetComponent<MeshRenderer>().material.color = Color.yellow; if (Input.GetKeyDown("s") && (selectedSong < songIndex-1)) { songHolders[selectedSong].GetComponent<MeshRenderer>().material.color = Color.white; selectedSong++; } if (Input.GetKeyDown("w") && (selectedSong > 0)) { songHolders[selectedSong].GetComponent<MeshRenderer>().material.color = Color.white; selectedSong--; } if (Input.GetKeyDown("x")) { selectedAlbum.GetChild(0).gameObject.SetActive(true); for (int i = 0; i <= songIndex; i++) { Destroy(songHolders[i]); } songsOut = false; albumSelected = false; selectedSong = 0; songIndex = 0; } if (Input.GetKeyDown("f")) { PreserveData p = selectedSongInfo.GetComponent<PreserveData>(); p.path = songHolders[selectedSong].name; if (selectedAlbum.gameObject.GetComponent<Renderer>().material.mainTexture == null) { print ("null here"); } if (selectedAlbum.gameObject.GetComponent<Renderer>().material.mainTexture != null) { Color[] albumColors = colorAnalyzer.findColorScheme(selectedAlbum.gameObject.GetComponent<Renderer>().material.mainTexture, colorDetail, colorTolerance); p.colorScheme = albumColors; /*for (int i = 0; i < colorDetail; i++) { print (albumColors[i]); }*/ }; //Application.LoadLevel("ParticleRoom"); Application.LoadLevel(levelToLoad); } } } } catch (UnityException) { } }
void alphabetizeByArtist() { GameObject[] audioHolders = new GameObject[10000]; //temporary placeholder max size //print (musicFiles.Count); string artist = "No Artist"; string album = "No Album"; foreach(FileInfo f in musicFiles) { //print("Loading "+f.FullName); GameObject audioHolder = new GameObject(f.Name); audioHolder.name = f.FullName; audioHolders[songIndex] = audioHolder; songIndex++; if (!(f.FullName.EndsWith(".jpg") || f.FullName.EndsWith(".png"))) { ID3v1 tagger = new ID3v1(); FileStream mp3Stream = new FileStream(f.FullName, FileMode.Open, FileAccess.Read, FileShare.None); Mp3File currentMP3 = new Mp3File(f); currentMP3.Update(); try { tagger.Deserialize(mp3Stream); mp3Stream.Close(); artist = tagger.Artist; album = tagger.Album; } catch (Id3Lib.Exceptions.TagNotFoundException ex) { mp3Stream.Close(); album = "No Album"; artist = "No artist"; print(ex); } } for (int i = 0; i < alphabeticalTitles.Length; i++) { string firstChar = alphabeticalTitles.GetValue(i).ToString().Substring(5,1); //Organize into specific artists and albums if (artist.StartsWith(firstChar)) { //if artist holder doesn't already exist, create - otherwise add song to artist holder bool artistExists = false; Transform currentArtistHolder = null; foreach(Transform artistHolder in alphabeticalTitles[i].transform) { if (artistHolder.name.Trim() == artist.Trim()) { artistExists = true; currentArtistHolder = artistHolder; } } if (!artistExists) { currentArtistHolder = new GameObject(artist).transform; currentArtistHolder.transform.parent = alphabeticalTitles[i].transform; } //audioHolder.transform.parent = currentArtistHolder; bool albumExists = false; Transform currentAlbumHolder = null; foreach(Transform albumHolder in currentArtistHolder.transform) { if (albumHolder.name.Trim () == album.Trim()) { albumExists = true; currentAlbumHolder = albumHolder; } } if (!albumExists) { currentAlbumHolder = Instantiate(albumSleevePrefab).transform; currentAlbumHolder.transform.parent = currentArtistHolder; currentAlbumHolder.name = album; } /*if (!albumExists) { currentAlbumHolder = new GameObject(album).transform; currentAlbumHolder.transform.parent = currentArtistHolder; currentAlbumHolder.gameObject.AddComponent<Renderer>(); currentAlbumHolder.gameObject.GetComponent<Renderer>().material.mainTexture = artGrabber.getAlbumArtAsTexture(audioHolder); }*/ audioHolder.transform.parent = currentAlbumHolder; currentAlbumHolder.gameObject.GetComponent<Renderer>().material.mainTexture = artGrabber.getAlbumArtAsTexture(audioHolder, f); if (currentAlbumHolder.gameObject.GetComponent<Renderer>().material.mainTexture == null) { currentAlbumHolder.gameObject.GetComponent<Renderer>().material.mainTexture = Resources.Load("defaultAlbumArt") as Texture2D; } } } } return; }