示例#1
0
文件: Song.cs 项目: empika/MonoGame
 internal Song(string fileName)
 {
     FilePath = fileName;
     initializeMixer();
     INTERNAL_mixMusic = SDL_mixer.Mix_LoadMUS(fileName);
     IsDisposed        = false;
 }
示例#2
0
 internal Song(string fileName)
 {
     FilePath = fileName;
     Name     = Path.GetFileNameWithoutExtension(FilePath);
     initializeMixer();
     INTERNAL_mixMusic = SDL_mixer.Mix_LoadMUS(fileName);
     IsDisposed        = false;
 }
示例#3
0
文件: Music.cs 项目: Gabryx64/GEALOS
        public override void load(string path)
        {
            music = SDL_mixer.Mix_LoadMUS(path);

            if (music == IntPtr.Zero)
            {
                Console.WriteLine($"There was an issue loading the music. {SDL.SDL_GetError()}");
                Environment.Exit(-1);
            }
        }
示例#4
0
文件: SDLSounds.cs 项目: Jebeli/Tiles
        public override Music GetMusic(string fileId, IFileResolver fileResolver)
        {
            IntPtr chunk = SDL_mixer.Mix_LoadMUS(fileResolver.Resolve(fileId));

            if (!chunk.Equals(IntPtr.Zero))
            {
                return(new SDLMusic(fileId, chunk));
            }
            return(null);
        }
示例#5
0
    /// <summary>
    /// Loads a music file from the Assets directory. Supports the following formats: WAV, OGG, MP3, FLAC.
    /// </summary>
    /// <param name="path">The path to the music file, relative to the Assets directory.</param>
    public static Music LoadMusic(string path)
    {
        IntPtr handle = SDL_mixer.Mix_LoadMUS(GetAssetPath(path));

        if (handle == IntPtr.Zero)
        {
            throw new Exception("Failed to load music.");
        }

        return(new Music(handle));
    }
示例#6
0
        public static Music Load(string path)
        {
            var sdlMusic = SDL_mixer.Mix_LoadMUS(path);

            if (sdlMusic == IntPtr.Zero)
            {
                throw new InvalidOperationException($"Cant load music '{path}': {SDL.SDL_GetError()}");
            }

            return(new Music(sdlMusic));
        }
示例#7
0
        private void LoadAndPrepareResources()
        {
            music_mp3 = SDL_mixer.Mix_LoadMUS("ringtone.mp3");
            if (music_mp3 == IntPtr.Zero)
            {
                Console.WriteLine("Failed to load mp3! SDL_mixer error {0}", SDL.SDL_GetError());
            }

            SDL_mixer.Mix_PlayMusic(music_mp3, -1);

            music_wav = SDL_mixer.Mix_LoadWAV("beat.wav");
            if (music_wav == IntPtr.Zero)
            {
                Console.WriteLine("Failed to load wav! SDL_mixer error {0}", SDL.SDL_GetError());
            }

            font = SDL_ttf.TTF_OpenFont("lazy.ttf", 64);
            if (font == IntPtr.Zero)
            {
                Console.WriteLine("Failed to load lazy font! SDL_ttf Error: {0}", SDL.SDL_GetError());
            }

            //Render text surface
            var textColor   = new SDL.SDL_Color();
            var textSurface = SDL_ttf.TTF_RenderText_Solid(font, "Hello SDL2 World!", textColor);

            if (textSurface == IntPtr.Zero)
            {
                Console.WriteLine("Unable to render text surface! SDL_ttf Error: {0}", SDL.SDL_GetError());
            }

            texture_text = SDL.SDL_CreateTextureFromSurface(renderer, textSurface);
            if (texture_text == IntPtr.Zero)
            {
                Console.WriteLine("Unable to create texture from rendered text! SDL Error: {0}", SDL.SDL_GetError());
            }
            s_text = Marshal.PtrToStructure <SDL.SDL_Surface>(textSurface);

            IntPtr image_png = SDL_image.IMG_Load("transparent.png");

            if (image_png == IntPtr.Zero)
            {
                Console.WriteLine("Unable to load image! SDL_image Error: {0}", SDL.SDL_GetError());
            }

            s_png = Marshal.PtrToStructure <SDL.SDL_Surface>(image_png);
            SDL.SDL_SetColorKey(image_png, (int)SDL.SDL_bool.SDL_TRUE, SDL.SDL_MapRGB(s_png.format, 0, 0xFF, 0xFF));

            texture_png = SDL.SDL_CreateTextureFromSurface(renderer, image_png);
            if (texture_png == IntPtr.Zero)
            {
                Console.WriteLine("Unable to create texture from png! SDL Error: {0}", SDL.SDL_GetError());
            }
        }
示例#8
0
        static bool LoadMedia()
        {
            //Loading success flag
            bool success = true;

            if (!_PromptTexture.LoadFromFile("prompt.png"))
            {
                Console.WriteLine("Failed to load!");
                success = false;
            }

            //Load music
            _Music = SDL_mixer.Mix_LoadMUS("beat.wav");
            if (_Music == IntPtr.Zero)
            {
                Console.WriteLine("Failed to load! {0}", SDL.SDL_GetError());
                success = false;
            }

            //Load sound effects
            _Scratch = SDL_mixer.Mix_LoadWAV("scratch.wav");
            if (_Scratch == IntPtr.Zero)
            {
                Console.WriteLine("Failed to load! {0}", SDL.SDL_GetError());
                success = false;
            }

            _High = SDL_mixer.Mix_LoadWAV("high.wav");
            if (_High == IntPtr.Zero)
            {
                Console.WriteLine("Failed to load! {0}", SDL.SDL_GetError());
                success = false;
            }

            _Medium = SDL_mixer.Mix_LoadWAV("medium.wav");
            if (_Medium == IntPtr.Zero)
            {
                Console.WriteLine("Failed to load! {0}", SDL.SDL_GetError());
                success = false;
            }

            _Low = SDL_mixer.Mix_LoadWAV("low.wav");
            if (_Low == IntPtr.Zero)
            {
                Console.WriteLine("Failed to load! {0}", SDL.SDL_GetError());
                success = false;
            }

            return(success);
        }
示例#9
0
 public static IntPtr Music(string path)
 {
     if (music == null)
     {
         music = new Dictionary <string, IntPtr>();
     }
     if (!music.ContainsKey(path))
     {
         var audioPath = LoadPath(path);
         var newAudio  = SDL_mixer.Mix_LoadMUS(Assets.LoadPath(audioPath));
         music.Add(path, newAudio);
     }
     return(music[path]);
 }
示例#10
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();
    }
示例#12
0
 static int prechargerMusiques()
 {
     try
     {
         musiques = new Dictionary <String, IntPtr>()
         {
             // écran jeu
             { "jeu_1", SDL_mixer.Mix_LoadMUS(@"res\musiques\jeu_1.mp3") },
             { "jeu_2", SDL_mixer.Mix_LoadMUS(@"res\musiques\jeu_2.mp3") },
             // écran titre
             { "titre", SDL_mixer.Mix_LoadMUS(@"res\musiques\titre.mp3") }
         };
         return(0);
     }
     catch
     {
         return(1);
     }
 }
示例#13
0
    static void Main(string[] args)
    {
        FFI.CreateContext("Little Polygon C# Demo", 3 * 320, 3 * 115, "");

        // PLAY SOME MUSIC
        var mus = SDL_mixer.Mix_LoadMUS("song.mid");

        if (mus != IntPtr.Zero)
        {
            SDL_mixer.Mix_PlayMusic(mus, -1);
        }

        // BASIC EVENT LOOP
        var window = SDL.SDL_GL_GetCurrentWindow();

        while (!gDone)
        {
            HandleEvents();
            FFI.ClearScreen();
            SDL.SDL_GL_SwapWindow(window);
        }

        FFI.DestroyContext();
    }
示例#14
0
 private void LoadAndPlayMusic()
 {
     MusicPointer = SDL_mixer.Mix_LoadMUS(MusicFilePath);
     SDL_mixer.Mix_FadeInMusic(MusicPointer.Value, -1, 1000);
     SDL_mixer.Mix_VolumeMusic(MusicVolume);
 }
示例#15
0
        public Music(string File)
        {
            myPtr = SDL_mixer.Mix_LoadMUS(File);

            CheckPtr();
        }
示例#16
0
 public MixerMusic(string filePath)
 {
     mixerMusicPtr = SDL_mixer.Mix_LoadMUS(filePath);
 }
