Ejemplo n.º 1
0
 public static void bgs_play(string filename, int volume = 100, int pitch = 100, int pos = 0)
 {
     if (String.IsNullOrEmpty(filename)) return;
     string file = OpenGame.Runtime.Runtime.FindAudioResource(filename);
     if (file == null) return;
     bgs = OpenGame.Audio.Audio.Play(file, /* loop */ true, (float)volume / 100f, (float)pitch / 100f);
 }
Ejemplo n.º 2
0
        public AudioSample GetSampleFromArray(byte[] data, float volume, float pitch)
        {
            Stream stream = new MemoryStream(data);

            if (stream.ReadByte() == 'R' &&
                stream.ReadByte() == 'I' &&
                stream.ReadByte() == 'F' &&
                stream.ReadByte() == 'F')
            {
                stream.Position = 0;
                int         channels, bits_per_sample, sample_rate;
                byte[]      sound_data = LoadWave(stream, out channels, out bits_per_sample, out sample_rate);
                AudioSample sample     = new AudioSample()
                {
                    Pcm           = sound_data,
                    BitsPerSample = bits_per_sample,
                    Channels      = channels,
                    Rate          = sample_rate,
                };
                return(sample);
            }
            else
            {
                stream.Position = 0;
                AudioSample sample = new OggDecoder().OggToWav(stream, volume, pitch);
                return(sample);
            }
        }
Ejemplo n.º 3
0
 public void PlayLoop(AudioSample sample, bool play, bool restart, float x, float y, float z)
 {
     if (sample == null)
     {
         return;
     }
     if (context == null)
     {
         return;
     }
     if (play)
     {
         if (!soundsplaying.ContainsKey(sample))
         {
             soundsplaying[sample]      = new AudioTask(sample, this, x, y, z);
             soundsplaying[sample].loop = true;
         }
         if (restart)
         {
             soundsplaying[sample].Restart();
         }
         soundsplaying[sample].shouldplay = true;
         soundsplaying[sample].Play();
     }
     else
     {
         if (soundsplaying.ContainsKey(sample))
         {
             soundsplaying[sample].shouldplay = false;
         }
     }
 }
Ejemplo n.º 4
0
 public void Play(AudioSample sample, float x, float y, float z)
 {
     if (sample == null)
     {
         return;
     }
     if (context == null)
     {
         return;
     }
     new AudioTask(sample, this, x, y, z).Play();
 }
Ejemplo n.º 5
0
    public static void bgs_play(string filename, int volume = 100, int pitch = 100, int pos = 0)
    {
        if (String.IsNullOrEmpty(filename))
        {
            return;
        }
        string file = OpenGame.Runtime.Runtime.FindAudioResource(filename);

        if (file == null)
        {
            return;
        }
        bgs = OpenGame.Audio.Audio.Play(file, /* loop */ true, (float)volume / 100f, (float)pitch / 100f);
    }
Ejemplo n.º 6
0
        private static AudioSample GetAudioSample(string file, float volume, float pitch)
        {
            string key = file + "." + volume.ToString("0.00") + "." + pitch.ToString("0.00");

            if (!audiosamples.ContainsKey(key))
            {
                byte[]      data   = GetFile(file);
                AudioSample sample = Load(data, volume, pitch);
                audiosamples[key] = sample;
            }

            AudioSample ret = audiosamples[key];

            return(ret);
        }
Ejemplo n.º 7
0
        public static AudioSample Play(string f, bool loop, float volume, float pitch, float x = 0f, float y = 0f, float z = 0f)
        {
            if (!setup)
            {
                Setup();
            }
            AudioSample aas = GetAudioSample(f, volume, pitch);

            if (loop)
            {
                audio.PlayLoop(aas, x, y, z);
            }
            else
            {
                audio.Play(aas, x, y, z);
            }
            return(aas);
        }
Ejemplo n.º 8
0
 public void PlayLoop(AudioSample sample, float x, float y, float z)
 {
     PlayLoop(sample, true, false, x, y, z);
 }
