Example #1
0
        public static unsafe void StartMixer(int sampleRate, short bufferLen)
        {
            SDL_AudioSpec desired = new SDL_AudioSpec();

            m_ObtainedSpecs = new SDL_AudioSpec();

            desired.channels = 1;
            desired.format   = Sdl.AUDIO_U8;
            desired.freq     = sampleRate;
            desired.samples  = bufferLen;
            desired.callback = audioCallback;

            IntPtr desiredPtr  = Marshal.AllocHGlobal(Marshal.SizeOf(desired));
            IntPtr obtainedPtr = Marshal.AllocHGlobal(Marshal.SizeOf(m_ObtainedSpecs));

            try
            {
                Marshal.StructureToPtr(desired, desiredPtr, false);
                int i = Sdl.SDL_OpenAudio(desiredPtr, obtainedPtr);
                m_ObtainedSpecs = (SDL_AudioSpec)Marshal.PtrToStructure(obtainedPtr, typeof(SDL_AudioSpec));
            }
            finally
            {
                Marshal.FreeHGlobal(desiredPtr);
                Marshal.FreeHGlobal(obtainedPtr);
            }

            m_SampleRate = m_ObtainedSpecs.freq;
            m_BaseLine   = (byte)m_ObtainedSpecs.silence;

            Sdl.SDL_PauseAudio(0);
        }
        public AudioContext(int freq, ushort format, byte channels)
        {
            AudioSpec = new SDL_AudioSpec()
            {
                freq     = freq,
                format   = format,
                channels = channels,
                samples  = (ushort)MinBufferSize,
            };

            switch (format)
            {
            case AUDIO_U8: {
                BytesPerSample = 1;
            } break;

            case AUDIO_S16LSB: {
                BytesPerSample = 2;
            } break;
            }
            ;

            DeviceId = SDL_OpenAudioDevice(null, 0, ref AudioSpec, out AudioSpec, 0);
            if (DeviceId == 0)
            {
                throw new Exception(SDL_GetError());
            }

            SDL_PauseAudioDevice(DeviceId, 0);
        }
Example #3
0
        static void Test()
        {
            SF2 sf2 = new SF2("g:\\test.sf2");

            List <short> samples = sf2.SoundChunk.SMPLSubChunk.samples;

            PCMProcesser pcmProcesser = new PCMProcesser();

            short[] newSamples = pcmProcesser.PitchPcmNote(samples.ToArray(), 24);

            // pitch.Seq(samples.ToArray());


            pcm_buffer = new byte[newSamples.Length * 2];
            int idx = 0;

            for (int i = 0; i < newSamples.Length; i++)
            {
                pcm_buffer[idx++] = (byte)(newSamples[i] & 0xff);
                pcm_buffer[idx++] = (byte)((newSamples[i] >> 8) & 0xff);
            }


            float newTime = pcmProcesser.GetPcmTime(newSamples, 44100);


            SDL_AudioSpec OutputAudioSpec = new SDL_AudioSpec();

            OutputAudioSpec.freq     = 44100;
            OutputAudioSpec.format   = AUDIO_S16;
            OutputAudioSpec.channels = 1;
            OutputAudioSpec.samples  = 1024;
            OutputAudioSpec.callback = SDL_AudioCallback;


            if (SDL_AudioInit(null) < 0)
            {
                return;
            }

            IntPtr n = IntPtr.Zero;

            if (SDL_OpenAudio(ref OutputAudioSpec, n) < 0)
            {
                return;
            }


            audio_len = pcm_buffer.Length; //长度为读出数据长度,在read_audio_data中做减法
            audio_pos = pcm_buffer;
            audio_idx = 0;

            SDL_PauseAudio(0);
            SDL_Delay((uint)(Math.Round(newTime) * 1000));
        }
        private void InitialiseAudio(int sampleRate)
        {
            DisposeAudio();
            var want = new SDL_AudioSpec
            {
                freq     = sampleRate,
                format   = AUDIO_S16,
                channels = 2
            };

            _audioDevice = SDL_OpenAudioDevice(null, 0, ref want, out var have, 0);
            SDL_PauseAudioDevice(_audioDevice, 0);
        }
        int ISoundPcmDriver.Initialize(int freq, int format)
        {
            int result = 0;

            this.sdlCallback = new SDL_AudioCallback(this.SdlCallback);

            SDL_AudioSpec desired = new SDL_AudioSpec
            {
                freq     = freq,
                format   = (ushort)format,
                samples  = SAMPLESIZE,
                channels = 1,
                userdata = IntPtr.Zero,
                callback = this.sdlCallback,
            };

            SDL_AudioSpec obtained = default(SDL_AudioSpec);

            //IntPtr desiredPtr = Marshal.AllocHGlobal(Marshal.SizeOf(desired));
            IntPtr obtainedPtr = Marshal.AllocHGlobal(Marshal.SizeOf(obtained));

            try
            {
                //Marshal.StructureToPtr(desired, desiredPtr, false);
                result = SDL_OpenAudio(ref desired, obtainedPtr);
                if (result == 0)
                {
                    obtained = (SDL_AudioSpec)Marshal.PtrToStructure(obtainedPtr, typeof(SDL_AudioSpec));

                    // pauses callback
                    (this as ISoundPcmDriver).SetState(false);
                }
                else
                {
                    Trace.WriteLine("pcthis.out_sdl_init(): unable to open audio.");
                }
            }
            finally
            {
                //Marshal.FreeHGlobal(desiredPtr);
                Marshal.FreeHGlobal(obtainedPtr);
            }

            return(result);
        }
