public void Update(GameTime gameTime)
        {
            system.update();
            if (IsSongPlaying())
            {
                uint length   = 0;
                uint position = 0;
                CurrentSong.getLength(out length, FMOD.TIMEUNIT.MS);
                Channel.getPosition(out position, FMOD.TIMEUNIT.MS);
                if (position == length)
                {
                    NextSongEvent.Invoke();
                }
            }


            #region Update System & FFT Algorithm

            if (FFTEnabled)
            {
                // Perform a Fast Fourier Transform

                int    windowSize = spectrum.Length; //Samples.
                IntPtr data;
                uint   length;
                //hanning bpm
                MyDSP.setParameterInt((int)FMOD.DSP_FFT.WINDOWTYPE, (int)FMOD.DSP_FFT_WINDOW.HAMMING);
                MyDSP.setParameterInt((int)FMOD.DSP_FFT.WINDOWSIZE, windowSize);
                MyDSP.getParameterData((int)FMOD.DSP_FFT.SPECTRUMDATA, out data, out length);
                MyDSP.getParameterFloat((int)FMOD.DSP_FFT.DOMINANT_FREQ, out higher_freq);
                fftParameter = (FMOD.DSP_PARAMETER_FFT)Marshal.PtrToStructure(data, typeof(FMOD.DSP_PARAMETER_FFT));

                #region BPMDetection

                if (fftParameter.spectrum.Length != 0)
                {
                    for (int i = 0; i < windowSize; i++)
                    {
                        if (fftParameter.spectrum.Length == 2)
                        {
                            spectrum[i] = (fftParameter.spectrum[0][i] + fftParameter.spectrum[1][i]) / 2f;
                        }
                    }
                }
                previousSpectrum = spectrum;
                #endregion
            }
            #endregion
        }
示例#2
0
        private void timer_Tick(object sender, System.EventArgs e)
        {
            if (system != null && sound != null)
            {
                uint recordpos = 0;
                uint playpos   = 0;
                bool recording = false;
                bool playing   = false;

                system.isRecording(selected, ref recording);
                system.getRecordPosition(selected, ref recordpos);

                if (channel != null)
                {
                    channel.isPlaying(ref playing);;

                    channel.getPosition(ref playpos, FMOD.TIMEUNIT.PCM);
                }

                statusBar.Text = "State:" + (recording ? playing ? " Recording / playing " : " Recording " : playing ? " Playing " : " Idle ")
                                 + " Record pos = " + recordpos + " Play pos = " + playpos + " Loop " + (looping ? "On" : "Off");
            }
            if (system != null)
            {
                system.update();
            }
        }
示例#3
0
        void tmr_Tick(object sender, EventArgs e)
        {
            y += 1f;

            if (y >= img.Height)
            {
                y = -this.Height;
            }

            specValue++;

            if (specValue > (customSpectrum.Count - 1))
            {
                specValue = 0;
            }


            soundChannel.getPosition(ref position, FMOD.TIMEUNIT.MS);

            if (position >= 33500 && position <= 33500 + 2500)
            {
                soundChannel.setPosition(position + 2500, FMOD.TIMEUNIT.MS);
            }

            this.Invalidate();
        }
        private void timer_Tick(object sender, System.EventArgs e)
        {
            FMOD.RESULT    result;
            FMOD.OPENSTATE openstate       = 0;
            uint           percentbuffered = 0;
            bool           starving        = false;
            bool           busy            = false;

            if (soundcreated)
            {
                result = sound.getOpenState(ref openstate, ref percentbuffered, ref starving, ref busy);
                ERRCHECK(result);

                if (openstate == FMOD.OPENSTATE.READY && channel == null)
                {
                    result = system.playSound(FMOD.CHANNELINDEX.FREE, sound, false, ref channel);
                    ERRCHECK(result);
                }
            }

            if (channel != null)
            {
                uint ms      = 0;
                bool playing = false;
                bool paused  = false;

                for (;;)
                {
                    FMOD.TAG tag = new FMOD.TAG();
                    if (sound.getTag(null, -1, ref tag) != FMOD.RESULT.OK)
                    {
                        break;
                    }
                    if (tag.datatype == FMOD.TAGDATATYPE.STRING)
                    {
                        textBox.Text = tag.name + " = " + Marshal.PtrToStringAnsi(tag.data) + " (" + tag.datalen + " bytes)";
                    }
                    else
                    {
                        break;
                    }
                }

                result = channel.getPaused(ref paused);
                ERRCHECK(result);
                result = channel.isPlaying(ref playing);
                ERRCHECK(result);
                result = channel.getPosition(ref ms, FMOD.TIMEUNIT.MS);
                ERRCHECK(result);

                statusBar.Text = "Time " + (ms / 1000 / 60) + ":" + (ms / 1000 % 60) + ":" + (ms / 10 % 100) + (openstate == FMOD.OPENSTATE.BUFFERING ? " Buffering..." : (openstate == FMOD.OPENSTATE.CONNECTING ? " Connecting..." : (paused ? " Paused       " : (playing ? " Playing      " : " Stopped      ")))) + "(" + percentbuffered + "%)" + (starving ? " STARVING" : "        ");
            }

            if (system != null)
            {
                system.update();
            }
        }