Ejemplo n.º 9
0
 public AudioTask(AudioSample sample, AudioOpenAl audio, float x, float y, float z)
 {
     this.sample   = sample;
     this.audio    = audio;
     this.position = new Vector3(x, y, z);
 }
Ejemplo n.º 10
0
        public AudioSample OggToWav(Stream ogg, float volume, float pitch)
        {
            AudioSample sample = new AudioSample();
            TextWriter s_err = new StringWriter();
            Stream input = null;
            MemoryStream output = null;

            input = ogg;
            output = new MemoryStream();

            SyncState oy = new SyncState();
            StreamState os = new StreamState();
            Page og = new Page();
            Packet op = new Packet();

            Info vi = new Info();
            Comment vc = new Comment();
            DspState vd = new DspState();
            Block vb = new Block(vd);

            byte[] buffer;
            int bytes = 0;
            oy.init();
            while (true)
            {
                int eos = 0;
                int index = oy.buffer(4096);
                buffer = oy.data;
                try
                {
                    bytes = input.Read(buffer, index, 4096);
                }
                catch (Exception e)
                {
                    s_err.WriteLine(e);
                }
                oy.wrote(bytes);
                if (oy.pageout(og) != 1)
                {
                    if (bytes < 4096) break;
                    s_err.WriteLine("Input does not appear to be an Ogg bitstream.");
                }
                os.init(og.serialno());
                vi.init();
                vc.init();
                if (os.pagein(og) < 0)
                {
                    s_err.WriteLine("Error reading first page of Ogg bitstream data.");
                }

                if (os.packetout(op) != 1)
                {
                    s_err.WriteLine("Error reading initial header packet.");
                }

                if (vi.synthesis_headerin(vc, op) < 0)
                {
                    s_err.WriteLine("This Ogg bitstream does not contain Vorbis audio data.");
                }

                int i = 0;
                while (i < 2)
                {
                    while (i < 2)
                    {
                        int result = oy.pageout(og);
                        if (result == 0) break;
                        if (result == 1)
                        {
                            os.pagein(og);
                            while (i < 2)
                            {
                                result = os.packetout(op);
                                if (result == 0) break;
                                vi.synthesis_headerin(vc, op);
                                i++;
                            }
                        }
                    }
                    index = oy.buffer(4096);
                    buffer = oy.data;
                    try
                    {
                        bytes = input.Read(buffer, index, 4096);
                    }
                    catch (Exception e)
                    {
                        s_err.WriteLine(e);
                    }
                    oy.wrote(bytes);
                }
                sample.Channels = vi.channels;
                sample.Rate = (int)((float)vi.rate * pitch);
                convsize = 4096 / vi.channels;
                vd.synthesis_init(vi);
                vb.init(vd);
                float[][][] _pcm = new float[1][][];
                int[] _index = new int[vi.channels];
                while (eos == 0)
                {
                    while (eos == 0)
                    {
                        int result = oy.pageout(og);
                        if (result == 0) break;
                        if (result != -1)
                        {
                            os.pagein(og);
                            while (true)
                            {
                                result = os.packetout(op);
                                if (result == 0) break;
                                if (result != -1)
                                {
                                    int samples;
                                    if (vb.synthesis(op) == 0)
                                    {
                                        vd.synthesis_blockin(vb);
                                    }
                                    while ((samples = vd.synthesis_pcmout(_pcm, _index)) > 0)
                                    {
                                        float[][] pcm = _pcm[0];
                                        int bout = (samples < convsize ? samples : convsize);
                                        for (i = 0; i < vi.channels; i++)
                                        {
                                            int ptr = i * 2;
                                            int mono = _index[i];
                                            for (int j = 0; j < bout; j++)
                                            {
                                                int val = (int)(pcm[i][mono + j] * 32767.0);
                                                if (val > 32767)
                                                {
                                                    val = 32767;
                                                }
                                                if (val < -32768)
                                                {
                                                    val = -32768;
                                                }
                                                val = (int)((float)val * volume);
                                                if (val < 0) val = val | 0x8000;
                                                convbuffer[ptr] = (byte)(val);
                                                convbuffer[ptr + 1] = (byte)((uint)val >> 8);
                                                ptr += 2 * (vi.channels);
                                            }
                                        }
                                        output.Write(convbuffer, 0, 2 * vi.channels * bout);
                                        vd.synthesis_read(bout);
                                    }
                                }
                            }
                            if (og.eos() != 0) eos = 1;
                        }
                    }
                    if (eos == 0)
                    {
                        index = oy.buffer(4096);
                        buffer = oy.data;
                        try
                        {
                            bytes = input.Read(buffer, index, 4096);
                        }
                        catch (Exception e)
                        {
                            s_err.WriteLine(e);
                        }
                        oy.wrote(bytes);
                        if (bytes == 0) eos = 1;
                    }
                }
                os.clear();
                vb.clear();
                vd.clear();
                vi.clear();
                break;
            }

            oy.clear();
            input.Close();
            sample.Pcm = output.ToArray();
            return sample;
        }