Example #6
0
 private static extern IntPtr SDL_LoadWAV_RW(IntPtr source, int freeSource, ref SDL_AudioSpec specs, out IntPtr buffer, out uint length);
Example #7
0
		public extern static SDL_AudioSpec SDL_LoadWAV_RW( SDL_RWops src,
                                                           int freesrc,
                                                           SDL_AudioSpec spec,
                                                           byte[] audio_buf,
                                                           out uint audio_len);
Example #8
0
 public static extern uint SDL_OpenAudioDevice([In()][MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(SDLMarshaler))] string device, int iscapture, ref SDL_AudioSpec desired, out SDL_AudioSpec obtained, int allowed_change);
Example #9
0
		public static extern uint SDL_OpenAudioDevice(
			[InAttribute()] [MarshalAsAttribute(UnmanagedType.LPStr)]
				string device,
			int iscapture,
			ref SDL_AudioSpec desired,
			ref SDL_AudioSpec obtained,
			int allowed_changes
		);
Example #10
0
		public extern static int SDL_OpenAudio(SDL_AudioSpec desired, out SDL_AudioSpec obtained);
Example #11
0
 public static IntPtr SDL_LoadWAV_RW(IntPtr src, int freesrc, ref SDL_AudioSpec spec, IntPtr audio_buf, ref uint audio_len) => s_SDL_LoadWAV_RW_SDL_RWops_int_SDL_AudioSpec_IntPtr_uint_t(src, freesrc, ref spec, audio_buf, ref audio_len);
Example #12
0
 public static int SDL_OpenAudio(ref SDL_AudioSpec desired, ref SDL_AudioSpec obtained) => s_SDL_OpenAudio_SDL_AudioSpec_SDL_AudioSpec_t(ref desired, ref obtained);
Example #13
0
 private static IntPtr SDL_LoadWAV_RW(string filename, int freeSource, ref SDL_AudioSpec specs, out IntPtr buffer, out uint length) => SDL_LoadWAV_RW(SDL_RWFromFile(filename.ToBytes(), "rb".ToBytes()), freeSource, ref specs, out buffer, out length);
        public static bool LoadWAV(string fileName, out byte[] pcm, out SDL_AudioSpec spec)
        {
            pcm  = new byte[0];
            spec = new SDL_AudioSpec();

            byte[] bytes   = File.ReadAllBytes(fileName);
            int    pointer = 0;

            WaveHeader header = bytes.Consume <WaveHeader>(ref pointer);

            if (header.RiffId != WaveId_RIFF)
            {
                return(false);
            }
            if (header.WaveId != WaveId_WAVE)
            {
                return(false);
            }

            while (pointer < bytes.Length)
            {
                WaveChunkHeader chunkHeader = bytes.Consume <WaveChunkHeader>(ref pointer);

                if (chunkHeader.Id == WaveId_fmt)
                {
                    WaveFmt fmt = bytes.ReadStruct <WaveFmt>(pointer);
                    spec = new SDL_AudioSpec()
                    {
                        freq     = (int)fmt.SampleRate,
                        channels = (byte)fmt.Channels,
                    };

                    if (fmt.FormatTag != 1)
                    {
                        return(false);
                    }

                    switch (fmt.BitsPerSample)
                    {
                    case 8: {
                        spec.format = AUDIO_U8;
                    } break;

                    case 16: {
                        spec.format = AUDIO_S16LSB;
                    } break;
                    }
                    ;
                }
                else if (chunkHeader.Id == WaveId_data)
                {
                    pcm = bytes.Subarray(pointer, (int)chunkHeader.Size);
                }

                pointer += (int)chunkHeader.Size;
            }

            if (spec.format == AUDIO_U8)
            {
                byte[] newPCM = new byte[pcm.Length * 2];
                for (int i = 0; i < pcm.Length; i++)
                {
                    short sample16 = (short)((pcm[i] - 0x80) << 8);
                    newPCM[i * 2 + 0] = (byte)(sample16 & 0xFF);
                    newPCM[i * 2 + 1] = (byte)(sample16 >> 8);
                }
                pcm         = newPCM;
                spec.format = AUDIO_S16LSB;
            }

            return(true);
        }
