Beispiel #1
0
        public int SDL_Init()
        {
            Callback = SDL_AudioCallback;
            #region SDL调用
            //// 初始化调用SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_AUDIO | SDL.SDL_INIT_TIMER)
            //if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_AUDIO | SDL.SDL_INIT_TIMER) < 0)
            //{
            //    Console.WriteLine("Could not initialize SDL - {0}\n", SDL.SDL_GetError());
            //    return -1;
            //}

            #endregion


            SDL.SDL_AudioSpec wanted_spec = new SDL.SDL_AudioSpec();
            wanted_spec.freq     = 8000;
            wanted_spec.format   = SDL.AUDIO_S16;
            wanted_spec.channels = 1;
            wanted_spec.silence  = 0;
            wanted_spec.samples  = 320;
            wanted_spec.callback = Callback;


            if (SDL.SDL_OpenAudio(ref wanted_spec, out SDL.SDL_AudioSpec obtained) < 0)
            {
                Console.WriteLine("can't open audio.");
                return(-1);
            }
            //Play
            SDL.SDL_PauseAudio(0);
            return(0);
        }
Beispiel #2
0
        public int SDL_Init(AVCodecContext *audioCtx)
        {
            Callback = SDL_AudioCallback;

            SDL.SDL_AudioSpec wanted_spec = new SDL.SDL_AudioSpec();
            wanted_spec.freq     = audioCtx->sample_rate;
            wanted_spec.format   = SDL.AUDIO_F32;
            wanted_spec.channels = (byte)audioCtx->channels;
            wanted_spec.silence  = 0;
            wanted_spec.samples  = 1024;
            wanted_spec.callback = Callback;

            if (SDL.SDL_OpenAudio(ref wanted_spec, IntPtr.Zero) < 0)
            {
                Console.WriteLine("can't open audio.");
                return(-1);
            }
            //Play
            SDL.SDL_PauseAudio(0);
            return(0);
        }
Beispiel #3
0
        public AudioEngine(string audioDevice = null, AudioDriver driver = AudioDriver.Default, int sampleRate = 44100, int bufferLength = 1024, ResampleQuality resampleQuality = ResampleQuality.High)
        {
            this.resampleQuality = resampleQuality;

            SDL.SDL_InitSubSystem(SDL.SDL_INIT_AUDIO);

            audioCallback = AudioCallback;
            SDL.SDL_AudioSpec desired = new SDL.SDL_AudioSpec()
            {
                freq     = sampleRate,
                format   = SDL.AUDIO_S16,
                channels = 2,
                samples  = (ushort)bufferLength,
                callback = audioCallback,
            };

            if (driver == AudioDriver.File)
            {
                audioSpec   = desired;
                audioDriver = driver;
            }
            else
            {
                string[] audioDrivers = new string[SDL.SDL_GetNumAudioDrivers()];
                for (int i = 0; i < audioDrivers.Length; i++)
                {
                    audioDrivers[i] = SDL.SDL_GetAudioDriver(i);
                }

                string driverName         = audioDrivers[0];
                string driverFallbackName = audioDrivers.Length > 1 ? audioDrivers[1] : null;

                if (driver != AudioDriver.Default)
                {
                    driverName = driver.ToString();
                }

                int init = SDL.SDL_AudioInit(driverName.ToLower());
                if (init != 0 && driverName == AudioDriver.XAudio2.ToString().ToLower() && driverFallbackName != null)
                {
                    // supplied SDL.dll does not support XAudio2, fallback to next driver
                    driverName = driverFallbackName;
                    init       = SDL.SDL_AudioInit(driverName.ToLower());
                }
                if (init != 0)
                {
                    throw new ApplicationException("Failed to initialize audio driver " + driverName + ": " + SDL.SDL_GetError());
                }

                Enum.TryParse(driverName, true, out audioDriver);

                if (audioDevice == null)
                {
                    string[] audioDevices = new string[SDL.SDL_GetNumAudioDevices(0)];
                    for (int i = 0; i < audioDevices.Length; i++)
                    {
                        audioDevices[i] = SDL.SDL_GetAudioDeviceName(i, 0);
                    }

                    audioDevice = audioDevices.Length > 0 ? audioDevices[0] : null;
                }

                outputDevice = SDL.SDL_OpenAudioDevice(audioDevice, 0, ref desired, out audioSpec, 0);
                if (outputDevice == 0)
                {
                    throw new ApplicationException("Failed to open audio device " + audioDevice + ": " + SDL.SDL_GetError());
                }
            }

            if (audioSpec.format == SDL.AUDIO_S32)
            {
                bytesPerSample = (uint)audioSpec.channels * 4;
            }
            else if (audioSpec.format == SDL.AUDIO_S16 || audioSpec.format == SDL.AUDIO_F32)
            {
                bytesPerSample = (uint)audioSpec.channels * 2;
            }
            else if (audioSpec.format == SDL.AUDIO_S8 || audioSpec.format == SDL.AUDIO_U8)
            {
                bytesPerSample = (uint)audioSpec.channels * 1;
            }

            if (audioSpec.size == 0)
            {
                audioSpec.size = audioSpec.samples * bytesPerSample;
            }

            emptyBuffer = new byte[audioSpec.size];
            audioBuffer = new byte[audioSpec.size];

            lastCallback = 0.0;
            bufferTimer  = Stopwatch.StartNew();

            if (outputDevice != 0)
            {
                SDL.SDL_PauseAudioDevice(outputDevice, 0);
            }
        }