示例#1
0
文件: Sound.cs 项目: rkhapov/simple3d
 public void Play(int loopsCount)
 {
     if (SDL_mixer.Mix_PlayChannel(-1, chunk, loopsCount) < 0)
     {
         throw new NotImplementedException($"Cant play chunk: {SDL.SDL_GetError()}");
     }
 }
示例#2
0
文件: Sound.cs 项目: Gabryx64/GEALOS
 public override void play()
 {
     if (SDL_mixer.Mix_PlayChannel(-1, sound, 0) == -1)
     {
         Console.WriteLine($"There was an issue playing the sound. {SDL.SDL_GetError()}");
         Environment.Exit(-1);
     }
 }
示例#3
0
 protected void PlayChunk(int volume, int loops = 0)
 {
     SetVolume(volume); // TODO only if volume has changed ?
     Channel = SDL_mixer.Mix_PlayChannel(-1, AudioChunk, loops);
     if (Channel < 0)
     {
         Console.WriteLine($"ERROR Mix_PlayChannel: {AudioFilename}");
     }
 }
        public void PlaySong()
        {
            if (music == IntPtr.Zero)
            {
                return;
            }

            musicChannel = SDL_mixer.Mix_PlayChannel(-1, music, 1);
            //SDL_mixer.Mix_Volume(musicChannel, 64); // TODO: Customizable volume
        }
示例#5
0
        public int PlayChannel(int Channel = -1, int Loops = 0)
        {
            int Result = SDL_mixer.Mix_PlayChannel(Channel, myPtr, Loops);

            if (Result == -1)
            {
                throw new SDLErrorException(Result);
            }

            return(Result);
        }
        public int PlaySfx(byte[] data)
        {
            if (data == null || data.Length == 0)
            {
                return(-1);
            }

            var sound   = SDL_mixer.Mix_QuickLoad_WAV(data);
            var channel = SDL_mixer.Mix_PlayChannel(-1, sound, 0);

            //SDL_mixer.Mix_Volume(channel, 64); // TODO: Customizable volume
            return(channel);
        }
示例#7
0
        internal static void JouerSon(String son)
        {
            int id_canal = sons.Keys.ToList().IndexOf(son);

            // trop de canaux utilisés
            if (id_canal == -1)
            {
                return;
            }

            // Son déjà joué
            if (SDL_mixer.Mix_Playing(id_canal) > 0)
            {
                return;
            }

            SDL_mixer.Mix_PlayChannel(id_canal, sons[son], 0);
        }
    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();
    }
示例#9
0
文件: SDLSounds.cs 项目: Jebeli/Tiles
        protected override void Play(Playback play)
        {
            int  channel    = -1;
            bool setChannel = false;

            if (!play.VirtualChannel.Equals(Sound.GLOBAL_VIRTUAL_CHANNEL))
            {
                if (channels.TryGetValue(play.VirtualChannel, out int vc))
                {
                    SDL_mixer.Mix_HaltChannel(vc);
                    channels.Remove(play.VirtualChannel);
                }
                setChannel = true;
            }
            channel = SDL_mixer.Mix_PlayChannel(-1, play.Sound.GetChunk(), play.Loop ? -1 : 0);
            if (channel == -1)
            {
                Logger.Error("Sound", $"Failed to play sound '{play.Sound}', no more channels available");
            }
            else
            {
                SDL_mixer.Mix_ChannelFinished(channelFinished);
                Logger.Info("Sound", $"Playing sound '{play.Sound}' on channel {channel} ({play.VirtualChannel})");
            }
            byte dist;

            if (play.Location.X != 0 || play.Location.Y != 0)
            {
                float v = 255.0f * (CalcDist(lastPos, play.Location) / soundFallOff);
                v    = Math.Min(Math.Max(v, 0.0f), 255.0f);
                dist = (byte)v;
            }
            else
            {
                dist = 0;
            }
            SetChannelPosition(channel, 0, dist);
            if (setChannel)
            {
                channels[play.VirtualChannel] = channel;
            }
            playback[channel] = play;
        }
