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();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Reads the tags from the stream
        /// </summary>
        /// <param name="snd"></param>
        /// <param name="numTags"></param>
        private TagInfo ReadTags(FMOD.Sound snd, int numTags)
        {
            FMOD.RESULT result;
            var         tagInfo = new TagInfo();

            //var tagList = new List<FMOD.TAG>();

            for (var x = 0; x < numTags; x++)
            {
                FMOD.TAG tag;
                //get tag
                result = snd.getTag(null, x, out tag);
                CheckError(result);

                //get the tag info from the song
                SetSongInfo(tag, tagInfo);

                //add to list
                //tagList.Add(tag);
            }

            //return the tag info
            return(tagInfo);
        }
        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();
            }
        }
Esempio n. 4
0
        IEnumerator StreamCR()
        {
            /*
             * FMOD seems to like this
             */
            yield return(new WaitForSeconds(2f));

            this.streamCaught = false;
            this.trackChanged = false;

            for (;;)
            {
                if (this.isPaused)
                {
                    yield return(null);
                }

                if (this.finished)
                {
                    this.Stop();
                    yield break;
                }

                result = system.update();
                ERRCHECK(result, "system.update", false);

                result = sound.getOpenState(out openstate, out bufferFillPercentage, out starving, out deviceBusy);
                ERRCHECK(result, "sound.getOpenState", false);

                LOG(LogLevel.DEBUG, "Stream open state: {0}, buffer fill {1} starving {2} networkBusy {3}", openstate, bufferFillPercentage, starving, deviceBusy);

                if (!this.streamCaught)
                {
                    int c = 0;
                    do
                    {
                        result = system.update();
                        ERRCHECK(result, "system.update", false);

                        result = sound.getOpenState(out openstate, out bufferFillPercentage, out starving, out deviceBusy);
                        ERRCHECK(result, "sound.getOpenState", false);

                        LOG(LogLevel.DEBUG, "Stream open state: {0}, buffer fill {1} starving {2} networkBusy {3}", openstate, bufferFillPercentage, starving, deviceBusy);

                        if (result == FMOD.RESULT.OK && openstate == FMOD.OPENSTATE.READY)
                        {
                            /*
                             * stream caught
                             */
                            FMOD.SOUND_TYPE   _streamType;
                            FMOD.SOUND_FORMAT _streamFormat;
                            int _streamBits;

                            result = sound.getFormat(out _streamType, out _streamFormat, out this.streamChannels, out _streamBits);
                            ERRCHECK(result, "sound.getFormat");

                            float freq; int prio;
                            result = sound.getDefaults(out freq, out prio);
                            ERRCHECK(result, "sound.getDefaults");

                            LOG(LogLevel.INFO, "Stream format {0} {1} {2} channels {3} bits {4} samplerate", _streamType, _streamFormat, this.streamChannels, _streamBits, freq);

                            if (this is AudioStream)
                            {
                                // compensate for the stream samplerate
                                this.GetComponent <AudioSource>().pitch = ((float)(freq * this.streamChannels) / (float)(AudioSettings.outputSampleRate * (int)AudioSettings.speakerMode));
                            }

                            this.StreamStarting((int)freq);

                            this.streamCaught = true;

                            this.isPlaying = true;

                            if (this.OnPlaybackStarted != null)
                            {
                                this.OnPlaybackStarted.Invoke(this.gameObjectName);
                            }

                            break;
                        }
                        else
                        {
                            /*
                             * Unable to stream
                             */
                            if (++c > 60)
                            {
                                if (this.url.StartsWith("http"))
                                {
                                    LOG(LogLevel.ERROR, "Can't start playback. Please make sure that correct audio type of stream is selected and network is reachable.");
#if UNITY_EDITOR
                                    LOG(LogLevel.ERROR, "If everything seems to be ok, restarting the editor often helps while having trouble connecting to especially OGG streams.");
#endif
                                }
                                else
                                {
                                    LOG(LogLevel.ERROR, "Can't start playback. Unrecognized audio type.");
                                }

                                this.Stop();

                                yield break;
                            }
                        }

                        yield return(new WaitForSeconds(0.1f));
                    } while (result != FMOD.RESULT.OK || openstate != FMOD.OPENSTATE.READY);
                }

                if (this.StreamStarving())
                {
                    LOG(LogLevel.WARNING, "Stream buffer starving - stopping playback");

                    this.Stop();

                    yield break;
                }

                FMOD.TAG streamTag;

                /*
                 *  Read any tags that have arrived, this could happen if a radio station switches
                 *  to a new song.
                 */
                while (sound.getTag(null, -1, out streamTag) == FMOD.RESULT.OK)
                {
                    if (!this.trackChanged)
                    {
                        this.tags         = new string[tagcount];
                        this.tagindex     = 0;
                        this.trackChanged = true;
                    }

                    if (streamTag.datatype == FMOD.TAGDATATYPE.STRING)
                    {
                        string tagData = Marshal.PtrToStringAnsi(streamTag.data, (int)streamTag.datalen);
                        tags[tagindex] = string.Format("{0} = {1}", streamTag.name, tagData);
                        tagindex       = (tagindex + 1) % tagcount;
                    }
                    else if (streamTag.type == FMOD.TAGTYPE.FMOD)
                    {
                        /* When a song changes, the samplerate may also change, so compensate here. */
                        if (streamTag.name == "Sample Rate Change")
                        {
                            // TODO: actual float and samplerate change test - is there a way to test this ?

                            float defFrequency;
                            int   defPriority;
                            result = sound.getDefaults(out defFrequency, out defPriority);
                            ERRCHECK(result, "sound.getDefaults");

                            LOG(LogLevel.INFO, "Stream samplerate change from {0}", defFrequency);

                            // float frequency = *((float*)streamTag.data);
                            float[] frequency = new float[1];
                            Marshal.Copy(streamTag.data, frequency, 0, sizeof(float));

                            LOG(LogLevel.INFO, "Stream samplerate change to {0}", frequency[0]);

                            result = sound.setDefaults(frequency[0], defPriority);
                            ERRCHECK(result, "sound.setDefaults");

                            /*
                             * need to restart audio when changing samplerate..
                             */
                            if (this is AudioStream)
                            {
                                // compensate for the stream samplerate
                                this.GetComponent <AudioSource>().pitch = ((float)(frequency[0] * this.streamChannels) / (float)(AudioSettings.outputSampleRate * (int)AudioSettings.speakerMode));
                            }
                        }
                    }
                }

                if (this.trackChanged)
                {
                    // TODO: track changed event
                    LOG(LogLevel.INFO, "Track changed {0}", this.tags[0]); // print tags[0] for info only - might be anything
                    this.trackChanged = false;
                }

                yield return(null);
            }
        }