示例#5
0
        private void timer_Tick(object sender, System.EventArgs e)
        {
            FMOD.RESULT result;

            /*
             *  Replace the subsound that just finished with a new subsound, to create endless seamless stitching!
             *
             *  Note that this polls the currently playing subsound using the FMOD_TIMEUNIT_BUFFERED flag.
             *  Remember streams are decoded / buffered ahead in advance!
             *  Don't use the 'audible time' which is FMOD_TIMEUNIT_SENTENCE_SUBSOUND by itself.  When streaming, sound is
             *  processed ahead of time, and things like stream buffer / sentence manipulation (as done below) is required
             *  to be in 'buffered time', or else there will be synchronization problems and you might end up releasing a
             *  sub-sound that is still playing!
             */
            if (channel != null)
            {
                uint currentsubsoundid = 0;

                result = channel.getPosition(ref currentsubsoundid, FMOD.TIMEUNIT.SENTENCE_SUBSOUND | FMOD.TIMEUNIT.BUFFERED);
                ERRCHECK(result);

                if (currentsubsoundid != subsoundid)
                {
                    /*
                     *  Release the sound that isn't playing any more.
                     */
                    result = subsound[subsoundid].release();
                    ERRCHECK(result);

                    /*
                     *  Replace it with a new sound in our list.
                     */
                    result = system.createStream(soundname[sentenceid], FMOD.MODE.DEFAULT, ref subsound[subsoundid]);
                    ERRCHECK(result);

                    result = sound.setSubSound(subsoundid, subsound[subsoundid]);
                    ERRCHECK(result);

                    statusBar.Text = "Replacing subsound " + subsoundid + " / 2 with sound " + sentenceid + " / " + NUMSOUNDS;

                    sentenceid++;
                    if (sentenceid >= NUMSOUNDS)
                    {
                        sentenceid = 0;
                    }

                    subsoundid = (int)currentsubsoundid;
                }
            }

            if (system != null)
            {
                result = system.update();
                ERRCHECK(result);
            }
        }
示例#6
0
        /// <summary>
        /// Gets the position of the media playback in milliseconds
        /// </summary>
        /// <param name="channel"></param>
        /// <returns></returns>
        private uint GetPosition()
        {
            FMOD.RESULT result;
            uint        position;

            //get position
            result = _channel.getPosition(out position, FMOD.TIMEUNIT.MS);
            CheckError(result);

            return(position);
        }
示例#7
0
        public static uint GetPlayPosition()
        {
            if (channel == null)
            {
                return(0);
            }

            uint pos = 0;

            channel.getPosition(ref pos, FMOD.TIMEUNIT.MS);
            return(pos);
        }