Ejemplo n.º 11
0
 public void PlayLoop(AudioSample sample, bool play, bool restart, float x, float y, float z)
 {
     if (sample == null)
     {
         return;
     }
     if (context == null)
     {
         return;
     }
     if (play)
     {
         if (!soundsplaying.ContainsKey(sample))
         {
             soundsplaying[sample] = new AudioTask(sample, this, x, y, z);
             soundsplaying[sample].loop = true;
         }
         if (restart)
         {
             soundsplaying[sample].Restart();
         }
         soundsplaying[sample].shouldplay = true;
         soundsplaying[sample].Play();
     }
     else
     {
         if (soundsplaying.ContainsKey(sample))
         {
             soundsplaying[sample].shouldplay = false;
         }
     }
 }
Ejemplo n.º 12
0
 public void PlayLoop(AudioSample sample, float x, float y, float z)
 {
     PlayLoop(sample, true, false, x, y, z);
 }
Ejemplo n.º 13
0
 public void Play(AudioSample sample, float x, float y, float z)
 {
     if (sample == null)
     {
         return;
     }
     if (context == null)
     {
         return;
     }
     new AudioTask(sample, this, x, y, z).Play();
 }
Ejemplo n.º 14
0
 public AudioSample GetSampleFromArray(byte[] data, float volume, float pitch)
 {
     Stream stream = new MemoryStream(data);
     if (stream.ReadByte() == 'R'
         && stream.ReadByte() == 'I'
         && stream.ReadByte() == 'F'
         && stream.ReadByte() == 'F')
     {
         stream.Position = 0;
         int channels, bits_per_sample, sample_rate;
         byte[] sound_data = LoadWave(stream, out channels, out bits_per_sample, out sample_rate);
         AudioSample sample = new AudioSample()
         {
             Pcm = sound_data,
             BitsPerSample = bits_per_sample,
             Channels = channels,
             Rate = sample_rate,
         };
         return sample;
     }
     else
     {
         stream.Position = 0;
         AudioSample sample = new OggDecoder().OggToWav(stream, volume, pitch);
         return sample;
     }
 }
Ejemplo n.º 15
0
 public AudioTask(AudioSample sample, AudioOpenAl audio, float x, float y, float z)
 {
     this.sample = sample;
     this.audio = audio;
     this.position = new Vector3(x, y, z);
 }