Esempio n. 5
0
        private void ReadTags_Load(object sender, System.EventArgs e)
        {
            FMOD.TAG tag = new FMOD.TAG();
            int      numtags = 0, numtagsupdated = 0;
            uint     version = 0;

            FMOD.RESULT result;

            /*
             *  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(100, FMOD.INITFLAGS.NORMAL, (IntPtr)null);
            ERRCHECK(result);

            /*
             *  Open the specified file. Use FMOD_CREATESTREAM and FMOD_DONTPREBUFFER so it opens quickly
             */
            result = system.createSound("../../../../../examples/media/wave.mp3", (FMOD.MODE.SOFTWARE | FMOD.MODE._2D | FMOD.MODE.CREATESTREAM | FMOD.MODE.OPENONLY), ref sound);
            ERRCHECK(result);

            /*
             *  Read and display all tags associated with this file
             */
            for (;;)
            {
                /*
                 *  An index of -1 means "get the first tag that's new or updated".
                 *  If no tags are new or updated then getTag will return FMOD_ERR_TAGNOTFOUND.
                 *  This is the first time we've read any tags so they'll all be new but after we've read them,
                 *  they won't be new any more.
                 */
                if (sound.getTag(null, -1, ref tag) != FMOD.RESULT.OK)
                {
                    break;
                }
                if (tag.datatype == FMOD.TAGDATATYPE.STRING)
                {
                    listBox.Items.Add(tag.name + " = " + Marshal.PtrToStringAnsi(tag.data) + " (" + tag.datalen + " bytes)");
                }
                else
                {
                    listBox.Items.Add(tag.name + " = <binary> (" + tag.datalen + " bytes)");
                }
            }

            listBox.Items.Add(" ");

            /*
             *  Read all the tags regardless of whether they're updated or not. Also show the tag type.
             */
            result = sound.getNumTags(ref numtags, ref numtagsupdated);
            ERRCHECK(result);
            for (int count = 0; count < numtags; count++)
            {
                string tagtext = null;

                result = sound.getTag(null, count, ref tag);
                ERRCHECK(result);

                switch (tag.type)
                {
                case FMOD.TAGTYPE.UNKNOWN:
                    tagtext = "FMOD_TAGTYPE_UNKNOWN  ";
                    break;

                case FMOD.TAGTYPE.ID3V1:
                    tagtext = "FMOD_TAGTYPE_ID3V1  ";
                    break;

                case FMOD.TAGTYPE.ID3V2:
                    tagtext = "FMOD_TAGTYPE_ID3V2  ";
                    break;

                case FMOD.TAGTYPE.VORBISCOMMENT:
                    tagtext = "FMOD_TAGTYPE_VORBISCOMMENT  ";
                    break;

                case FMOD.TAGTYPE.SHOUTCAST:
                    tagtext = "FMOD_TAGTYPE_SHOUTCAST  ";
                    break;

                case FMOD.TAGTYPE.ICECAST:
                    tagtext = "FMOD_TAGTYPE_ICECAST  ";
                    break;

                case FMOD.TAGTYPE.ASF:
                    tagtext = "FMOD_TAGTYPE_ASF  ";
                    break;

                case FMOD.TAGTYPE.FMOD:
                    tagtext = "FMOD_TAGTYPE_FMOD  ";
                    break;

                case FMOD.TAGTYPE.USER:
                    tagtext = "FMOD_TAGTYPE_USER  ";
                    break;
                }

                if (tag.datatype == FMOD.TAGDATATYPE.STRING)
                {
                    tagtext += (tag.name + " = " + Marshal.PtrToStringAnsi(tag.data) + "(" + tag.datalen + " bytes)");
                }
                else
                {
                    tagtext += (tag.name + " = ??? (" + tag.datalen + " bytes)");
                }

                listBox.Items.Add(tagtext);
            }

            listBox.Items.Add(" ");

            /*
             *   Find a specific tag by name. Specify an index > 0 to get access to multiple tags of the same name.
             */
            result = sound.getTag("ARTIST", 0, ref tag);
            ERRCHECK(result);
            listBox.Items.Add(tag.name + " = " + Marshal.PtrToStringAnsi(tag.data) + " (" + tag.datalen + " bytes)");
        }