示例#8
0
        public static int GetPlayPosition()
        {
            if (!IsPlaying())
            {
                return(0);
            }

            uint position = 0;

            FMODChannel.getPosition(ref position, FMOD.TIMEUNIT.MS);

            return((int)position);
        }
        private void timer_Tick(object sender, System.EventArgs e)
        {
            FMOD.RESULT result;
            uint        ms              = 0;
            uint        lenms           = 0;
            bool        paused          = false;
            bool        playing         = false;
            int         channelsplaying = 0;

            if (channel != null)
            {
                FMOD.Sound currentsound = null;

                result = channel.isPlaying(ref playing);
                if ((result != FMOD.RESULT.OK) && (result != FMOD.RESULT.ERR_INVALID_HANDLE) && (result != FMOD.RESULT.ERR_CHANNEL_STOLEN))
                {
                    ERRCHECK(result);
                }

                result = channel.getPaused(ref paused);
                if ((result != FMOD.RESULT.OK) && (result != FMOD.RESULT.ERR_INVALID_HANDLE) && (result != FMOD.RESULT.ERR_CHANNEL_STOLEN))
                {
                    ERRCHECK(result);
                }

                result = channel.getPosition(ref ms, FMOD.TIMEUNIT.MS);
                if ((result != FMOD.RESULT.OK) && (result != FMOD.RESULT.ERR_INVALID_HANDLE) && (result != FMOD.RESULT.ERR_CHANNEL_STOLEN))
                {
                    ERRCHECK(result);
                }

                channel.getCurrentSound(ref currentsound);
                if (currentsound != null)
                {
                    result = currentsound.getLength(ref lenms, FMOD.TIMEUNIT.MS);
                    if ((result != FMOD.RESULT.OK) && (result != FMOD.RESULT.ERR_INVALID_HANDLE) && (result != FMOD.RESULT.ERR_CHANNEL_STOLEN))
                    {
                        ERRCHECK(result);
                    }
                }

                system.getChannelsPlaying(ref channelsplaying);
            }

            statusBar.Text = "Time " + (ms / 1000 / 60) + ":" + (ms / 1000 % 60) + ":" + (ms / 10 % 100) + "/" + (lenms / 1000 / 60) + ":" + (lenms / 1000 % 60) + ":" + (lenms / 10 % 100) + " " + (paused ? "Paused " : playing ? "Playing" : "Stopped") + " Channels Playing " + channelsplaying;

            if (system != null)
            {
                system.update();
            }
        }
示例#10
0
        public void CheckPlaylist()
        {
            if (IsBGMPlaying)
            {
                uint len = 0, pos = 0;
                SoundBGM.getLength(ref len, FMOD.TIMEUNIT.MS);
                ChannelBGM.getPosition(ref pos, FMOD.TIMEUNIT.MS);

                if (len == pos)
                {
                    NextBGM();
                }
            }
        }