示例#10
0
        // Sound methods (for general sounds)
        public void PlaySound(IMixerSound sound, int channel = -1, bool propagateErrors = false)
        {
            int ret = -1;

            if (sound is MixerSound)
            {
                ret = SDL_mixer.Mix_PlayChannel(channel, sound.GetPointer(), 0);
            }
            else if (sound is MixerMusic)
            {
                ret = SDL_mixer.Mix_PlayMusic(sound.GetPointer(), 1);
            }
            else
            {
                throw new MixerException("Cannot play unknown object");
            }
            if (propagateErrors && ret == -1)
            {
                throw new MixerException();
            }
        }
示例#11
0
        public int LoopSound(IMixerSound sound, int channel = -1)
        {
            int ret = -1;

            if (sound is MixerSound)
            {
                ret = SDL_mixer.Mix_PlayChannel(channel, sound.GetPointer(), -1);
            }
            else if (sound is MixerMusic)
            {
                ret = SDL_mixer.Mix_PlayMusic(sound.GetPointer(), -1);
            }
            else
            {
                throw new MixerException("Cannot play unknown object");
            }
            if (ret == -1)
            {
                throw new MixerException();
            }
            return(ret);
        }
示例#12
0
 public void PlaySound(IntPtr mySound)
 {
     SDL_mixer.Mix_PlayChannel(-1, mySound, 0);
 }
示例#13
0
        /**
         * Used to start the GUI event loop using the SDL window to poll for events. When an event is
         * received, the keyboard state is read and the OnTick event is fired. After the event completes
         * a frame and/or audio can be rendered/played based on the values set in the OnTick event args.
         *
         * This is a BLOCKING call; the event loop will continue to be polled until (1) the user uses
         * the platform specific key combination or GUI action to close the window or (2) the OnTick
         * event arguments has ShouldQuit set to true.
         */
        public void StartLoop()
        {
            // Used to keep track of the time elapsed in each loop iteration. This is used to
            // notify the OnTick handlers so they can update their simulation, as well as throttle
            // the update loop to targetTicskHz if needed.
            var stopwatch = new Stopwatch();

            // Structure used to pass data to and from the OnTick handlers. We initialize it once
            // outside of the loop to avoid eating a ton of memory putting GC into a tailspin.
            var tickEventArgs = new GUITickEventArgs();

            // The SDL event polled for in each iteration of the loop.
            SDL.SDL_Event sdlEvent;

            while (true)
            {
                stopwatch.Restart();

                tickEventArgs.KeyDown     = null;
                tickEventArgs.ShouldBreak = false;

                while (SDL.SDL_PollEvent(out sdlEvent) != 0)
                {
                    switch (sdlEvent.type)
                    {
                    // e.g. Command + Q, ALT+F4, or clicking X...
                    case SDL.SDL_EventType.SDL_QUIT:
                        // Break out of the SDL event loop, which will close the program.
                        return;

                    case SDL.SDL_EventType.SDL_KEYDOWN:
                        tickEventArgs.KeyDown = sdlEvent.key.keysym.sym;
                        UpdateKeys(tickEventArgs, sdlEvent.key.keysym.sym, true);

                        // If the break/pause or 9 key is pressed, set a flag indicating the
                        // emulator's should activate the interactive debugger.
                        if (sdlEvent.key.keysym.sym == SDL.SDL_Keycode.SDLK_PAUSE ||
                            sdlEvent.key.keysym.sym == SDL.SDL_Keycode.SDLK_9)
                        {
                            tickEventArgs.ShouldBreak = true;
                        }

                        break;

                    case SDL.SDL_EventType.SDL_KEYUP:
                        UpdateKeys(tickEventArgs, sdlEvent.key.keysym.sym, false);
                        break;
                    }
                }

                // Update the event arguments that will be sent with the event handler.

                tickEventArgs.ShouldRender     = false;
                tickEventArgs.ShouldPlaySounds = false;
                tickEventArgs.SoundEffects.Clear();

                // Delegate out to the event handler so work can be done.
                if (OnTick != null)
                {
                    OnTick(tickEventArgs);
                }

                // We only want to re-render if the frame buffer has changed since last time because
                // the SDL_RenderPresent method is relatively expensive.
                if (tickEventArgs.ShouldRender && tickEventArgs.FrameBuffer != null)
                {
                    // Render screen from the updated the frame buffer.
                    // NOTE: The electron beam scans from left to right, starting in the upper left corner
                    // of the CRT when it is in 4:3 (landscape), which is how the framebuffer is stored.
                    // However, since the CRT in the cabinet is rotated left (-90 degrees) to show the game
                    // in 3:4 (portrait) we need to perform the rotation of points below by starting in the
                    // bottom left corner of the window and drawing upwards, ending on the top right.

                    // Clear the screen.
                    SDL.SDL_SetRenderDrawColor(_renderer, 0, 0, 0, 0);
                    SDL.SDL_RenderClear(_renderer);

                    var bits = new System.Collections.BitArray(tickEventArgs.FrameBuffer);

                    var x = 0;
                    var y = SpaceInvaders.RESOLUTION_WIDTH - 1;

                    for (var i = 0; i < bits.Length; i++)
                    {
                        if (bits[i])
                        {
                            // The CRT is black/white and the framebuffer is 1-bit per pixel.
                            // A transparent overlay added "colors" to areas of the CRT. These
                            // are the approximate y locations of each area/color of the overlay:

                            if (y >= 182 && y <= 223)
                            {
                                SDL.SDL_SetRenderDrawColor(_renderer, 0, 255, 0, 255); // Green - player and shields
                            }
                            else if (y >= 33 && y <= 55)
                            {
                                SDL.SDL_SetRenderDrawColor(_renderer, 255, 0, 0, 255); // Red - UFO
                            }
                            else
                            {
                                SDL.SDL_SetRenderDrawColor(_renderer, 255, 255, 255, 255); // White - Everything else
                            }
                            SDL.SDL_RenderDrawPoint(_renderer, x, y);
                        }

                        y--;

                        if (y == -1)
                        {
                            y = SpaceInvaders.RESOLUTION_WIDTH - 1;
                            x++;
                        }

                        if (x == SpaceInvaders.RESOLUTION_HEIGHT)
                        {
                            break;
                        }
                    }

                    SDL.SDL_RenderPresent(_renderer);
                }

                // Handle playing sound effects.
                if (soundEffects != null &&
                    tickEventArgs.ShouldPlaySounds &&
                    tickEventArgs.SoundEffects != null)
                {
                    foreach (var sfx in tickEventArgs.SoundEffects)
                    {
                        var pointer = soundEffects[sfx];

                        // Result of Mix_PlayChannel, which indicates the channel the sound is playing on.
                        // -1 indicates an error.
                        var playChannelResult = -1;

                        switch (sfx)
                        {
                        // The UFO sound effect loops until the UFO disappears or is destroyed.
                        case SoundEffect.UFO_Start:
                            playChannelResult = SDL_mixer.Mix_PlayChannel(AUDIO_CHANNEL_UFO, pointer, AUDIO_INFINITE_LOOP);
                            break;

                        case SoundEffect.UFO_Stop:
                        {
                            SDL_mixer.Mix_Pause(AUDIO_CHANNEL_UFO);

                            // Mix_Pause doesn't return a channel or error code. Ensure we don't leave as -1.
                            playChannelResult = AUDIO_CHANNEL_UFO;

                            break;
                        }

                        case SoundEffect.InvaderMove1:
                        case SoundEffect.InvaderMove2:
                        case SoundEffect.InvaderMove3:
                        case SoundEffect.InvaderMove4:
                            playChannelResult = SDL_mixer.Mix_PlayChannel(AUDIO_CHANNEL_INVADER_MOVEMENT, pointer, AUDIO_NO_LOOP);
                            break;

                        default:
                            playChannelResult = SDL_mixer.Mix_PlayChannel(AUDIO_CHANNEL_COMMON, pointer, AUDIO_NO_LOOP);
                            break;
                        }

                        if (playChannelResult == -1)
                        {
                            Console.WriteLine("Error playing sound effect {0}. SDL Error: {1}", sfx, SDL.SDL_GetError());
                        }
                    }
                }

                // See if we need to delay to keep locked to ~ targetTicskHz.

                if (stopwatch.Elapsed.TotalMilliseconds < (1000 / _targetTicksHz))
                {
                    var delay = (1000 / _targetTicksHz) - stopwatch.Elapsed.TotalMilliseconds;
                    SDL.SDL_Delay((uint)delay);
                }

                // If the event handler indicated we should quit, then stop.
                if (tickEventArgs.ShouldQuit)
                {
                    return;
                }
            }
        }
