protected FMOD.Sound GetSound(string name, bool ambient, bool local) { Dictionary <string, FMOD.Sound> soundDict; FMOD.Sound sound; FMOD.MODE mode; if (ambient) { soundDict = ambientSounds; mode = FMOD.MODE.DEFAULT; } else { soundDict = positionalSounds; mode = FMOD.MODE._3D; } if (soundDict.ContainsKey(name)) { sound = soundDict[name]; } else { if (local) { // Ask the resource manager where the file is (check any common asset directory) string filename = ResourceManager.ResolveCommonResourceData(name); FileInfo info = new FileInfo(filename); sound = null; if (info.Length > 200000) { FMOD.RESULT result = fmod.createStream(filename, mode, ref sound); CheckResults(result); } else { FMOD.RESULT result = fmod.createSound(filename, mode, ref sound); CheckResults(result); } } else { sound = null; log.DebugFormat("before playing non-local sound: {0}", name); FMOD.RESULT result = fmod.createStream(name, mode, ref sound); log.DebugFormat("after playing non-local sound: {0}", name); CheckResults(result); } soundDict.Add(name, sound); } return(sound); }
private MusicPlayer() { result = FMOD.Factory.System_Create(out FMODSystem); if (result != FMOD.RESULT.OK) { throw new Exception("This crap didn't work!!"); } result = FMODSystem.setDSPBufferSize(1024, 10); result = FMODSystem.init(32, FMOD.INITFLAGS.NORMAL, (IntPtr)0); var info = new FMOD.CREATESOUNDEXINFO(); var song = new FMOD.Sound(); ChannelGroup = new FMOD.ChannelGroup(); ChannelGroup.clearHandle(); result = FMODSystem.createStream("rain.ogg", FMOD.MODE.DEFAULT, out song); result = FMODSystem.playSound(song, ChannelGroup, false, out Channel); bool isPlaying = false; Channel.isPlaying(out isPlaying); Channel.setVolume(1); Channel.setMode(FMOD.MODE.LOOP_NORMAL); Channel.setLoopCount(-1); int t = 1; }
private void button1_Click(object sender, System.EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); if (sound != null) { if (channel != null) { channel.stop(); channel = null; } sound.release(); sound = null; } if (dialog.ShowDialog() == DialogResult.OK) { FMOD.RESULT result; result = system.createStream(dialog.FileName, FMOD.MODE.SOFTWARE | FMOD.MODE._2D, ref sound); ERRCHECK(result); textBox.Text = dialog.FileName; } playButton.Text = "Play"; }
private void JingleInit(string filename) { FMOD.Sound tempSound = null; thisLock = new Object(); // Retrieve information about sound FMOD.RESULT result = system.createSound(filename, (FMOD.MODE._2D | FMOD.MODE.SOFTWARE), ref tempSound); ErrorCheck(result); FMOD.SOUND_TYPE soundType = (FMOD.SOUND_TYPE) 0; FMOD.SOUND_FORMAT soundFormat = (FMOD.SOUND_FORMAT) 0; int tempBits = 0, tempChannels = 0, tempPriority = 0; uint tempPcm = 0; float tempFrequency = 0.0f, tempPan = 0.0f, tempVolume = 0.0f; result = tempSound.getFormat(ref soundType, ref soundFormat, ref tempChannels, ref tempBits); ErrorCheck(result); result = tempSound.getDefaults(ref tempFrequency, ref tempVolume, ref tempPan, ref tempPriority); ErrorCheck(result); result = tempSound.getLength(ref tempPcm, FMOD.TIMEUNIT.PCM); ErrorCheck(result); // Fill sound parameters ChannelCount = tempChannels; Frequency = (int)tempFrequency; pcm = tempPcm; bits = tempBits; // Obtain Raw PCM data from sound GetRawData(tempSound, tempChannels, tempBits, tempPcm); // Release temp sound instance tempSound.release(); tempSound = null; FMOD.CREATESOUNDEXINFO exInfo = new FMOD.CREATESOUNDEXINFO(); exInfo.cbsize = Marshal.SizeOf(exInfo); exInfo.length = (uint)(pcm * sizeof(short)); exInfo.numchannels = ChannelCount; exInfo.format = FMOD.SOUND_FORMAT.PCM16; exInfo.defaultfrequency = (int)Frequency; // Create a stream from obtained data result = system.createStream(SoundDataBuffer.Buffer, (FMOD.MODE.OPENMEMORY | FMOD.MODE.OPENRAW), ref exInfo, ref sounds); ErrorCheck(result); } // loading jingle
public FMOD_Music(FMOD_Audio audio, string filename) { mAudio = audio; mSystem = mAudio.FMODSystem; FMOD.RESULT result = mSystem.createStream(filename, FMOD.MODE.LOOP_NORMAL | FMOD.MODE.ACCURATETIME, ref mSound); CreateChannel(); }
public void PlayBGM(string path = null) { string play; if (path == null) { path = BGMPlaylist[BGMId]; } play = "Media/Music/" + path; Result = System.createStream(play, FMOD.MODE.DEFAULT, ref SoundBGM); Result = System.playSound(0, SoundBGM, false, ref ChannelBGM); IsBGMPlaying = true; Volume = _volume; }
static GmfSound() { sys = new FMOD.System(); FMOD.Factory.System_Create(ref sys); sys.init(64, FMOD.INITFLAGS.NORMAL, IntPtr.Zero); for (int i = 0; i < sounds.Length; ++i) { sys.createStream(string.Format("Sound/{0}.wav", i), FMOD.MODE.DEFAULT, ref sounds[i]); } }
public static void LoadSong(int id, bool loop) { PlayingSongID = id; if (CurrentSong != null) { CurrentSong.release(); } FMOD.MODE mode = (loop) ? FMOD.MODE.LOOP_NORMAL : FMOD.MODE.DEFAULT; system.createStream(PlayList[id], mode, out CurrentSong); system.playSound(CurrentSong, ChannelGroup, false, out Channel); }
private void timer1_Tick(object sender, System.EventArgs e) { FMOD.RESULT result; if (!initialised) { ERRCHECK(result = FMOD.Event_Factory.EventSystem_Create(ref eventsystem)); ERRCHECK(result = eventsystem.init(64, FMOD.INITFLAGS.NORMAL, (IntPtr)null, FMOD.EVENT_INITFLAGS.NORMAL)); ERRCHECK(result = eventsystem.setMediaPath("../../../../examples/media/")); ERRCHECK(result = eventsystem.load("examples.fev")); ERRCHECK(result = eventsystem.getGroup("examples/FeatureDemonstration/SequencingAndStitching", false, ref eventgroup)); ERRCHECK(result = eventsystem.getSystemObject(ref sys)); ERRCHECK(result = sys.createStream("../../../../examples/media/tutorial_bank.fsb", (FMOD.MODE._2D | FMOD.MODE.SOFTWARE), ref fsb)); initialised = true; } /* * "Main Loop" */ ERRCHECK(result = eventsystem.update()); if (eventstart) { ERRCHECK(result = eventgroup.getEvent("ProgrammerSounds", FMOD.EVENT_MODE.DEFAULT, ref _event)); ERRCHECK(result = _event.setCallback(eventcallback, (IntPtr)null)); ERRCHECK(result = _event.start()); eventstart = false; } /* * Cleanup and exit */ if (exit) { ERRCHECK(result = eventsystem.unload()); ERRCHECK(result = fsb.release()); ERRCHECK(result = eventsystem.release()); Application.Exit(); } }
/// <summary> /// Reads the tag of a file /// </summary> /// <param name="filename"></param> public TagInfo ReadTags(string filename) { FMOD.Sound snd = null; TagInfo tagInfo = null; try { //if ok, create a sound with tag support FMOD.RESULT result; //open the sound for tag reading result = _system.createStream(filename, FMOD.MODE.OPENONLY, out snd); CheckError(result); //if ok, get the number of tags in the fie int numTags; int numTagsUpdated; //get num tags result = snd.getNumTags(out numTags, out numTagsUpdated); CheckError(result); //read the tags tagInfo = ReadTags(snd, numTags); } catch (Exception) { //ignore errors for now } finally { if (snd != null) { //release the sound snd.release(); snd = null; } } //return the tag info return(tagInfo); }
/// <summary> /// Loads streamed sound stream from file. /// Use this function to load music and long ambience tracks. /// </summary> public static Sound LoadStreamedSound(string path) { var buffer = FileLoader.LoadFileAsBuffer(path); // Internal FMOD pointer points to this memory, so we don't want it to go anywhere. var handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); var info = new FMOD.CREATESOUNDEXINFO(); info.length = (uint)buffer.Length; info.cbsize = Marshal.SizeOf(info); Native.createStream( buffer, FMOD.MODE.OPENMEMORY | FMOD.MODE.CREATESTREAM, ref info, out FMOD.Sound newSound ); return(new Sound(newSound, buffer, handle)); }
private void Form1_Load(object sender, System.EventArgs e) { FMOD.CREATESOUNDEXINFO exinfo = new FMOD.CREATESOUNDEXINFO(); FMOD.RESULT result; uint version = 0; /* * Create a System object and initialize. */ result = FMOD.Factory.System_Create(ref system); ERRCHECK(result); result = system.getVersion(ref version); ERRCHECK(result); if (version < FMOD.VERSION.number) { MessageBox.Show("Error! You are using an old version of FMOD " + version.ToString("X") + ". This program requires " + FMOD.VERSION.number.ToString("X") + "."); Application.Exit(); } result = system.init(1, FMOD.INITFLAGS.NORMAL, (IntPtr)null); ERRCHECK(result); /* * Set up the FMOD_CREATESOUNDEXINFO structure for the user stream with room for 2 subsounds. (our subsound double buffer) */ exinfo.cbsize = Marshal.SizeOf(exinfo); exinfo.defaultfrequency = 44100; exinfo.numsubsounds = 2; exinfo.numchannels = 1; exinfo.format = FMOD.SOUND_FORMAT.PCM16; /* * Create the 'parent' stream that contains the substreams. Set it to loop so that it loops between subsound 0 and 1. */ result = system.createStream("", FMOD.MODE.LOOP_NORMAL | FMOD.MODE.OPENUSER, ref exinfo, ref sound); ERRCHECK(result); /* * Add 2 of our streams as children of the parent. They should be the same format (ie mono/stereo and bitdepth) as the parent sound. * When subsound 0 has finished and it is playing subsound 1, we will swap subsound 0 with a new sound, and the same for when subsound 1 has finished, * causing a continual double buffered flip, which means continuous sound. */ result = system.createStream(soundname[0], FMOD.MODE.DEFAULT, ref subsound[0]); ERRCHECK(result); result = system.createStream(soundname[1], FMOD.MODE.DEFAULT, ref subsound[1]); ERRCHECK(result); result = sound.setSubSound(0, subsound[0]); ERRCHECK(result); result = sound.setSubSound(1, subsound[1]); ERRCHECK(result); /* * Set up the gapless sentence to contain these first 2 streams. */ { int[] soundlist = { 0, 1 }; result = sound.setSubSoundSentence(soundlist, 2); ERRCHECK(result); } subsoundid = 0; sentenceid = 2; /* The next sound to be appeneded to the stream. */ /* * Play the sound. */ result = system.playSound(FMOD.CHANNELINDEX.FREE, sound, false, ref channel); ERRCHECK(result); }
/// <summary> /// Spielt die angegebene Musikdatei ab. /// </summary> /// <param name="filename"></param> public void Play(string filename) { Filename = filename; if (!this.isPreListenSoundEngine) { if (currentSoundIndex == 0) { currentSoundIndex = 1; } else { currentSoundIndex = 0; } } if (IsPlaying) { Stop(); } FMOD.RESULT result = FMOD.RESULT.OK; if (filename.StartsWith("cd:")) { string cdDrive = string.Format("{0}:", filename[3]); int trackNumber = Miscellaneous.Misc.Atoi(filename.Substring(5)); if (Settings.Current.UseMCI) { if (mciCDEngine == null) { mciCDEngine = new MCICDEngine(); mciCDEngine.Open(cdSoundDriveLetter[0]); } currentCDTrackNumber = trackNumber; mciCDEngine.PlayTrack(trackNumber); return; } else { if (cdSound == null) { result = system.createStream(cdDrive, (FMOD.MODE._2D | FMOD.MODE.HARDWARE), ref cdSound); ERRCHECK(result); } else { if (cdDrive != cdSoundDriveLetter) { cdSound.release(); result = system.createStream(cdDrive, (FMOD.MODE._2D | FMOD.MODE.HARDWARE), ref cdSound); ERRCHECK(result); } } if (currentSoundIndex == 0) { result = cdSound.getSubSound(trackNumber, ref sound1); } else { result = cdSound.getSubSound(trackNumber, ref sound2); } ERRCHECK(result); } cdSoundDriveLetter = cdDrive; } else { if (currentSoundIndex == 0) { result = system.createStream(filename, (FMOD.MODE._2D | FMOD.MODE.HARDWARE), ref sound1); } else { result = system.createStream(filename, (FMOD.MODE._2D | FMOD.MODE.HARDWARE), ref sound2); } } ERRCHECK(result); if (result == FMOD.RESULT.OK) { if (currentSoundIndex == 0) { system.playSound(FMOD.CHANNELINDEX.FREE, currentSound, false, ref channel1); } else { system.playSound(FMOD.CHANNELINDEX.FREE, currentSound, false, ref channel2); } // Hiermit wird der neu spielende Track "stumm" geschaltet. IsMuted = IsMuted; // Aktuelle Lautstärke setzen Volume = Volume; /*string str = string.Format("Detecting BPM..."); * Big3.Hitbase.SharedResources.HitbaseCommands.SetMainStatusText.Execute(str, System.Windows.Application.Current.MainWindow); * * BackgroundWorker bw = new BackgroundWorker(); * bw.DoWork += delegate * { * float bpm = BPMDetection(filename); * CurrentTrackBPM = (int)(bpm + 0.5); // Runden * }; * bw.RunWorkerCompleted += delegate * { * string bpmDetected = string.Format("BPM: {0}", CurrentTrackBPM); * Big3.Hitbase.SharedResources.HitbaseCommands.SetMainStatusText.Execute(bpmDetected, System.Windows.Application.Current.MainWindow); * }; * bw.RunWorkerAsync();*/ } }
private void LoadSong(int songId, string name) { FMOD.RESULT r = FMODSystem.createStream("Music/" + name, FMOD.MODE.DEFAULT, out Music[songId]); //Console.WriteLine("loading " + songId + ", got result " + r); }
/// <summary> /// Play The Sound /// </summary> /// <param name="path">Sound Path</param> /// <param name="paused">The Sound Stat</param> /// <param name="Type">Type Of Sound</param> public void Play(string path, bool paused, SoundType Type) { bool isPlaying = false; RESULT result = RESULT.OK; if (Channel != null) { //si la musique existe result = Channel.isPlaying(ref isPlaying); } else { isPlaying = false; } if (EngineError != null) { EngineError(result); } if ((currentPath == path) && isPlaying)//si la musique du chemin courant est entrain detre joué { Stop(); Play(Type); } else if (currentPath == path)//sinon la musique du chemin courant nest pas entrain detre joué { result = system.playSound(CHANNELINDEX.FREE, music, false, ref Channel); if (EngineError != null) { EngineError(result); } if (Channel != null) { result = Channel.setCallback(FMOD.CHANNEL_CALLBACKTYPE.END, channelCallback, 0); } if (EngineError != null) { EngineError(result); } } else { //si cest une nouvelle musique if (Channel != null) { Channel.stop(); Channel = null; } if (music != null) { result = music.release(); music = null; if (EngineError != null) { EngineError(result); } } if (Type == SoundType.Sound) { result = system.createSound(path, MODE.SOFTWARE | MODE.CREATECOMPRESSEDSAMPLE | MODE.LOOP_OFF, ref music); } else { result = system.createStream(path, MODE.SOFTWARE | MODE.CREATECOMPRESSEDSAMPLE | MODE.LOOP_OFF, ref music); } if (EngineError != null) { EngineError(result); } result = system.playSound(CHANNELINDEX.FREE, music, paused, ref Channel); if (EngineError != null) { EngineError(result); } if (Channel != null) { result = Channel.setCallback(FMOD.CHANNEL_CALLBACKTYPE.END, channelCallback, 0); } if (EngineError != null) { EngineError(result); } currentPath = path; Update(); } }
/// <summary> /// 播放 /// </summary> public void play() { //mciSendString("close all", "", 0, 0); //mciSendString("open \"" + FilePath + "\" alias media", "", 0, 0); //mciSendString("play media", "", 0, 0); try { //if(fmodSystem != null) { // stop(); //} FMOD.RESULT fmodResult; if (fmodSystem == null) { // 1.创建System实例 fmodResult = FMOD.Factory.System_Create(out fmodSystem); if (fmodResult != FMOD.RESULT.OK) { Debug.WriteLine(FMOD.Error.String(fmodResult)); return; } // 2.初始化System fmodResult = fmodSystem.init(32, FMOD.INITFLAGS.NORMAL, IntPtr.Zero); if (fmodResult != FMOD.RESULT.OK) { Debug.WriteLine(FMOD.Error.String(fmodResult)); return; } } //FMOD.Sound soundEffect; // 音效 //FMOD.Sound backgroundMusic; // 背景音乐 // 3.创建声音资源 // 使用createSound创建音效 创建音效要用MODE.LOOP_OFF关闭循环播放(默认循环开启) out 一个Sound实例 // createSound用来创建比较短不占用空间的音效,例如游戏中的攻击音效,可以在多个通道同时播放 //fmodResult = fmodSystem.createSound("test.wav", FMOD.MODE.LOOP_OFF, out soundEffect); //if(fmodResult != FMOD.RESULT.OK) { // Debug.WriteLine(FMOD.Error.String(fmodResult)); // return; //} // 使用createStream创建背景音乐 提供文件名 创建BGM可以默认开启循环 out 一个Sound实例 // createStream用来创建比较长的声音,需要不停加载数据 例如游戏中的背景音乐,stream只能在一个通道播放 fmodResult = fmodSystem.createStream(path, FMOD.MODE.LOOP_NORMAL, out backgroundMusic); if (fmodResult != FMOD.RESULT.OK) { Debug.WriteLine(FMOD.Error.String(fmodResult)); return; } //FMOD.Channel channelEffect; // 音效的播放通道 // 播放音乐 提供Sound 通道组我们没有 开始时是否暂停 是 out 当前声音的播放通道 fmodSystem.playSound(backgroundMusic, null, false, out channelMusic); // 暂停来设置音量 0.0 - 1.0 channelMusic.setVolume(volume); // 恢复播放 //channelMusic.setPaused(false); // 按下ESC退出播放 //while(Console.ReadKey().Key != ConsoleKey.Escape) { // // 按下空格播放音效 // if(Console.ReadKey().Key == ConsoleKey.Spacebar) // fmodSystem.playSound(soundEffect, null, false, out channelEffect); //} // 最后释放所有资源 可以代替.close(); //fmodSystem.release(); } catch (Exception ex) { Debug.WriteLine("aa:" + ex.ToString()); } }