示例#17
0
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Please pass in a mp3 file");
            }

            if (SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING) < 0)
            {
                Console.WriteLine(SDL.SDL_GetError());

                Environment.Exit(1);
            }

            if (SDL_mixer.Mix_Init(SDL_mixer.MIX_InitFlags.MIX_INIT_MP3) < 0)
            {
                Console.WriteLine(SDL.SDL_GetError());

                Environment.Exit(1);
            }

            IntPtr window = SDL.SDL_CreateWindow("Yay", SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED, 1024, 600, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN);

            if (window == IntPtr.Zero)
            {
                Console.WriteLine(SDL.SDL_GetError());

                Console.WriteLine("Sad: Window");

                Environment.Exit(1);
            }

            IntPtr renderer = SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED | SDL.SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC);

            if (renderer == IntPtr.Zero)
            {
                Console.WriteLine(SDL.SDL_GetError());

                Console.WriteLine("Sad: Renderer");

                Environment.Exit(1);
            }

            if (SDL_mixer.Mix_OpenAudio(44100, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 2048) < 0)
            {
                Console.WriteLine(SDL.SDL_GetError());

                Console.WriteLine("Sad: Mixer");

                Environment.Exit(1);
            }

            bool mp3Done = false;

            IntPtr joy = SDL_mixer.Mix_LoadMUS(args[0]);

            if (joy == IntPtr.Zero)
            {
                Console.WriteLine(SDL.SDL_GetError());

                Console.WriteLine("Sad: Mix_LoadMUS");

                Environment.Exit(1);
            }

            MPGImport.mpg123_init();

            Int32 err = 0;

            GCHandle handleErr = GCHandle.Alloc(err, GCHandleType.Pinned);

            IntPtr ptrErr = GCHandle.ToIntPtr(handleErr);

            IntPtr handle_mpg = MPGImport.mpg123_new(null, ptrErr);
            int    x          = MPGImport.mpg123_open(handle_mpg, args[0]);

            handleErr.Free();

            MPGImport.mpg123_format_none(handle_mpg);
            MPGImport.mpg123_format(handle_mpg, 44100, 2, (int)MPGImport.mpg123_enc_enum.MPG123_ENC_SIGNED_16);

            int FrameSize = MPGImport.mpg123_outblock(handle_mpg);

            byte[] Buffer        = new byte[FrameSize];
            int    lengthSamples = MPGImport.mpg123_length(handle_mpg);

            if (SDL_mixer.Mix_PlayMusic(joy, 0) < 0)
            {
                Console.WriteLine(SDL.SDL_GetError());

                Console.WriteLine("Sad: Mix_PlayMusic");

                Environment.Exit(1);
            }

            object fun = new object();

            List <Int16> LeftChannels  = new List <Int16>();
            List <Int16> RightChannels = new List <Int16>();

            SDL_mixer.Mix_HookMusic((uData, stream, len) => {
                byte[] mixData       = new byte[len];
                byte[] managedStream = new byte[len];

                Marshal.Copy(stream, managedStream, 0, len);

                IntPtr done = IntPtr.Zero;

                if (MPGImport.mpg123_read(handle_mpg, managedStream, len, out done) is int hello)
                {
                    if (hello == -12)
                    {
                        mp3Done = true;
                    }
                    else if (hello == 0)
                    {
                        // foreach (var b in managedStream.Select((value, idx) => (value, idx)).Where((value, idx) => idx % 4 == 0)) {
                        Int16 leftChannel;
                        Int16 rightChannel;

                        byte[] datum = new byte[2];

                        datum[0]    = managedStream[0];
                        datum[1]    = managedStream[1];
                        leftChannel = BitConverter.ToInt16(datum, 0);

                        datum[0]     = managedStream[2];
                        datum[1]     = managedStream[3];
                        rightChannel = BitConverter.ToInt16(datum, 0);

                        leftChannel  = (Int16)Math.Sqrt(leftChannel);
                        rightChannel = (Int16)Math.Sqrt(rightChannel);

                        lock (fun) {
                            LeftChannels.Add(leftChannel);
                            RightChannels.Add(rightChannel);
                        }
                        // }
                    }
                }

                Marshal.Copy(managedStream, 0, stream, len);
            }, IntPtr.Zero);

            SDL.SDL_Event e;
            bool          quit = false;

            while (!quit)
            {
                while (SDL.SDL_WaitEventTimeout(out e, 10) != 0)
                {
                    switch (e.type)
                    {
                    case SDL.SDL_EventType.SDL_KEYDOWN:
                        switch (e.key.keysym.sym)
                        {
                        case SDL.SDL_Keycode.SDLK_ESCAPE:
                            quit = true;

                            break;
                        }

                        break;

                    case SDL.SDL_EventType.SDL_QUIT:
                        quit = true;

                        break;
                    }
                }

                if (mp3Done)
                {
                    quit = true;
                }
                else
                {
                    SDL.SDL_RenderClear(renderer);
                    SDL.SDL_SetRenderDrawColor(renderer, 218, 112, 214, 255);

                    // 40 is perfect.. ;)
                    lock (fun) {
                        foreach (var channel in LeftChannels.Select((value, idx) => (value, idx)))
                        {
                            if (channel.idx > 40)
                            {
                                continue;
                            }

                            SDL.SDL_Rect rect = new SDL.SDL_Rect()
                            {
                                x = channel.idx * 20,
                                y = 300,
                                w = 15,
                                h = -channel.value
                            };

                            SDL.SDL_RenderDrawRect(renderer, ref rect);
                        }

                        if (LeftChannels.Count > 40)
                        {
                            LeftChannels.RemoveAt(0);
                        }

                        foreach (var channel in RightChannels.Select((value, idx) => (value, idx)))
                        {
                            if (channel.idx > 40)
                            {
                                continue;
                            }

                            SDL.SDL_Rect rect = new SDL.SDL_Rect()
                            {
                                x = channel.idx * 20,
                                y = 315,
                                w = 15,
                                h = channel.value
                            };

                            SDL.SDL_RenderDrawRect(renderer, ref rect);
                        }

                        if (RightChannels.Count > 40)
                        {
                            RightChannels.RemoveAt(0);
                        }
                    }

                    SDL.SDL_Rect r = new SDL.SDL_Rect()
                    {
                        x = 0,
                        y = 300,
                        w = 1024,
                        h = 10
                    };

                    // SDL.SDL_RenderClear(renderer);
                    // SDL.SDL_SetRenderDrawColor(renderer, 218, 112, 214, 255);
                    SDL.SDL_RenderDrawRect(renderer, ref r);

                    SDL.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
                    SDL.SDL_RenderPresent(renderer);
                }
            }

            SDL_mixer.Mix_FreeMusic(joy);

            SDL.SDL_Quit();
        }