summaryrefslogtreecommitdiff
path: root/demo/morphos/ahitest.pas
blob: 61c42ef3391eddec562515f91009f2ce18986fa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
{
    Using AHI device interface to produce sound
    Free Pascal for MorphOS example

    Copyright (C) 2005 by Karoly Balogh

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}

{ * 2005.01.30 * }
{ * Needs MorphOS RTL 2005.01.30 or later! * }

program AHITest;

uses exec,doslib,utility,ahi; // AHI SUXX! :)


const
  FREQUENCY  = 44100;
  STYPE      = AHIST_M16S;
  BUFFERSIZE = 8192;

var
  myTask: PTask;
  oldPri: LongInt;

const
  AHImp : PMsgPort = nil;
  AHIios: Array[0..1] of PAHIRequest = (nil,nil);
  AHIio : PAHIRequest = nil;
  AHIiocopy: Pointer = nil;
  AHIdevice: ShortInt = -1;

  signals: DWord = 0;
  length : DWord = 0;

  link: PAHIRequest = nil;
  tmp : Pointer = nil;

  terminate: Boolean = False;

var
  { * Not an elegant way of buffer allocation, but i don't care. * }
  Buffer1: array[1..BUFFERSIZE] of Integer;
  Buffer2: array[1..BUFFERSIZE] of Integer;
  PB1, PB2: PInteger;

  IOErrCode: LongInt;


procedure cleanup(exitmsg: String; exitcode: LongInt);
begin
  if AHIdevice=0 then CloseDevice(PIORequest(AHIio));
  DeleteIORequest(PIORequest(AHIio));
  FreeMem(AHIiocopy);
  DeleteMsgPort(AHImp);
  SetTaskPri(myTask,oldPri);

  if exitmsg<>'' then writeln(exitmsg);
  halt(exitcode);
end;


{ * Fill up the buffer with some sound data * }
procedure fillbuffer;
var
  counter, counter2: longint;
  sndvalue: integer;
  chunksize: longint;
  chunknum : longint;
begin
  sndvalue:=32767;
  chunknum :=BUFFERSIZE div 32;
  chunksize:=BUFFERSIZE div chunknum;
  for counter:=1 to chunknum do begin
    for counter2:=1 to chunksize do
      pb1[(((counter-1)*chunksize)+counter2)-1]:=sndvalue;
    sndvalue:=0-sndvalue;
  end;
  length:=(BUFFERSIZE*2);
end;


begin
  PB1:=@Buffer1;
  PB2:=@Buffer2;

  myTask:=FindTask(nil);
  oldPri:=SetTaskPri(myTask,10);

  AHImp:=CreateMsgPort();
  if AHImp<>nil then begin
    AHIio:=CreateIORequest(AHImp,sizeof(TAHIRequest));
    if AHIio<>nil then begin
      AHIio^.ahir_Version:=4;
      AHIdevice:=OpenDevice(AHINAME,0,PIORequest(AHIio),0);
    end;
  end;

  if AHIdevice<>0 then
    cleanup('AHI opening error!',20);

  { * Make a copy of the request (for double buffering) * }
  AHIiocopy:=getmem(sizeof(TAHIRequest));
  if AHIiocopy=nil then
    cleanup('Memory allocation failure.',20);

  CopyMem(AHIio, AHIiocopy, sizeof(TAHIRequest));
  AHIios[0]:=AHIio;
  AHIios[1]:=AHIiocopy;

  writeln('Press CTRL-C to exit...');
  SetIoErr(0);

  while (not terminate) do begin

    { * Let's fill up the buffer with some data * }
    fillbuffer;

    { * Setting up IO request * }
    AHIios[0]^.ahir_Std.io_Message.mn_Node.ln_Pri := 127;
    AHIios[0]^.ahir_Std.io_Command := CMD_WRITE;
    AHIios[0]^.ahir_Std.io_Data    := pb1;
    AHIios[0]^.ahir_Std.io_Length  := length;
    AHIios[0]^.ahir_Std.io_Offset  := 0;
    AHIios[0]^.ahir_Frequency      := FREQUENCY;
    AHIios[0]^.ahir_Type           := STYPE;
    AHIios[0]^.ahir_Volume         := $10000;          { * Full volume * }
    AHIios[0]^.ahir_Position       := $8000;           { * Centered    * }
    AHIios[0]^.ahir_Link           := link;

    SendIO(PIORequest(AHIios[0]));

    if link<>nil then begin
      { * Wait until the last buffer is finished * }
      { * (== the new buffer is started) * }
      signals:=Wait(SIGBREAKF_CTRL_C Or (1 Shl AHImp^.mp_SigBit));

      { * Check for Ctrl-C and abort if pressed * }
      if (signals and SIGBREAKF_CTRL_C)>0 then begin
        SetIoErr(ERROR_BREAK);
        terminate:=True;
      end;

      { * Remove the reply and abort on error * }
      if (WaitIO(PIORequest(link)))<>0 then begin
        SetIoErr(ERROR_WRITE_PROTECTED);
        terminate:=True;
      end;
    end;

    link := AHIios[0];

    { * Swap buffer and request pointers, and restart * }
    tmp := pb1;
    pb1 := pb2;
    pb2 := tmp;

    tmp := AHIios[0];
    AHIios[0] := AHIios[1];
    AHIios[1] := tmp;
  end;

  { * Abort any pending IO requests * }
  AbortIO(PIORequest(AHIios[0]));
  WaitIO(PIORequest(AHIios[0]));

  if (link<>nil) then begin
    { * Only if the second request was started * }
    AbortIO(PIORequest(AHIios[1]));
    WaitIO(PIORequest(AHIios[1]));
  end;

  IOErrCode:=IoErr();
  if (IOErrCode<>0) and (IOErrCode<>ERROR_BREAK) then
    cleanup('Device I/O error.',20)
  else
    cleanup('',0);
end.