how can i read the raw data from usb sound device with ahk.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jonathan scott james
Posts: 42
Joined: 05 Apr 2016, 01:56

how can i read the raw data from usb sound device with ahk.

20 Jun 2017, 13:20

i discovered a way to make a current probe that puts out 60hz and i can feed it into a 99cent usb sound card and record it with wme and broadcast a live stream of 5000bps. but i want to read the raw ".wav" data and convert it to 100bps and get away from the encoder and make a smaller exe app to do everything. how can i connect to the usb sound device and read it's data without using the cumbersome wme(which inattly puts out wma when you only tell it to use audio) to capture the stream and then having to convert it form wma to wav? i think i could almost do it with qbasic using start:open "device name" for input as #1 : a$=a$+input(1,#1):wait 1000ms:goto start:system

thank you brothers and sisters
Last edited by jonathan scott james on 21 Jun 2017, 13:32, edited 1 time in total.
jonathan scott james
Posts: 42
Joined: 05 Apr 2016, 01:56

Re: how can i read the raw data from usb sound device with ahk in xp

21 Jun 2017, 11:21

this 99cent usb sound device is called "USB Audio Device"
it has only one input channel and 2 output channels

the input channel: seems to be a sample rate of 20ksps and supports condenser mic(supplies voltage to drive the front end in the condenser mic yet works with dynamic mic too).