示例#11
0
        private void timer_Tick(object sender, System.EventArgs e)
        {
            FMOD.RESULT result;
            uint        ms      = 0;
            uint        lenms   = 0;
            uint        lenmscd = 0;
            bool        playing = false;
            bool        paused  = false;

            if (channel != null)
            {
                result = channel.getPaused(ref paused);
                ERRCHECK(result);
                result = channel.isPlaying(ref playing);
                ERRCHECK(result);
                result = channel.getPosition(ref ms, FMOD.TIMEUNIT.MS);
                ERRCHECK(result);
                result = sound.getLength(ref lenms, FMOD.TIMEUNIT.MS);
                ERRCHECK(result);
            }

            if (cdsound != null)
            {
                /*
                 *  Get length of entire CD.  Did you know you can also play 'cdsound' and it will play the whole CD without gaps?
                 */
                result = cdsound.getLength(ref lenmscd, FMOD.TIMEUNIT.MS);
                ERRCHECK(result);
            }

            statusBar.Text = "Total CD length: " + (lenmscd / 1000 / 60) + ":" + (lenmscd / 1000 % 60) + ":" + (lenmscd / 10 % 100) +
                             " Track " + (currenttrack + 1) + "/" + numtracks + " : " + (ms / 1000 / 60) + ":" + (ms / 1000 % 60) + ":" + (ms / 10 % 100) + "/" + (lenms / 1000 / 60) + ":" + (lenms / 1000 % 60) + ":" + (lenms / 10 % 100) + (paused ? " Paused " : playing ? " Playing" : " Stopped");

            if (system != null)
            {
                system.update();
            }
        }
        private void timer_Tick(object sender, System.EventArgs e)
        {
            FMOD.RESULT    result;
            FMOD.OPENSTATE openstate = 0;

            if (soundcreated)
            {
                uint percentbuffered = 0;
                bool starving        = false;
                bool busy            = false;

                result = sound.getOpenState(ref openstate, ref percentbuffered, ref starving, ref busy);
                ERRCHECK(result);

                if (openstate == FMOD.OPENSTATE.READY && channel == null)
                {
                    result = system.playSound(FMOD.CHANNELINDEX.FREE, sound, false, ref channel);
                    ERRCHECK(result);
                }
            }

            if (channel != null)
            {
                uint ms          = 0;
                bool playing     = false;
                bool paused      = false;
                int  tagsupdated = 0;
                int  numtags     = 0;

                result = sound.getNumTags(ref numtags, ref tagsupdated);
                ERRCHECK(result);

                if (tagsupdated != 0)
                {
                    for (;;)
                    {
                        FMOD.TAG tag = new FMOD.TAG();

                        if (sound.getTag(null, -1, ref tag) != FMOD.RESULT.OK)
                        {
                            break;
                        }

                        if (tag.datatype == FMOD.TAGDATATYPE.STRING)
                        {
                            FMOD.SOUND_FORMAT format = FMOD.SOUND_FORMAT.NONE;
                            int channels             = 0;
                            int bits = 0;

                            sound.getFormat(ref gSoundType, ref format, ref channels, ref bits);

                            if (tag.name == "ARTIST")
                            {
                                if (Marshal.PtrToStringAnsi(tag.data) != gCurrentTrackArtist)
                                {
                                    gCurrentTrackArtist = Marshal.PtrToStringAnsi(tag.data);
                                    gUpdateFileName     = true;
                                }
                            }
                            if (tag.name == "TITLE")
                            {
                                if (Marshal.PtrToStringAnsi(tag.data) != gCurrentTrackTitle)
                                {
                                    gCurrentTrackTitle = Marshal.PtrToStringAnsi(tag.data);
                                    gUpdateFileName    = true;
                                }
                            }
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                result = channel.isPlaying(ref playing);
                if (result != FMOD.RESULT.OK || !playing)
                {
                    sound.release();
                    sound   = null;
                    channel = null;
                }
                else
                {
                    result = channel.getPaused(ref paused);
                    ERRCHECK(result);
                    result = channel.getPosition(ref ms, FMOD.TIMEUNIT.MS);
                    ERRCHECK(result);

                    statusBar.Text = "Time " + (ms / 1000 / 60) + ":" + (ms / 1000 % 60) + ":" + (ms / 10 % 100) + (paused ? " Paused " : playing ? " Playing" : " Stopped");
                }
            }

            if (sound != null)
            {
                uint percentbuffered = 0;
                bool starving        = false;
                bool busy            = false;

                sound.getOpenState(ref openstate, ref percentbuffered, ref starving, ref busy);

                if (openstate == FMOD.OPENSTATE.ERROR)
                {
                    sound.release();
                    sound   = null;
                    channel = null;
                }
            }

            if (openstate == FMOD.OPENSTATE.ERROR)
            {
                statusBar.Text = "Error occurred or stream ended.  Restarting stream..";
                result         = system.createSound(url, (FMOD.MODE.HARDWARE | FMOD.MODE._2D | FMOD.MODE.CREATESTREAM | FMOD.MODE.NONBLOCKING), ref exinfo, ref sound);
                ERRCHECK(result);
            }

            if (system != null)
            {
                system.update();
            }
        }
示例#13
0
        public static void Update(GameTime gameTime)
        {
            system.update();
            if (IsSongPlaying())
            {
                uint length   = 0;
                uint position = 0;
                CurrentSong.getLength(out length, FMOD.TIMEUNIT.MS);
                Channel.getPosition(out position, FMOD.TIMEUNIT.MS);
                if (position == length)
                {
                    NextSongEvent.Invoke();
                }



                // BPM Method 1 (More research)
                #region Update System & FFT Algorithm

                if (FFTEnabled)
                {
                    // Perform a Fast Fourier Transform

                    int    windowSize = spectrum.Length; //Samples.
                    IntPtr data;
                    uint   fft_length;
                    //hanning bpm
                    MyDSP.setParameterInt((int)FMOD.DSP_FFT.WINDOWTYPE, (int)FMOD.DSP_FFT_WINDOW.HAMMING);
                    MyDSP.setParameterInt((int)FMOD.DSP_FFT.WINDOWSIZE, windowSize);
                    MyDSP.getParameterData((int)FMOD.DSP_FFT.SPECTRUMDATA, out data, out fft_length);
                    MyDSP.getParameterFloat((int)FMOD.DSP_FFT.DOMINANT_FREQ, out higher_freq);
                    fftParameter = (FMOD.DSP_PARAMETER_FFT)Marshal.PtrToStructure(data, typeof(FMOD.DSP_PARAMETER_FFT));

                    #region BPMDetection

                    if (fftParameter.spectrum.Length != 0)
                    {
                        for (int i = 0; i < windowSize; i++)
                        {
                            if (fftParameter.spectrum.Length == 2)
                            {
                                spectrum[i] = (fftParameter.spectrum[0][i] + fftParameter.spectrum[1][i]) / 2f;
                            }
                        }
                    }
                    previousSpectrum = spectrum;
                    #endregion
                }
                #endregion

                // BPM Method 2 (Working! but with small sync bugs)

                float bps = (60f / BPM);
                bpm_elapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;

                if (bpm_elapsed > bps)
                {
                    gotBeat       = true;
                    offBeatDelay += (float)gameTime.ElapsedGameTime.TotalSeconds;
                    if (offBeatDelay > bps)
                    {
                        gotBeat      = false;
                        bpm_elapsed  = 0f;
                        offBeatDelay = 0f;
                    }
                }
            }
            Channel.setVolume(masterVolume * musicVolume);
        }
示例#14
0
        /**
         * called when this sample/effect should render itself, possibly spawning a new
         * playing instance or modifying an already playing instance.
         */
        internal void Render(RenderParams rp, RenderCanvas canvas, List <FMOD.DSP> dspList, int audioRepeats)
        {
#if DEBUG
            Util.Log("Render HID=" + rp.HierarchyID + " T=" + Math.Round(rp.Time, 3) + " AbsT=" + Math.Round(rp.AbsTime, 3) + " A=" + Math.Round(rp.Ampl, 3) + "\n");
#endif
            // check if duration is not exceeded
            if (rp.Time > _soundDuration * ((double)audioRepeats))
            {
                return;
            }

            bool         wasPlaying = _nowPlayingList.ContainsKey(rp.HierarchyID);
            FMOD.Channel channel    = null;
            FMOD.RESULT  r;

            if (wasPlaying)
            {
                channel = _nowPlayingList[rp.HierarchyID];
                // check if still playing now
                bool isPlayingNow = false;
                r = channel.isPlaying(ref isPlayingNow);
                //Util.ERRCHECK(r); // TODO is this needed?
                if (isPlayingNow)
                {   // if so, adapt sample properties only.
                    AdaptChannelSettings(channel, rp, canvas);

                    // check playing time
                    uint playPosMs      = 0;
                    int  idealPlayPosMs = (int)Math.Round(rp.Time * 1000.0);
                    channel.getPosition(ref playPosMs, FMOD.TIMEUNIT.MS);

                    if (Math.Abs(((int)playPosMs) - idealPlayPosMs) > 5000 && idealPlayPosMs >= 0)  // FIXME specify error margin better, somewhere? configurable per sample?
                    {
                        //FIXME HACK enable tracking when needed !!! below.
                        channel.setPosition((uint)idealPlayPosMs, FMOD.TIMEUNIT.MS);
                        playPosMs = (uint)idealPlayPosMs;
                    }
                    // store current pos on canvas
                    if (canvas.TimeMarker == 0)
                    {
                        canvas.TimeMarker = ((double)playPosMs) / 1000.0;
                    }
                }
                else
                {   // if not anymore, remove from list
                    _nowPlayingList.Remove(rp.HierarchyID);
                }
            }
            else
            {                                         // was not playing but should be rendered - hence, initiate playing now
                if (rp.Time < _soundDuration - 0.050) // extra safety margin - do not start if close to end. TODO configurable time?
                {
                    channel = PlaySample(rp, canvas, dspList);
                    channel.setLoopCount(audioRepeats - 1);
                    if (channel != null)
                    {
#if DEBUG
                        Util.Log("Play   HID=" + rp.HierarchyID + " T=" + Math.Round(rp.Time, 3) + " AbsT=" + Math.Round(rp.AbsTime, 3) + " A=" + Math.Round(rp.Ampl, 3) + "\n");
#endif
                        // store playing sound in the table
                        _nowPlayingList[rp.HierarchyID] = channel;
                    }
                    else
                    {
                        Util.Log("Play FAILED rp.H-ID=" + rp.HierarchyID + " rp.Time=" + rp.Time + " rp.AbsTime=" + rp.AbsTime + "\n");
                    }
                }
            }
        }
示例#15
0
        }         // GetSpectrumData

        private void analyzeSoundTrack()
        {
            uint endPosition     = 0u;
            uint currentPosition = 0u;

            // reset all of the counters and such in the frequency band
            foreach (var current in m_mapOfFrequencyBands)
            {
                // clear and allocate new data arrays
                current.Value.reset((uint)(m_sequence.TotalEventPeriods + 1));
            }             // end reset the data arrays

//			m_FMOD_sound.release();
//			m_FMOD_system.close();
//			m_FMOD_system.release();
            FMOD_ERRCHECK(FMOD.Factory.System_Create(ref m_FMOD_system));
            m_FMOD_system.setOutput(FMOD.OUTPUTTYPE.NOSOUND_NRT);

            FMOD_ERRCHECK(m_FMOD_system.init(32, FMOD.INITFLAG.STREAM_FROM_UPDATE, IntPtr.Zero));
            m_FMOD_system.createStream(Path.Combine(Paths.AudioPath, m_sequence.Audio.FileName), (FMOD.MODE) 72u, ref m_FMOD_sound);

            // How much music is there (in ms)
            m_FMOD_sound.getLength(ref endPosition, FMOD.TIMEUNIT.MS);

            // set up a progress bar
            TranscribeDialog transcribeDialog = new TranscribeDialog((int)endPosition);

            transcribeDialog.Show();

            // set up the playback
            FMOD_ERRCHECK(m_FMOD_system.playSound(FMOD.CHANNELINDEX.FREE, m_FMOD_sound, false, ref m_FMOD_channel));

            // while there is more sound to process
            while (currentPosition < endPosition)
            {
                // GET the playback index
                m_FMOD_channel.getPosition(ref currentPosition, FMOD.TIMEUNIT.MS);
                uint currentPeriod = ((uint)currentPosition / (uint)m_sequence.EventPeriod);

                // tell the user where we are in the process
                transcribeDialog.Progress = (int)currentPosition;
                transcribeDialog.Refresh();

                // calculate the frequency bands for the current position
                GetSpectrumData(currentPeriod);

                // which period are we in?
                if (currentPeriod >= m_sequence.TotalEventPeriods)
                {
                    // time to stop
                    break;
                }                 // last period check

                m_FMOD_system.update();
            }             // end while

            // turn it all off
            m_FMOD_channel.stop();
            transcribeDialog.Hide();
            transcribeDialog.Dispose();
            m_FMOD_sound.release();
            m_FMOD_sound = null;
            m_FMOD_system.close();
            m_FMOD_system.release();
            m_FMOD_system = null;
        }         // analyzeSoundTrack