예제 #1
0
        public void BasicOperations()
        {
            using (var buf = new RingBuffer()) {
                Assert.Equal(0, buf.BytesAvailable());
                Assert.Equal(8191, buf.WriteBytesAvailable());                   // implementation dependent.
                using (var synth = new SynthUnit(buf)) {
                    var bytes = new byte [] { 0x90, 0x30, 0 };                   // note on at 0x30, but with no velocity = no sound.
                    buf.Write(bytes, 0, bytes.Length);
                    var results = new short [1024];
                    Assert.Equal(3, buf.BytesAvailable());
                    synth.Init(44100);
                    synth.GetSamples(results, 0, results.Length);
                    Assert.All(results, r => Assert.Equal(0, r));

                    bytes = new byte [] { 0x90, 0x30, 100 };                     // now give vel = 100
                    buf.Write(bytes, 0, bytes.Length);
                    synth.GetSamples(results, 0, results.Length);
                    Assert.Contains(results, r => r != 0);

                    // to see what's being generated, enable these lines.
                    //var str = string.Concat (results.Select (s => s.ToString ("X04")));
                    //Console.WriteLine (str);
                }
            }
        }
예제 #2
0
        void Run(string [] args)
        {
            ring_buffer = new RingBuffer();
            synth       = new SynthUnit(ring_buffer);
            synth.Init(44100);

            soundio = new SoundIO();
            soundio.Connect();
            soundio.FlushEvents();
            Console.WriteLine("SoundIO backend: " + soundio.CurrentBackend);

            // MIDI sender
            var midi_wait = new ManualResetEvent(false);

            Task.Run(() => {
                midi_wait.WaitOne();
                if (args.Length > 0)
                {
                    var sysex     = File.ReadAllBytes(args [0]);
                    int remaining = sysex.Length;
                    while (remaining > 0)
                    {
                        int len = Math.Min(remaining, ring_buffer.WriteBytesAvailable());
                        ring_buffer.Write(sysex, 0, len);
                        Task.Delay(50);
                        remaining -= len;
                    }
                }
                for (int i = 0; i < 10; i++)
                {
                    byte [] bytes = { 0x90, (byte)(0x30 + i), 0x60 };
                    ring_buffer.Write(bytes, 0, 3);
                    System.Threading.Thread.Sleep(1000);
                    byte [] bytes2 = { 0x80, (byte)(0x30 + i), 0 };
                    ring_buffer.Write(bytes2, 0, 3);
                }
            });

            // Audio outputter
            var wait = new ManualResetEvent(false);

            Task.Run(() => {
                var device = soundio.GetOutputDevice(soundio.DefaultOutputDeviceIndex);
                if (device.ProbeError != 0)
                {
                    Console.Error.WriteLine($"Cannot probe device {device.Name}.");
                    return;
                }
                Console.WriteLine($"Output device: {device.Name}");
                var outStream = device.CreateOutStream();
                if (!device.SupportsFormat(SoundIOFormat.S16LE))
                {
                    throw new NotSupportedException();
                }
                outStream.Format            = SoundIOFormat.S16LE;
                outStream.WriteCallback     = (min, max) => WriteCallback(outStream, min, max);
                outStream.UnderflowCallback = () => { Console.WriteLine("underflow"); };
                outStream.ErrorCallback     = () => {
                    Console.WriteLine($"ERROR at libsoundio: {outStream.LayoutErrorMessage}");
                };
                outStream.Open();
                play_audio = true;
                midi_wait.Set();
                Task.Delay(50);
                outStream.Start();
                soundio.FlushEvents();

                wait.WaitOne();
                outStream.Dispose();
                device.RemoveReference();
            });

            Console.Read();
            play_audio = false;
            wait.Set();
            Console.WriteLine("Finishing up");
            soundio.Dispose();
        }
예제 #3
0
 public DX7Synthesizer()
 {
     ring_buffer = new RingBuffer();
     synth       = new SynthUnit(ring_buffer);
     synth.Init(44100);
 }