Пример #1
0
        public bool LoadSong(string filename, List <BpmChangeEvent> bpmChanges)
        {
            IsFinished        = false;
            streamHandle      = SDL_mixer.Mix_LoadMUS(filename);
            Globals.BpmEvents = bpmChanges;
            Globals.BpmEvents = Globals.BpmEvents.OrderBy(x => x.StartBeat).ToList();
            for (int i = 0; i < Globals.BpmEvents.Count; i++)
            {
                if (i > 0)
                {
                    Globals.BpmEvents[i].StartSeconds = (Globals.BpmEvents[i].StartBeat - Globals.BpmEvents[i - 1].StartBeat) / Globals.BpmEvents[i - 1].BPM * 60 + Globals.BpmEvents[i - 1].StartSeconds;
                }
            }

            //SongBpm = bpm;
            // Globals.CurrentBpm = Globals.BpmEvents[0].BPM;

            // create hooks here
            SDL_mixer.Mix_HookMusic(MixDelegate, IntPtr.Zero);
            SDL_mixer.Mix_HookMusicFinished(FinishedDelegate);

            // Use the left channel for grabbing this info for now.
            // Is this correct? I'm not sure.
            IntPtr leftChannelMixChunk = SDL_mixer.Mix_GetChunk(leftChannel);

            SDL_mixer.Mix_QuerySpec(out frequency, out format, out channels);

            unsafe
            {
                byte *pLeftChannel = (byte *)leftChannelMixChunk.ToPointer();
                if (pLeftChannel != null)
                {
                    /*
                     *  typedef struct Mix_Chunk {
                     *      int allocated;
                     *      Uint8 *abuf;
                     *      Uint32 alen;
                     *      Uint8 volume;
                     *  } Mix_Chunk;
                     * get the value of alen within this struct
                     * refer to https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer.html#SEC85 for more details
                     */
                    SongLengthBytes = *(pLeftChannel + sizeof(int) + sizeof(byte));
                    SongLengthSec   = GetCurrentSec(SongLengthBytes);
                }
            }
            return(streamHandle == IntPtr.Zero ? false : true);
        }
    public static void Main()
    {
        int    audio_rate     = 44100;
        ushort audio_format   = SDL.AUDIO_S16SYS;
        int    audio_channels = 2;
        int    audio_buffers  = 4096;
        int    audio_volume   = SDL_mixer.MIX_MAX_VOLUME;
        int    timeleft       = 50;

        SDL.SDL_Init(SDL.SDL_INIT_AUDIO);
        if (SDL_mixer.Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0)
        {
            return;
        }
        else
        {
            SDL_mixer.Mix_QuerySpec(out audio_rate, out audio_format, out audio_channels);
        }
        SDL_mixer.Mix_AllocateChannels(32);
        SDL_mixer.Mix_VolumeMusic(audio_volume / 3);

        System.IntPtr music = SDL_mixer.Mix_LoadMUS("Tetris_theme.ogg");
        System.IntPtr wave  = SDL_mixer.Mix_LoadWAV("Meow.ogg");
        SDL_mixer.Mix_FadeInMusic(music, 1, 2000);
        while (SDL_mixer.Mix_PlayingMusic() != 0)
        {
            SDL.SDL_Delay(100);
            timeleft--;
            if (timeleft == 0)
            {
                break;
            }
            if (timeleft == 25)
            {
                SDL_mixer.Mix_PlayChannel(-1, wave, 0);
            }
        }
        SDL_mixer.Mix_FadeOutMusic(2000);
        SDL_mixer.Mix_FreeMusic(music);
        SDL_mixer.Mix_FreeChunk(wave);
        SDL.SDL_Delay(500);
        SDL_mixer.Mix_CloseAudio();
        SDL.SDL_Quit();
    }