Esempio n. 6
0
        /// <summary>
        /// Die Informationen (TOC) einer CD auslesen
        /// </summary>
        public CDInfo ReadCDInformation(char driveLetter)
        {
            if (Settings.Current.UseMCI)
            {
                return(ReadCDInformationMCI(driveLetter));
            }

            CDInfo mciCDInfo = ReadCDInformationMCI(driveLetter);

            if (mciCDInfo.Tracks.Any(x => x.TrackType != TrackType.Audio))  // Bei einer Daten-CD nehmen wir MCI
            {
                return(mciCDInfo);
            }

            string drive = string.Format("{0}:", driveLetter);

            FMOD.Sound cdSound = null;

            FMOD.RESULT result = system.createStream(drive, FMOD.MODE.OPENONLY, ref cdSound);
            if (result != FMOD.RESULT.OK || cdSound == null)
            {
                return(mciCDInfo);
            }

            int numberOfTracks = 0;

            cdSound.getNumSubSounds(ref numberOfTracks);

            FMOD.CDTOC cdToc = new FMOD.CDTOC();

            int numtags        = 0;
            int numtagsupdated = 0;

            cdSound.getNumTags(ref numtags, ref numtagsupdated);

            for (int i = 0; i < numtags; i++)
            {
                FMOD.TAG tag = new FMOD.TAG();
                if (cdSound.getTag(null, i, ref tag) != FMOD.RESULT.OK)
                {
                    break;
                }
                if (tag.datatype == FMOD.TAGDATATYPE.CDTOC)
                {
                    cdToc = (FMOD.CDTOC)Marshal.PtrToStructure(tag.data, typeof(FMOD.CDTOC));
                    break;
                }
            }

            CDInfo cdInfo = new CDInfo();

            int trackNr = 0;

            for (; trackNr < numberOfTracks; trackNr++)
            {
                CDInfoTrack track = new CDInfoTrack();

                track.StartTime = (((cdToc.min[trackNr] * 60 + cdToc.sec[trackNr]) * 75 + cdToc.frame[trackNr]) * 1000 + 74) / 75;

                if (trackNr > 0)
                {
                    cdInfo.Tracks[trackNr - 1].Length = track.StartTime - cdInfo.Tracks[trackNr - 1].StartTime;
                }
                track.TrackType = TrackType.Audio;
                cdInfo.Tracks.Add(track);
            }

            cdInfo.TotalLength = (((cdToc.min[numberOfTracks] * 60 + cdToc.sec[numberOfTracks]) * 75 + cdToc.frame[numberOfTracks]) * 1000 + 74) / 75;
            cdInfo.Tracks[trackNr - 1].Length = cdInfo.TotalLength - cdInfo.Tracks[trackNr - 1].StartTime;

            cdSound.release();

            /*StringBuilder volumeName = new StringBuilder(1000);
             * StringBuilder fileSystemNameBuffer = new StringBuilder(1000);
             * uint serialNumber = 0;
             * uint maximumComponentLength = 0;
             * uint fileSystemFlags = 0;
             * GetVolumeInformation(drive, volumeName, 1000, ref serialNumber, ref maximumComponentLength, ref fileSystemFlags, fileSystemNameBuffer, 1000);
             *
             * cdInfo.Identity = serialNumber.ToString();*/

            cdInfo.Identity = mciCDInfo.Identity;

            return(cdInfo);
        }
Esempio n. 7
0
        public static void GetSoundTags(string filename, SoundFileInformation soundFileInfo)
        {
            FMOD.Sound  sound = null;
            FMOD.RESULT res   = SoundEngine.Instance.system.createStream(filename, FMOD.MODE.OPENONLY, ref sound);

            if (sound != null)
            {
                int numtags  = 0;
                int num2tags = 0;
                sound.getNumTags(ref numtags, ref num2tags);

                for (int i = 0; i < numtags; i++)
                {
                    FMOD.TAG tag2 = new FMOD.TAG();
                    res = sound.getTag(null, i, ref tag2);
                    if (res != FMOD.RESULT.ERR_TAGNOTFOUND)
                    {
                        string tagName = tag2.name.ToLower();
                        if (tagName == "title")
                        {
                            soundFileInfo.Title = Marshal.PtrToStringAnsi(tag2.data);
                        }

                        if (tagName == "author" || tagName == "artist")
                        {
                            soundFileInfo.Artist = Marshal.PtrToStringAnsi(tag2.data);
                        }

                        if (tagName == "wm/tracknumber" || tagName == "tracknumber")
                        {
                            if (tag2.datatype == FMOD.TAGDATATYPE.STRING)
                            {
                                soundFileInfo.TrackNumber = Big3.Hitbase.Miscellaneous.Misc.Atoi(Marshal.PtrToStringAnsi(tag2.data));
                            }

                            if (tag2.datatype == FMOD.TAGDATATYPE.INT)
                            {
                                soundFileInfo.TrackNumber = Convert.ToInt32(Marshal.ReadInt32(tag2.data));
                            }
                        }

                        if (tagName == "wm/composer")
                        {
                            soundFileInfo.Composer = Marshal.PtrToStringAnsi(tag2.data);
                        }

                        if (tagName == "wm/albumtitle" || tagName == "album")
                        {
                            soundFileInfo.Album = Marshal.PtrToStringAnsi(tag2.data);
                        }

                        if (tagName == "wm/year")
                        {
                            soundFileInfo.Year = Big3.Hitbase.Miscellaneous.Misc.Atoi(Marshal.PtrToStringAnsi(tag2.data));
                        }

                        if (tagName == "wm/genre" || tagName == "genre")
                        {
                            soundFileInfo.Genre = Marshal.PtrToStringAnsi(tag2.data);
                        }
                    }
                }

                sound.release();
            }
        }