示例#14
0
 public void PlaySong()
 {
     SDL_mixer.Mix_PlayChannel(-1, music, 1);
 }
示例#15
0
 [JSFunction(Name = "playSound")] public void PlaySound(string audioPath)
 {
     SDL_mixer.Mix_PlayChannel(0, Disaster.Assets.Audio(audioPath), 0);
 }
示例#16
0
        static int Main(string[] args)
        {
            SDL.SDL_SetHint(SDL.SDL_HINT_WINDOWS_DISABLE_THREAD_NAMING, "1");
            Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
            Thread.CurrentThread.CurrentCulture   = CultureInfo.InvariantCulture;

            //Start up SDL and create window
            var success = Init();

            if (success == false)
            {
                Console.WriteLine("Failed to initialize!");
            }
            else
            {
                //Load media
                success = LoadMedia();
                if (success == false)
                {
                    Console.WriteLine("Failed to load media!");
                }
                else
                {
                    //Main loop flag
                    bool quit = false;

                    //While application is running
                    while (!quit)
                    {
                        //Event handler
                        SDL.SDL_Event e;

                        //Handle events on queue
                        while (SDL.SDL_PollEvent(out e) != 0)
                        {
                            //User requests quit
                            if (e.type == SDL.SDL_EventType.SDL_QUIT)
                            {
                                quit = true;
                            }
                            //Handle key press
                            else if (e.type == SDL.SDL_EventType.SDL_KEYDOWN)
                            {
                                switch (e.key.keysym.sym)
                                {
                                //Play high sound effect
                                case SDL.SDL_Keycode.SDLK_1:
                                    SDL_mixer.Mix_PlayChannel(-1, _High, 0);
                                    break;

                                //Play medium sound effect
                                case SDL.SDL_Keycode.SDLK_2:
                                    SDL_mixer.Mix_PlayChannel(-1, _Medium, 0);
                                    break;

                                //Play low sound effect
                                case SDL.SDL_Keycode.SDLK_3:
                                    SDL_mixer.Mix_PlayChannel(-1, _Low, 0);
                                    break;

                                //Play scratch sound effect
                                case SDL.SDL_Keycode.SDLK_4:
                                    SDL_mixer.Mix_PlayChannel(-1, _Scratch, 0);
                                    break;

                                case SDL.SDL_Keycode.SDLK_9:
                                    //If there is no music playing
                                    if (SDL_mixer.Mix_PlayingMusic() == 0)
                                    {
                                        //Play the music
                                        SDL_mixer.Mix_PlayMusic(_Music, -1);
                                    }
                                    //If music is being played
                                    else
                                    {
                                        //If the music is paused
                                        if (SDL_mixer.Mix_PausedMusic() == 1)
                                        {
                                            //Resume the music
                                            SDL_mixer.Mix_ResumeMusic();
                                        }
                                        //If the music is playing
                                        else
                                        {
                                            //Pause the music
                                            SDL_mixer.Mix_PauseMusic();
                                        }
                                    }
                                    break;

                                case SDL.SDL_Keycode.SDLK_0:
                                    //Stop the music
                                    SDL_mixer.Mix_HaltMusic();
                                    break;
                                }
                            }
                        }

                        //Clear screen
                        SDL.SDL_SetRenderDrawColor(Renderer, 0xFF, 0xFF, 0xFF, 0xFF);
                        SDL.SDL_RenderClear(Renderer);

                        //Render prompt
                        _PromptTexture.Render(0, 0);

                        //Update screen
                        SDL.SDL_RenderPresent(Renderer);
                    }
                }
            }


            //Free resources and close SDL
            Close();

            if (success == false)
            {
                Console.ReadLine();
            }

            return(0);
        }