the output channel: is 2 channel and supports stereo 5.1 surround. it seems to have an output sample rate of higher than 44.1khz(even though it's input is only 20khz)
if i could retrieve data from any device , i might be able to get it to work like i want. does anyone have any ideas?
jonathan scott james
Posts: 42
Joined: 05 Apr 2016, 01:56

Re: how can i read the raw data from usb sound device with ahk in xp

21 Jun 2017, 13:30

can someone help me read data int a variable from "USB Audio Device" or even from a buffer or make a buffer for it and when it's full, transfer the data to a variable please?
pneumatic
Posts: 338
Joined: 05 Dec 2016, 01:51

Re: how can i read the raw data from usb sound device with ahk.

22 Jun 2017, 06:00

I think you would need to use ahk's DllCall to call windows audio functions: https://msdn.microsoft.com/en-us/library/dd316602.aspx

If you havent done DllCalls before in ahk it requires a fair bit of research on it as it can be tricky creating and referencing windows function parameters in just the right way that windows accepts and returns correctly. Frankly I'm not even sure all the windows parameter types can even be created in ahk. The documentation for DllCall is not 100% accurate either and you may need to do a lot of trial and error.
jonathan scott james
Posts: 42
Joined: 05 Apr 2016, 01:56

Re: how can i read the raw data from usb sound device with ahk.

22 Jun 2017, 15:27

thanks . sorry to say , i cant read msdun or i might use it . i used to program amazingly when i wrote autologin scripts to switch to slip. but one day i was editing my quick reference and i did something to it and my list of command syntax stopped working.. and i was so lucky to be inspired to try ahk.. and i can construct good lines of script.. but i can't say that i could ever understand dll calls with all their variable "preformations". yet i can use them when other people formulate them

given all that ,, without sounding like i'm begging, how would it look if you used a default sound device compared to using a 3rd party not designated as default please
p,s, thnanks for the reply, you are kind
jonathan scott james
Posts: 42
Joined: 05 Apr 2016, 01:56

Re: how can i read the raw data from usb sound device with ahk.

23 Jun 2017, 09:43

pneumatic wrote:I think you would need to use ahk's DllCall to call windows audio functions: https://msdn.microsoft.com/en-us/library/dd316602.aspx

If you havent done DllCalls before in ahk it requires a fair bit of research on it as it can be tricky creating and referencing windows function parameters in just the right way that windows accepts and returns correctly. Frankly I'm not even sure all the windows parameter types can even be created in ahk. The documentation for DllCall is not 100% accurate either and you may need to do a lot of trial and error.
so you saying i should type this and it will work like a charm?

HRESULT Item(
[in] UINT nDevice,
[out] IMMDevice **ppDevice
);

i cant get iot to work.. are you having any luck?
is there something i'm missing?
robertcollier4
Posts: 33
Joined: 09 Apr 2016, 22:14

Re: how can i read the raw data from usb sound device with ahk.

23 Jun 2017, 10:48

Following links may help in figuring out the DllCall to the WinAPI.

https://stackoverflow.com/questions/886 ... sound-card - Read raw data from the sound card
http://msdn.microsoft.com/en-us/library ... _topic_006 - coredll.dll WaveIn reference
https://www.codeproject.com/Articles/24 ... ers-in-NET
https://www.codeproject.com/Articles/48 ... e-waveIn-w
https://www.google.com/search?q=read+so ... d+raw+data

But overall, I think it would be easier for you to do this in C# using one of the sample projects on CodeProject.
jonathan scott james
Posts: 42
Joined: 05 Apr 2016, 01:56

Re: how can i read the raw data from usb sound device with ahk.

23 Jun 2017, 11:41

why won't it work?

Code: Select all

[code][code]public class WaveIn
{
   protected class WaveFile : IDisposable
   {

    protected IntPtr m_hwi = IntPtr.Zero;
      protected Wave.WAVEFORMATEX m_wfmt = null;
      protected Wave.WAVEHDR[] m_whdr = null;

 protected bool m_inited = false;
      protected int m_curBlock;
      protected int m_numBlocks;
      protected uint m_bufferSize;
      protected uint m_maxDataLength;

public bool Done { get { return !m_recording; } }
      protected bool m_recording = false;

public Wave.MMSYSERR Preload(uint curDevice, IntPtr hwnd,
         int maxRecordLength_ms, int bufferSize)
      {
         if (m_recording)
            return Wave.MMSYSERR.ERROR;

         if (m_inited)
         {
            Stop();
            FreeWaveBuffers();
         }

         WAVEINCAPS caps = new WAVEINCAPS();
         waveInGetDevCaps(0, caps, caps.Size);

         if ((caps.dwFormats & Wave.WAVE_FORMAT_2S16) == 0)
            return Wave.MMSYSERR.NOTSUPPORTED;

         m_wfmt = new Wave.WAVEFORMATEX();
         m_wfmt.wFormatTag = Wave.WAVE_FORMAT_PCM;
         m_wfmt.wBitsPerSample = 16;
         m_wfmt.nChannels = 2;
         m_wfmt.nSamplesPerSec = 22050;
         m_wfmt.nAvgBytesPerSec = (uint)(m_wfmt.nSamplesPerSec *
            m_wfmt.nChannels * (m_wfmt.wBitsPerSample / 8));
         m_wfmt.nBlockAlign = (ushort)(m_wfmt.wBitsPerSample *
            m_wfmt.nChannels / 8);

         Wave.MMSYSERR result = waveInOpen(ref m_hwi, curDevice,
            m_wfmt, hwnd, 0, Wave.CALLBACK_WINDOW);
         if (result != Wave.MMSYSERR.NOERROR)
            return result;

         if (bufferSize == 0)
            return Wave.MMSYSERR.ERROR;

         m_bufferSize = (uint)bufferSize;

         if (m_bufferSize % m_wfmt.nBlockAlign != 0)
            m_bufferSize += m_wfmt.nBlockAlign - (m_bufferSize %
               m_wfmt.nBlockAlign);

         m_maxDataLength = (uint)(m_wfmt.nAvgBytesPerSec *
            maxRecordLength_ms / 1000);
         m_numBlocks = (int)(m_maxDataLength / m_bufferSize);
         if (m_maxDataLength % m_bufferSize != 0)
            m_numBlocks++;

         m_whdr = new Wave.WAVEHDR[m_numBlocks + 1];

         m_whdr[0] = new Wave.WAVEHDR();
         m_whdr[1] = new Wave.WAVEHDR();

         result = InitBuffer(0);
         if (result != Wave.MMSYSERR.NOERROR)
            return result;

         result = InitBuffer(1);
         if (result != Wave.MMSYSERR.NOERROR)
            return result;

         m_curBlock = 0;
         m_inited = true;

         return Wave.MMSYSERR.NOERROR;
      }

 public void BlockDone()
      {
         m_curBlock++;

         if (m_curBlock < m_numBlocks)
         {
            InitBuffer(m_curBlock + 1);
         }
         else if (m_curBlock == m_numBlocks)
         {
            Stop();
         }
      }

public Wave.MMSYSERR InitBuffer(int bufIndex)
      {
         uint writeLength = (uint)m_bufferSize;
         if (bufIndex < m_numBlocks)
         {
            uint remainingDataLength = (uint)(m_maxDataLength –
               bufIndex * m_bufferSize);
            if (m_bufferSize > remainingDataLength)
               writeLength = remainingDataLength;
         }

         if (m_whdr[bufIndex] == null)
            m_whdr[bufIndex] = new Wave.WAVEHDR();

         Wave.MMSYSERR result = m_whdr[bufIndex].Init(writeLength,
            false);
         if (result != Wave.MMSYSERR.NOERROR)
            return result;

         result = waveInPrepareHeader(m_hwi, m_whdr[bufIndex],
            (uint)Marshal.SizeOf(m_whdr[bufIndex]));
         if (result != Wave.MMSYSERR.NOERROR)
            return result;

         return waveInAddBuffer(m_hwi, m_whdr[bufIndex],
            (uint)Marshal.SizeOf(m_whdr[bufIndex]));

      }

public Wave.MMSYSERR Start()
      {
         if (!m_inited || m_recording)
            return Wave.MMSYSERR.ERROR;

         Wave.MMSYSERR result = waveInStart(m_hwi);
         if (result != Wave.MMSYSERR.NOERROR)
            return result;

         m_recording = true;

         return Wave.MMSYSERR.NOERROR;
      }

private void FreeWaveBuffers()
      {
         m_inited = false;

         if (m_whdr != null)
         {
            for(int i = 0; i < m_whdr.Length; i++)
            {
               if (m_whdr[i] != null)
               {
                  waveInUnprepareHeader(m_hwi, m_whdr[i],
                     (uint)Marshal.SizeOf(m_whdr[i]));

                  m_whdr[i].Dispose();
                  m_whdr[i] = null;
               }
            }

            m_whdr = null;
         }

         waveInClose(m_hwi);
      
         m_hwi = IntPtr.Zero;
      }

private void WriteChars(BinaryWriter wrtr, string text)
      {
         for (int i = 0; i < text.Length; i++)
         {
            char c = (char)text[i];
            wrtr.Write(c);
         }
      }

 public Wave.MMSYSERR Save(string fileName)
      {
         if (!m_inited)
            return Wave.MMSYSERR.ERROR;

         if (m_recording)
            Stop();

         FileStream strm = null;
         BinaryWriter wrtr = null;

         try
         {
            if (File.Exists(fileName))
            {
               FileInfo fi = new FileInfo(fileName);
               if ((fi.Attributes & FileAttributes.ReadOnly) != 0)
                  fi.Attributes -= FileAttributes.ReadOnly;

               strm = new FileStream(fileName, FileMode.Truncate);
            }
            else
            {
               strm = new FileStream(fileName, FileMode.Create);
            }

            if (strm == null)
               return Wave.MMSYSERR.ERROR;

            wrtr = new BinaryWriter(strm);
            if (wrtr == null)
               return Wave.MMSYSERR.ERROR;

            uint totalSize = 0;
            for (int i = 0; i < m_numBlocks; i++)
            {
               if (m_whdr[i] != null)
                  totalSize += m_whdr[i].dwBytesRecorded;
            }

            int chunkSize = (int)(36 + totalSize);

            WriteChars(wrtr, "RIFF");
            wrtr.Write(chunkSize);
            WriteChars(wrtr, "WAVEfmt ");
            wrtr.Write((int)16);
            m_wfmt.Write(wrtr);
            WriteChars(wrtr, "data");
            wrtr.Write((int)totalSize);

            for (int i = 0; i < m_numBlocks; i++)
            {
               if (m_whdr[i] != null)
               {
                  Wave.MMSYSERR result = m_whdr[i].Write(wrtr);
                  if (result != Wave.MMSYSERR.NOERROR)
                     return result;
               }
            }

            return Wave.MMSYSERR.NOERROR;
         }
         finally
         {
            FreeWaveBuffers();

            if (strm != null)
               strm.Close();

            if (wrtr != null)
               wrtr.Close();
         }
      }

public void Stop()
      {
         waveInReset(m_hwi);

         m_recording = false;
      }

 public void Dispose()
      {
         Stop();

         FreeWaveBuffers();
      }
   }
protected class SoundMessageWindow : MessageWindow
   {
      // note custom window messages are after WM_USER = 0x400
      public const int MM_WIM_OPEN = 0x3BE;
      public const int MM_WIM_CLOSE = 0x3BF;
      public const int MM_WIM_DATA = 0x3C0;

      protected WaveIn m_wi = null;

      public SoundMessageWindow(WaveIn wi)
      {
         m_wi = wi;
      }

      protected override void WndProc(ref Message msg)
      {
         switch(msg.Msg)
         {
            case MM_WIM_DATA:
            {
               if (m_wi != null)
                  m_wi.BlockDone();
            }
            break;
         }
         base.WndProc(ref msg);
      }
   }

protected SoundMessageWindow m_msgWindow = null;
   protected WaveFile m_file = null;

public WaveIn()
   {
      m_msgWindow = new SoundMessageWindow(this);
      m_file = new WaveFile();
   }

 public uint NumDevices()
   {
      return (uint)waveInGetNumDevs();
   }
 public Wave.MMSYSERR GetDeviceName(uint deviceId,
      ref string prodName)
   {
      WAVEINCAPS caps = new WAVEINCAPS();
      Wave.MMSYSERR result = waveInGetDevCaps(deviceId, caps,
         caps.Size);
      if (result != Wave.MMSYSERR.NOERROR)
         return result;

      prodName = caps.szPname;

      return Wave.MMSYSERR.NOERROR;
   }

public void BlockDone()
   {
      m_file.BlockDone();
   }

   public Wave.MMSYSERR Preload(int maxRecordLength_ms, int bufferSize)
   {
      if (m_file != null)
         return m_file.Preload(0, m_msgWindow.Hwnd, maxRecordLength_ms,
            bufferSize);

      return Wave.MMSYSERR.NOERROR;
   }

   public void Stop()
   {
      if (m_file != null)
         m_file.Stop();
   }

   public Wave.MMSYSERR Start()
   {
      if (m_file != null)
         return m_file.Start();

      return Wave.MMSYSERR.NOERROR;
   }

   public bool Done()
   {
      return m_file.Done;
   }

   public Wave.MMSYSERR Save(string fileName)
   {
      if (m_file != null)
         return m_file.Save(fileName);

      return Wave.MMSYSERR.NOERROR;
   }


   public void Dispose()
   {
      m_msgWindow.Dispose();

      if (m_file != null)
         m_file.Dispose();
   }

[DllImport ("coredll.dll")]
   protected static extern int waveInGetNumDevs();

   [DllImport ("coredll.dll")]
   private static extern Wave.MMSYSERR waveInOpen(ref IntPtr phwi,
      uint uDeviceID, Wave.WAVEFORMATEX pwfx, IntPtr dwCallback,
      uint dwInstance, uint fdwOpen);

   [DllImport ("coredll.dll")]
   private static extern Wave.MMSYSERR waveInPrepareHeader(IntPtr hwi,
      Wave.WAVEHDR pwh, uint cbwh);

   [DllImport ("coredll.dll")]
   private static extern Wave.MMSYSERR waveInUnprepareHeader(IntPtr
      hwi, Wave.WAVEHDR pwh, uint cbwh);

   [DllImport ("coredll.dll")]
   protected static extern Wave.MMSYSERR waveInClose(IntPtr hwi);

   [DllImport ("coredll.dll")]
   protected static extern Wave.MMSYSERR waveInReset(IntPtr hwi); 

   [DllImport ("coredll.dll")]
   protected static extern Wave.MMSYSERR waveInStart(IntPtr hwi);
 
   [DllImport ("coredll.dll")]
   protected static extern Wave.MMSYSERR waveInStop(IntPtr hwi);

   [DllImport ("coredll.dll")]
   private static extern Wave.MMSYSERR waveInAddBuffer(IntPtr hwi,
      Wave.WAVEHDR pwh, uint cbwh);
   
   protected class WAVEINCAPS
   {
      const uint WAVEINCAPS_SIZE = 80;

      public static implicit operator byte[](WAVEINCAPS caps)
      {
         return caps.m_data;
      }

      private byte[] m_data = null;
      public uint Size { get { return (uint)WAVEINCAPS_SIZE; } }

      public ushort wMid
         { get { return BitConverter.ToUInt16(m_data, 0); } }
      public ushort wPid
         { get { return BitConverter.ToUInt16(m_data, 2); } }
      public uint vDriverVersion
         { get { return BitConverter.ToUInt32(m_data, 4); } }
      public uint dwFormats
         { get { return BitConverter.ToUInt32(m_data, 72); } }
      public ushort wChannels
         { get { return BitConverter.ToUInt16(m_data, 76); } }
      public ushort wReserved1
         { get { return BitConverter.ToUInt16(m_data, 78); } }

      public WAVEINCAPS()
      {
         m_data = new byte[WAVEINCAPS_SIZE];
      }

      public string szPname
      {
         get
         {
            char[] bytes = new char[32];
            for (int i = 0; i < 32; i++)
            {
               bytes[i] = (char)BitConverter.ToUInt16(m_data,
                 i * 2 + 8);
            }

            return new string(bytes);
         }
      }
   }

   [DllImport ("coredll.dll")]
   protected static extern Wave.MMSYSERR waveInGetDevCaps(uint
      uDeviceID, byte[] pwic, uint cbwic);
}
[/code][/code]
Last edited by jonathan scott james on 26 Jun 2017, 09:47, edited 1 time in total.
jonathan scott james
Posts: 42
Joined: 05 Apr 2016, 01:56

Re: how can i read the raw data from usb sound device with ahk.

23 Jun 2017, 18:41

can someone help me understand how to input raw data from this usb audio device(which works normally in the system with all applications). someone who can configure calldll in a way that i can integrate into my scripting(if i even need a calldll)
thanks for reading

i caN USE usbview to get the name of the device and it's other factors. but i want to be able to open usb devices the way i can with qbasic in the form:

i'm making a sound mic input data processing circuit. i guess it will have a scope in ahk but this is the qbasic kind of:

screen 9 'sets the graphics ourtput to 640x490 pixels and 80x25 text font
open "lpt2" for binary as #1 'i guess this is arcane to you but it associates the device with #1

checkBuffer:
if eof(#1) then goto checkBuffer: 'if there is no new data then keep checking with "eof"
a=asc(input$(1,#1))*256+asc(input$(1,#1)) 'compile the 16bit binary value from 2 successive bytes

y=y+1 'incriment the y position
pset y,a 'draws a single pixel
if y=460 then print 'this script fills the screen from top to bottom then uses "print" to scroll-
if y=460 then y=y-20 'instead of redrawing the whole screen with the supervisor mode script

goto checkbuffer 'loop till ctrl+"c" or ctrl+"payse/break"
robertcollier4
Posts: 33
Joined: 09 Apr 2016, 22:14

Re: how can i read the raw data from usb sound device with ahk.

24 Jun 2017, 03:25

jonathan scott james wrote:why won't it work?
public class WaveIn
You can't use C# code directly in AutoHotKey, they're two different languages - you would have to translate it.
That is why I recommended using C# (you can download Visual Studio Express for free) since there are already code examples available in C#.
jonathan scott james
Posts: 42
Joined: 05 Apr 2016, 01:56

Re: how can i read the raw data from usb sound device with ahk.

24 Jun 2017, 04:44

is there an ahk for pi yet?
is there an ahk stand alone system that can run the file system and main gui or is it dependent on msie?
jonathan scott james
Posts: 42
Joined: 05 Apr 2016, 01:56

Re: how can i read the raw data from usb sound device with ahk.

24 Jun 2017, 04:49

i'm not getting money refunds or perks from apple or ibm or microsoft so i would like to make a whole system from tyhe cpu up.. but start with xilinx since i know it can hit 2gig lvdp and i assume it can do 1gig ttl. but these bastards are just pissing me off with their nefariousness networks which is now an old boys network of disinformation and antitrust that would have shut Microsoft down off the bat in 1976
jonathan scott james
Posts: 42
Joined: 05 Apr 2016, 01:56

Re: how can i read the raw data from usb sound device with ahk.

24 Jun 2017, 11:29

i want all the script commands that i have learned and all the script commands that i am learning things about to all be the same. i do not want to use c# or any other language

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, mikeyww, rc76 and 218 guests