Example #15
0
 /// <summary>
 /// SDs the l_ load WA.
 /// </summary>
 /// <returns>
 /// The l_ load WA.
 /// </returns>
 /// <param name='file'>
 /// File.
 /// </param>
 /// <param name='spec'>
 /// Spec.
 /// </param>
 /// <param name='audio_buf'>
 /// Audio_buf.
 /// </param>
 /// <param name='audio_len'>
 /// Audio_len.
 /// </param>
 public static SDL_AudioSpec SDL_LoadWAV(string file, SDL_AudioSpec spec, byte[] audio_buf, out uint audio_len)
 {
     return(SDL_LoadWAV_RW(SDL_RWFromFile(file, "rb"), 1, spec, audio_buf, out audio_len));
 }
Example #16
0
 public extern static SDL_AudioSpec SDL_LoadWAV_RW(SDL_RWops src,
                                                   int freesrc,
                                                   SDL_AudioSpec spec,
                                                   byte[] audio_buf,
                                                   out uint audio_len);
Example #17
0
 public extern static int SDL_OpenAudio(SDL_AudioSpec desired, out SDL_AudioSpec obtained);
Example #18
0
		/// <summary>
		/// SDs the l_ load WA.
		/// </summary>
		/// <returns>
		/// The l_ load WA.
		/// </returns>
		/// <param name='file'>
		/// File.
		/// </param>
		/// <param name='spec'>
		/// Spec.
		/// </param>
		/// <param name='audio_buf'>
		/// Audio_buf.
		/// </param>
		/// <param name='audio_len'>
		/// Audio_len.
		/// </param>
		public static SDL_AudioSpec SDL_LoadWAV(string file, SDL_AudioSpec spec, byte[] audio_buf, out uint audio_len) {
            return SDL_LoadWAV_RW( SDL_RWFromFile( file, "rb" ), 1, spec, audio_buf, out audio_len );
        }
Example #19
0
 private static extern int SDL_OpenAudio(ref SDL_AudioSpec desired, out SDL_AudioSpec obtained);
Example #20
0
		private static extern IntPtr INTERNAL_SDL_LoadWAV(
			[InAttribute()] [MarshalAsAttribute(UnmanagedType.LPStr)]
				string filename,
			ref SDL_AudioSpec spec,
			ref IntPtr audio_buf,
			ref uint audio_len
		);
Example #21
0
 public static SDL_AudioDeviceID SDL_OpenAudioDevice(IntPtr device, int iscapture, ref
                                                     SDL_AudioSpec desired, ref SDL_AudioSpec obtained, int
                                                     allowed_changes) => s_SDL_OpenAudioDevice_IntPtr_int_SDL_AudioSpec_SDL_AudioSpec_int_t(device, iscapture, ref desired, ref obtained, allowed_changes);
Example #22
0
		public static SDL_AudioSpec SDL_LoadWAV(
			string filename,
			ref SDL_AudioSpec spec,
			ref IntPtr audio_buf,
			ref uint audio_len
		) {
			SDL_AudioSpec result;
			IntPtr result_ptr = INTERNAL_SDL_LoadWAV(
				filename,
				ref spec,
				ref audio_buf,
				ref audio_len
			);
			result = (SDL_AudioSpec) Marshal.PtrToStructure(
				result_ptr,
				result.GetType()
			);
			return result;
		}
Example #23
0
 public static extern SDL_AudioSpec *SDL_LoadWAV_RW(
     RWOps src,
     int freesrc,
     out SDL_AudioSpec spec,
     out byte *audio_buf,
     out uint audio_len);
Example #24
0
		public static extern int SDL_OpenAudio(
			ref SDL_AudioSpec desired,
			ref SDL_AudioSpec obtained
		);