Ejemplo n.º 16
0
        public AudioSample OggToWav(Stream ogg, float volume, float pitch)
        {
            AudioSample  sample = new AudioSample();
            TextWriter   s_err  = new StringWriter();
            Stream       input  = null;
            MemoryStream output = null;

            input  = ogg;
            output = new MemoryStream();

            SyncState   oy = new SyncState();
            StreamState os = new StreamState();
            Page        og = new Page();
            Packet      op = new Packet();

            Info     vi = new Info();
            Comment  vc = new Comment();
            DspState vd = new DspState();
            Block    vb = new Block(vd);

            byte[] buffer;
            int    bytes = 0;

            oy.init();
            while (true)
            {
                int eos   = 0;
                int index = oy.buffer(4096);
                buffer = oy.data;
                try
                {
                    bytes = input.Read(buffer, index, 4096);
                }
                catch (Exception e)
                {
                    s_err.WriteLine(e);
                }
                oy.wrote(bytes);
                if (oy.pageout(og) != 1)
                {
                    if (bytes < 4096)
                    {
                        break;
                    }
                    s_err.WriteLine("Input does not appear to be an Ogg bitstream.");
                }
                os.init(og.serialno());
                vi.init();
                vc.init();
                if (os.pagein(og) < 0)
                {
                    s_err.WriteLine("Error reading first page of Ogg bitstream data.");
                }

                if (os.packetout(op) != 1)
                {
                    s_err.WriteLine("Error reading initial header packet.");
                }

                if (vi.synthesis_headerin(vc, op) < 0)
                {
                    s_err.WriteLine("This Ogg bitstream does not contain Vorbis audio data.");
                }

                int i = 0;
                while (i < 2)
                {
                    while (i < 2)
                    {
                        int result = oy.pageout(og);
                        if (result == 0)
                        {
                            break;
                        }
                        if (result == 1)
                        {
                            os.pagein(og);
                            while (i < 2)
                            {
                                result = os.packetout(op);
                                if (result == 0)
                                {
                                    break;
                                }
                                vi.synthesis_headerin(vc, op);
                                i++;
                            }
                        }
                    }
                    index  = oy.buffer(4096);
                    buffer = oy.data;
                    try
                    {
                        bytes = input.Read(buffer, index, 4096);
                    }
                    catch (Exception e)
                    {
                        s_err.WriteLine(e);
                    }
                    oy.wrote(bytes);
                }
                sample.Channels = vi.channels;
                sample.Rate     = (int)((float)vi.rate * pitch);
                convsize        = 4096 / vi.channels;
                vd.synthesis_init(vi);
                vb.init(vd);
                float[][][] _pcm   = new float[1][][];
                int[]       _index = new int[vi.channels];
                while (eos == 0)
                {
                    while (eos == 0)
                    {
                        int result = oy.pageout(og);
                        if (result == 0)
                        {
                            break;
                        }
                        if (result != -1)
                        {
                            os.pagein(og);
                            while (true)
                            {
                                result = os.packetout(op);
                                if (result == 0)
                                {
                                    break;
                                }
                                if (result != -1)
                                {
                                    int samples;
                                    if (vb.synthesis(op) == 0)
                                    {
                                        vd.synthesis_blockin(vb);
                                    }
                                    while ((samples = vd.synthesis_pcmout(_pcm, _index)) > 0)
                                    {
                                        float[][] pcm  = _pcm[0];
                                        int       bout = (samples < convsize ? samples : convsize);
                                        for (i = 0; i < vi.channels; i++)
                                        {
                                            int ptr  = i * 2;
                                            int mono = _index[i];
                                            for (int j = 0; j < bout; j++)
                                            {
                                                int val = (int)(pcm[i][mono + j] * 32767.0);
                                                if (val > 32767)
                                                {
                                                    val = 32767;
                                                }
                                                if (val < -32768)
                                                {
                                                    val = -32768;
                                                }
                                                val = (int)((float)val * volume);
                                                if (val < 0)
                                                {
                                                    val = val | 0x8000;
                                                }
                                                convbuffer[ptr]     = (byte)(val);
                                                convbuffer[ptr + 1] = (byte)((uint)val >> 8);
                                                ptr += 2 * (vi.channels);
                                            }
                                        }
                                        output.Write(convbuffer, 0, 2 * vi.channels * bout);
                                        vd.synthesis_read(bout);
                                    }
                                }
                            }
                            if (og.eos() != 0)
                            {
                                eos = 1;
                            }
                        }
                    }
                    if (eos == 0)
                    {
                        index  = oy.buffer(4096);
                        buffer = oy.data;
                        try
                        {
                            bytes = input.Read(buffer, index, 4096);
                        }
                        catch (Exception e)
                        {
                            s_err.WriteLine(e);
                        }
                        oy.wrote(bytes);
                        if (bytes == 0)
                        {
                            eos = 1;
                        }
                    }
                }
                os.clear();
                vb.clear();
                vd.clear();
                vi.clear();
                break;
            }

            oy.clear();
            input.Close();
            sample.Pcm = output.ToArray();
            return(sample);
        }