ASX Version4.37.03

csharp_asx_player/Form1.cs

This is an example of how to use the ASX Player control from c#.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Audio.AudioScience;


namespace csharp_asx_player
{
    public partial class Form1 : Form
    {
        public static Form1 instance;
        IntPtr hASX = (IntPtr)null;
        IntPtr hAdapter = (IntPtr)null;
        IntPtr hMixer = (IntPtr)null;
        IntPtr hStream = (IntPtr)null;
        IntPtr hMeter = (IntPtr)null;
        bool nPlaying = false;
        int nChannels, nSampleRate;

        public Form1()
        {
            InitializeComponent();
            instance = this;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Asx.ASX_System_Delete(hASX);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            uint uError = Asx.ASX_System_Create(Asx.ASX_SYSTEM_TYPE_HPI, ref hASX);
            if (uError != 0)
                textBox1.AppendText("Error opening ASX " + uError.ToString());

            StringBuilder sSystemVersion = new StringBuilder(Asx.ASX_SHORT_STRING);
            StringBuilder sSubSystemVersion = new StringBuilder(Asx.ASX_SHORT_STRING);

            int n1,n2;
            uError = Asx.ASX_System_GetVersion(hASX, sSystemVersion, Asx.ASX_SHORT_STRING, out n1, sSubSystemVersion, Asx.ASX_SHORT_STRING, out n2);
            if (uError != 0)
                textBox1.AppendText("Error ASX_System_GetVersion() " + uError.ToString());
            textBox1.AppendText("ASX Version " + sSystemVersion + " SubSystem Version " + sSubSystemVersion );

            int nAdapters;
            uError = Asx.ASX_System_GetAdapterCount(hASX, out nAdapters);
            if (uError != 0)
                textBox1.AppendText("Error ASX_System_GetAdapterCount() " + uError.ToString());

            int i;
            for (i = 0; i < nAdapters; i++)
            {
                uError = Asx.ASX_System_GetAdapter(hASX, i, ref hAdapter);
                if (uError != 0)
                    textBox1.AppendText("Error ASX_System_GetAdapter() " + uError.ToString());
                StringBuilder name = new StringBuilder(Asx.ASX_SHORT_STRING);
                uError = Asx.ASX_Adapter_GetName(hAdapter, name, Asx.ASX_SHORT_STRING, out n1);
                if (uError != 0)
                    textBox1.AppendText("Error ASX_Adapter_GetName() " + uError.ToString());
                else
                {
                    comboBox1.Items.Insert(i, name);
                }
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            comboBox2.Items.Clear();
            // list output streams
            uint uError = Asx.ASX_System_GetAdapter(hASX, comboBox1.SelectedIndex, ref hAdapter);
            if (uError != 0)
                textBox1.AppendText("Error ASX_System_GetAdapter() " + uError.ToString());

            uError = Asx.ASX_Adapter_GetMixer(hAdapter, ref hMixer);
            if (uError != 0)
                textBox1.AppendText("Error ASX_Adapter_GetMixer()" + uError.ToString());
            int nNodes;
            uError = Asx.ASX_Mixer_GetNodeTypeCount(hMixer, Asx.asxNODE.asxNODE_PLAYER, out nNodes);
            if (uError != 0)
                textBox1.AppendText("Error ASX_Mixer_GetNodeTypeCount()" + uError.ToString());
            StringBuilder szNode = new StringBuilder(Asx.ASX_SHORT_STRING);
            int n1;
            uError = Asx.ASXSTRING_EnumToString((int)Asx.asxNODE.asxNODE_PLAYER, szNode, Asx.ASX_SHORT_STRING, out n1);
            if (uError != 0)
                textBox1.AppendText("Error ASXSTRING_EnumToString()" + uError.ToString());
            for (int i = 0; i < nNodes; i++)
            {
                string szNodeName = szNode.ToString() + " " + (i + 1);
                comboBox2.Items.Insert(i, szNodeName);
            }
            comboBox2.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            label1.Text = openFileDialog1.FileName;

            uint uError = Asx.ASX_Mixer_GetControlByNodeTypeAndIndex(hMixer, 
                        Asx.asxNODE.asxNODE_PLAYER, comboBox2.SelectedIndex, 
                        0, 0, 
                        Asx.asxCONTROL.asxCONTROL_PLAYER, ref hStream);
            if (uError != 0)
                textBox1.AppendText("Error ASX_Mixer_GetControlByNodeTypeAndIndex()" + uError.ToString());
            uError = Asx.ASX_Player_Open(hStream, new StringBuilder(label1.Text));
            if (uError != 0)
                textBox1.AppendText("Error ASX_Player_Open()" + uError.ToString());

            Asx.asxAUDIO_FORMAT eFormat;
            int nBitRate;
            uError = Asx.ASX_Player_Format_GetDetails(hStream, out eFormat, out nChannels, out nSampleRate, out nBitRate);
            if (uError != 0)
                textBox1.AppendText("Error ASX_Player_Format_GetDetails()" + uError.ToString());

            label6.Text = nChannels.ToString();
            label8.Text = nSampleRate.ToString();
            if(eFormat==Asx.asxAUDIO_FORMAT.asxAUDIO_FORMAT_PCM16)
                label7.Text = "PCM16";
            else if(eFormat==Asx.asxAUDIO_FORMAT.asxAUDIO_FORMAT_MPEG_L2)
                label7.Text = "MPEG2";
            else
                label7.Text = "unknown";
            uError = Asx.ASX_Player_Close(hStream);
            if (uError != 0)
                textBox1.AppendText("Error ASX_Player_Close()" + uError.ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            uint uError = Asx.ASX_Player_Open(hStream, new StringBuilder(label1.Text));
            if (uError != 0)
                textBox1.AppendText("Error ASX_Player_Open()" + uError.ToString());
            uError = Asx.ASX_Player_PreLoad(hStream, Asx.asxTIMESCALE.asxTIMESCALE_SAMPLES, 0);
            if (uError != 0)
                textBox1.AppendText("Error ASX_Player_PreLoad()" + uError.ToString());
            uError = Asx.ASX_Player_Start(hStream );
            if (uError != 0)
                textBox1.AppendText("Error ASX_Player_Start()" + uError.ToString());

            uError = Asx.ASX_Mixer_GetControlByNodeTypeAndIndex(hMixer,
                        Asx.asxNODE.asxNODE_PLAYER, comboBox2.SelectedIndex,
                        0, 0,
                        Asx.asxCONTROL.asxCONTROL_METER, ref hMeter);

            nPlaying = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (nPlaying)
            {
                uint lms;
                uint uError = Asx.ASX_Player_GetPosition(hStream, Asx.asxTIMESCALE.asxTIMESCALE_SAMPLES, out lms);
                if (uError != 0)
                    textBox1.AppendText("Error ASX_Player_GetPosition()" + uError.ToString());
                int sec = (int)(lms / (ulong)nSampleRate);
                int hund = (int)(lms % (ulong)nSampleRate * 100 / (ulong)nSampleRate);
                int hours = sec / 3600;
                sec = sec % 3600;
                int min = sec / 60;
                sec = sec % 60;

                label9.Text = String.Format("{0:d2}:{1:d2}:{2:d2}.{3:d2}", hours, min, sec, hund);

                Asx.asxPLAYER_STATE eState;
                uError = Asx.ASX_Player_GetState(hStream,out eState);
                if (uError != 0)
                    textBox1.AppendText("Error ASX_Player_GetState()" + uError.ToString());

                float[] peak = new float[2];
                float[] rms = new float[2];
                uError = Asx.ASX_Meter_GetPeak(hMeter, peak, 2);
                if (uError != 0)
                    textBox1.AppendText("Error ASX_Meter_GetPeak()" + uError.ToString());
                uError = Asx.ASX_Meter_GetRMS(hMeter, rms, 2);
                if (uError != 0)
                    textBox1.AppendText("Error ASX_Meter_GetRMS()" + uError.ToString());

                int n1 = (int)(peak[0] + 40.0);
                if (n1 < 0)
                    n1 = 0;
                progressBar1.Value = n1;
                n1 = (int)(peak[1] + 40.0);
                if (n1 < 0)
                    n1 = 0;
                progressBar2.Value = n1;
                leftPeak.Text = String.Format("{0:d2}", (int)peak[0]);
                rightPeak.Text = String.Format("{0:d2}", (int)peak[1]);
                leftRMS.Text = String.Format("{0:d2}", (int)rms[0]);
                rightRMS.Text = String.Format("{0:d2}", (int)rms[1]);

                if (eState == Asx.asxPLAYER_STATE.asxPLAYER_DONE)
                {
                    uError = Asx.ASX_Player_Close(hStream);
                    if (uError != 0)
                        textBox1.AppendText("Error ASX_Player_Close()" + uError.ToString());
                    nPlaying = false;

                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (nPlaying)
            {
                uint uError = Asx.ASX_Player_Close(hStream);
                if (uError != 0)
                    textBox1.AppendText("Error ASX_Player_Close()" + uError.ToString());
                label9.Text = "00:00:00.00";

                progressBar1.Value = 0;
                progressBar2.Value = 0;
                leftPeak.Text = String.Format("{0:d2}", 0);
                rightPeak.Text = String.Format("{0:d2}", 0);
                leftRMS.Text = String.Format("{0:d2}", 0);
                rightRMS.Text = String.Format("{0:d2}", 0);

                nPlaying = false;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}