comp.lang.ada
 help / color / mirror / Atom feed
From: John McCabe <john@nospam.assen.demon.co.uk>
Subject: This MIDI stuff, would someone be interested in reviewing my code?
Date: Mon, 08 Mar 2010 11:40:16 +0000
Date: 2010-03-08T11:40:16+00:00	[thread overview]
Message-ID: <uun9p5dv8raemp9rrrbmfl08i8fqhjgqe9@4ax.com> (raw)

Hi

It's still early days but, if I'm going to be using Ada to try to
build this app I want, it would be nice to write it in a style that
looks appropriate. I'm aware of the Q&S guide but I was hoping that
someone could take a quick look at the code I've written (it's only 80
lines or so, and it's down below) and see if there's anything
obviously stupid I'm doing.

My specific thoughts on this are:

1) Perhaps I should be using limited withs in some places to get
access to the primitive operators/functions of the stuff in
Interfaces.C/.strings and Win32 etc.

2) The for loops: for devId in Win32.UINT range 0..(NumOutputDevices -
1) etc. These are protected by a "if NumOutputDevices < 0" condition
but before I realised my mistake I found that when NumOutputDevices is
0, the loop executes as many times as it can before it crashed. This
was obviously because NumOutputDevices was 0, so the range
"0..(NumOutputDevices - 1)" was 0..4929blahblah due to Win32.UINT
being a modular type. I looked at the option to use something like:
   for index in Win32.UINT range 1..NumOuputDevices loop
      declare
         devId : Win32.UINT := index - 1;
      begin
         ...
      end;
   end loop;
but stuck with the original with the conditional round it.

3) Would it be more appropriate to use something like
Win32.UINT'Image() instead of getting an instantiation of the
Modular_IO package?

Anyway - thanks to anyone who can be bothered to look at this. It will
be much appreciated, and thanks for everyone's help so far.

John


===================
with Ada.Text_IO;
with Ada.Unchecked_Deallocation;

with Interfaces.C; use Interfaces.C;
with Interfaces.C.Strings; use Interfaces.C.Strings;

with Win32; use Win32;
with Win32.Mmsystem; use Win32.Mmsystem;

procedure MidiDevs is
    NumInputDevices  : Win32.UINT;
    NumOutputDevices : Win32.UINT;

    res         : Win32.Mmsystem.MMRESULT;
    midiInCaps  : Win32.Mmsystem.LPMIDIINCAPS;
    midiOutCaps : Win32.Mmsystem.LPMIDIOUTCAPS;

    package UINTText_IO is new Ada.Text_IO.Modular_IO(Win32.UINT);
    package MMText_IO is new
Ada.Text_IO.Modular_IO(Win32.Mmsystem.MMRESULT);

    procedure Free is new
Ada.Unchecked_Deallocation(Win32.Mmsystem.MIDIINCAPS,
Win32.Mmsystem.LPMIDIINCAPS);
    procedure Free is new
Ada.Unchecked_Deallocation(Win32.Mmsystem.MIDIOUTCAPS,
Win32.Mmsystem.LPMIDIOUTCAPS);

begin
   NumInputDevices := Win32.Mmsystem.midiInGetNumDevs;
   NumOutputDevices := Win32.Mmsystem.midiOutGetNumDevs;
   midiInCaps  := new Win32.Mmsystem.MIDIINCAPS;
   midiOutCaps := new Win32.Mmsystem.MIDIOUTCAPS;

   Ada.Text_IO.Put("There are ");
   UINTText_IO.Put(NumInputDevices, 0);
   Ada.Text_IO.Put(" input devices available, and ");
   UINTText_IO.Put(NumOutputDevices, 0);
   Ada.Text_IO.Put_Line(" output devices available.");

   if NumInputDevices > 0
   then
      Ada.Text_IO.New_Line;
      Ada.Text_IO.Put("The ");
      UINTText_IO.Put(NumInputDevices, 0);
      Ada.Text_IO.Put_Line(" input devices are:");
      Ada.Text_IO.New_Line;

      for devId in Win32.UINT range 0..(NumInputDevices - 1)
      loop
         res := Win32.Mmsystem.midiInGetDevCaps(devId,
                                                midiInCaps,

(Win32.Mmsystem.MIDIINCAPS'size * Win32.BYTE'size));
         UINTText_IO.Put(devId, 0);
         Ada.Text_IO.Put(") ");
         if res = Win32.Mmsystem.MMSYSERR_NOERROR
         then
            Ada.Text_IO.Put("szPname = ");
            Ada.Text_IO.Put_Line(To_Ada(To_C(midiInCaps.szPname)));
         else
            Ada.Text_IO.Put("Query Failed. Returned ");
            MMText_IO.Put(res, 0);
         end if;
         Ada.Text_IO.New_Line;
      end loop;
   end if;

   if NumOutputDevices > 0
   then
      Ada.Text_IO.New_Line;
      Ada.Text_IO.Put("The ");
      UINTText_IO.Put(NumOutputDevices, 0);
      Ada.Text_IO.Put_Line(" output devices are:");
      Ada.Text_IO.New_Line;

      for devId in Win32.UINT range 0..(NumOutputDevices - 1)
      loop
         res := Win32.Mmsystem.midiOutGetDevCaps(devId,
                                                 midiOutCaps,

(Win32.Mmsystem.MIDIOUTCAPS'size * Win32.BYTE'size));
         UINTText_IO.Put(devId, 0);
         Ada.Text_IO.Put(") ");
         if res = Win32.Mmsystem.MMSYSERR_NOERROR
         then
            Ada.Text_IO.Put("szPname = ");
            Ada.Text_IO.Put_Line(To_Ada(To_C(midiOutCaps.szPname)));
         else
            Ada.Text_IO.Put("Query Failed. Returned ");
            MMText_IO.Put(res, 0);
         end if;
         Ada.Text_IO.New_Line;
      end loop;
   end if;

   Free(midiInCaps);
   Free(midiOutCaps);
   
end MidiDevs;




             reply	other threads:[~2010-03-08 11:40 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-08 11:40 John McCabe [this message]
2010-03-08 11:52 ` This MIDI stuff, would someone be interested in reviewing my code? Gautier write-only
2010-03-08 12:30   ` John McCabe
2010-03-08 17:24 ` Jeffrey R. Carter
2010-03-09 10:21   ` John McCabe
2010-03-09 12:10     ` Brian Drummond
2010-03-09 12:26       ` John McCabe
2010-03-09 12:42         ` John McCabe
2010-03-10 11:12           ` Stephen Leake
2010-03-10 12:29             ` John McCabe
2010-03-09 12:24     ` John McCabe
2010-03-09 13:11       ` Martin
2010-03-09 14:00         ` John McCabe
2010-03-13  8:12 ` Christophe Chaumet
2010-03-15 11:35   ` John McCabe
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox