// =================== Copyright (C) 2007 Odorless Entertainment =================== // // // // This source file was written by the collective efforts of Odorless Entertainment: // // // // Nauful 'Cade' Shaikh [co-owner, lead technical director] // // Jonathan 'Blade' Bastnagel [co-owner, co-technical director] // // Maciek 'ManTis' Kozlowski [co-owner, lead toolset developer] // // Mats 'RBurn' Soderlund [co-owner, lead art director/lead designer] // // // // File: Music.cs // Purpose: [Write a brief description here] // // // This program is free software; you can redistribute it and/or modify // // it under the terms of the GNU General Public License as published by // // the Free Software Foundation; either version 2 of the License, or // // (at your option) any later version. // // // // This program is distributed in the hope that it will be useful, // // but WITHOUT ANY WARRANTY; without even the implied warranty of // // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // // GNU General Public License for more details. // // // // You should have received a copy of the GNU General Public License along // // with this program; if not, write to the Free Software Foundation, Inc., // // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. // // // // ================================================================================== // //#define STOPMUSIC using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; using Microsoft.DirectX; using Microsoft.DirectX.Direct3D; using KillShit.Engine.Audio; namespace KillShit.Game.Audio { public class MusicManager { public bool bDisposed = false; Engine.Audio.FMOD.Channel chMusicChannel; public bool bShuffle = true; bool bIsPlaying = false; List fiSongs; int iCurrentSong = 0; int iNextSong = 0; string strSongTitle = null; string strSongArtist = null; Vector2 v2Position = Vector2.Empty; Vector2 v2Length = Vector2.Empty; string strPosition; string strLength; DirectoryInfo diMusicDirectory = new DirectoryInfo ( Game.Misc.IOFunc.BaseDir + "Sound\\Music" ); public MusicManager ( ) { fiSongs = new List ( ); } public void Init ( ) { if ( fiSongs.Count > 1 ) { Engine.Renderer.Driver.Debug.AddText ( fiSongs.Count + " songs added from the Music Directory.", System.Drawing.Color.LightGreen.ToArgb ( ) ); } Engine.Renderer.Driver.Debug.AddText ( "Music Manager Initialization Successful!", (int)Engine.Debug.MESSAGE_TYPE.INITIALIZE ); DoAll ( ); } public void Update ( float DeltaTime ) { chMusicChannel.isPlaying ( ref bIsPlaying ); if ( !bIsPlaying ) { DoAll ( ); } GetPosition ( ); RenderSongInfo ( ); } private void DoAll ( ) { UpdateSongList ( ); CheckPlayModes ( ); if ( bShuffle ) SelectRandomSong ( ); PlayNextSong ( ); GetTagInfo ( ); GetLength ( ); } private void PlayNextSong ( ) { AudioManager.getPreCachedSound ( fiSongs [ iNextSong ].FullName ).sdSound.setMode ( Engine.Audio.FMOD.MODE.LOOP_OFF ); AudioManager.playSound ( KillShit.Engine.Audio.FMOD.CHANNELINDEX.REUSE, fiSongs [ iNextSong ].FullName, false, ref chMusicChannel ); Engine.Renderer.Driver.Debug.AddText ( "Music Manager[Status] : Switching to next song.", ( int ) Engine.Debug.MESSAGE_TYPE.INFORMATION ); iCurrentSong = iNextSong; } private void RenderSongInfo ( ) { string finalOutput = "Currently Playing : " + strSongTitle + " by " + strSongArtist + " - [" + strPosition + " / " + strLength + "]"; int Width = Engine.Renderer.Driver.Text.MeasureSize ( finalOutput ).Width; Engine.Renderer.Driver.Text.RenderText ( finalOutput, frmMain.pWidth - Width - frmMain.pWidthPad, 0, Color.FromArgb ( 255, 128, 192, 255 ).ToArgb ( ) ); } private void GetTagInfo ( ) { Engine.Audio.FMOD.TAG Tag = new Engine.Audio.FMOD.TAG ( ); // TITLE if ( AudioManager.getPreCachedSound ( fiSongs [ iCurrentSong ].FullName ).sdSound.getTag ( "TITLE", 0, ref Tag ) == Engine.Audio.FMOD.RESULT.OK ) { strSongTitle = "" + Marshal.PtrToStringAnsi ( Tag.data ); } else { strSongTitle = "Unknown Title"; } // ARTIST if ( AudioManager.getPreCachedSound ( fiSongs [ iCurrentSong ].FullName ).sdSound.getTag ( "ARTIST", 0, ref Tag ) == Engine.Audio.FMOD.RESULT.OK ) { strSongArtist = "" + Marshal.PtrToStringAnsi ( Tag.data ); } else { strSongArtist = "Unknown Artist"; } } private void GetLength ( ) { uint uiLength = 0; AudioManager.getPreCachedSound ( fiSongs [ iCurrentSong ].FullName ).sdSound.getLength ( ref uiLength, Engine.Audio.FMOD.TIMEUNIT.MS ); uiLength /= 1000; v2Length.X = ( float ) Math.Floor ( ( float ) uiLength / 60 ); v2Length.Y = ( float ) Math.Floor ( ( float ) uiLength % 60 ); string LengthFinalY = v2Length.Y.ToString ( ); // Add leading zero. if ( v2Length.Y < 10 ) { LengthFinalY = "0" + v2Length.Y; } strLength = v2Length.X + ":" + LengthFinalY; } private void GetPosition ( ) { uint uiPosition = 0; chMusicChannel.getPosition ( ref uiPosition, Engine.Audio.FMOD.TIMEUNIT.MS ); uiPosition /= 1000; v2Position.X = ( float ) Math.Floor ( ( float ) uiPosition / 60 ); v2Position.Y = ( float ) Math.Floor ( ( float ) uiPosition % 60 ); string PositionY = v2Position.Y.ToString ( ); // Add leading zero. if ( v2Position.Y < 10 ) { PositionY = "0" + v2Position.Y; } strPosition = v2Position.X + ":" + PositionY; } private void CheckPlayModes ( ) { if ( bShuffle ) { SelectRandomSong ( ); } else { iNextSong = iCurrentSong; } } public void SelectRandomSong ( ) { if ( fiSongs.Count > 1 ) { Random RandomSongSelector = new Random ( ); iNextSong = iCurrentSong; while ( iNextSong == iCurrentSong ) { iNextSong = RandomSongSelector.Next ( fiSongs.Count ); } } else if ( fiSongs.Count == 1 ) { iNextSong = 0; Engine.Renderer.Driver.Debug.AddText ( "Music Manager[Warning] : Only one song was in the Music Directory, it will continue to loop.", ( int ) Engine.Debug.MESSAGE_TYPE.WARNING ); } else { Engine.Renderer.Driver.Debug.AddText ( "Music Manager[Warning] : Couldn't find any songs in the Music Directory.", ( int ) Engine.Debug.MESSAGE_TYPE.WARNING ); return; } } private void UpdateSongList ( ) { FileInfo [ ] fiFiles; fiFiles = diMusicDirectory.GetFiles ( ); fiSongs.Clear ( ); foreach ( FileInfo fiFile in fiFiles ) { if ( fiFile.Extension == ".mp3" || fiFile.Extension == ".wav" || fiFile.Extension == ".ogg" ) { fiSongs.Add ( fiFile ); } } foreach ( FileInfo Song in fiSongs ) { AudioManager.addSound ( Song.FullName, TYPE.Music, ( Engine.Audio.FMOD.MODE._2D | Engine.Audio.FMOD.MODE.HARDWARE | Engine.Audio.FMOD.MODE.CREATESTREAM ) ); } } public void Dispose ( ) { foreach( FileInfo Song in fiSongs ) { AudioManager.destroySound ( fiSongs [ iCurrentSong ].FullName ); } bDisposed = true; } } }