2
0

ahitest.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. {
  2. $Id$
  3. Using AHI device interface to produce sound
  4. Free Pascal for MorphOS example
  5. Copyright (C) 2005 by Karoly Balogh
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. { * 2005.01.30 * }
  13. { * Needs MorphOS RTL 2005.01.30 or later! * }
  14. program AHITest;
  15. uses exec,doslib,utility,ahi; // AHI SUXX! :)
  16. const
  17. FREQUENCY = 44100;
  18. STYPE = AHIST_M16S;
  19. BUFFERSIZE = 8192;
  20. var
  21. myTask: PTask;
  22. oldPri: LongInt;
  23. const
  24. AHImp : PMsgPort = nil;
  25. AHIios: Array[0..1] of PAHIRequest = (nil,nil);
  26. AHIio : PAHIRequest = nil;
  27. AHIiocopy: Pointer = nil;
  28. AHIdevice: ShortInt = -1;
  29. signals: DWord = 0;
  30. length : DWord = 0;
  31. link: PAHIRequest = nil;
  32. tmp : Pointer = nil;
  33. terminate: Boolean = False;
  34. var
  35. { * Not an elegant way of buffer allocation, but i don't care. * }
  36. Buffer1: array[1..BUFFERSIZE] of Integer;
  37. Buffer2: array[1..BUFFERSIZE] of Integer;
  38. PB1, PB2: PInteger;
  39. IOErrCode: LongInt;
  40. procedure cleanup(exitmsg: String; exitcode: LongInt);
  41. begin
  42. if AHIdevice=0 then CloseDevice(PIORequest(AHIio));
  43. DeleteIORequest(PIORequest(AHIio));
  44. FreeMem(AHIiocopy);
  45. DeleteMsgPort(AHImp);
  46. SetTaskPri(myTask,oldPri);
  47. if exitmsg<>'' then writeln(exitmsg);
  48. halt(exitcode);
  49. end;
  50. { * Fill up the buffer with some sound data * }
  51. procedure fillbuffer;
  52. var
  53. counter, counter2: longint;
  54. sndvalue: integer;
  55. chunksize: longint;
  56. chunknum : longint;
  57. begin
  58. sndvalue:=32767;
  59. chunknum :=BUFFERSIZE div 32;
  60. chunksize:=BUFFERSIZE div chunknum;
  61. for counter:=1 to chunknum do begin
  62. for counter2:=1 to chunksize do
  63. pb1[(((counter-1)*chunksize)+counter2)-1]:=sndvalue;
  64. sndvalue:=0-sndvalue;
  65. end;
  66. length:=(BUFFERSIZE*2);
  67. end;
  68. begin
  69. PB1:=@Buffer1;
  70. PB2:=@Buffer2;
  71. myTask:=FindTask(nil);
  72. oldPri:=SetTaskPri(myTask,10);
  73. AHImp:=CreateMsgPort();
  74. if AHImp<>nil then begin
  75. AHIio:=CreateIORequest(AHImp,sizeof(TAHIRequest));
  76. if AHIio<>nil then begin
  77. AHIio^.ahir_Version:=4;
  78. AHIdevice:=OpenDevice(AHINAME,0,PIORequest(AHIio),0);
  79. end;
  80. end;
  81. if AHIdevice<>0 then
  82. cleanup('AHI opening error!',20);
  83. { * Make a copy of the request (for double buffering) * }
  84. AHIiocopy:=getmem(sizeof(TAHIRequest));
  85. if AHIiocopy=nil then
  86. cleanup('Memory allocation failure.',20);
  87. CopyMem(AHIio, AHIiocopy, sizeof(TAHIRequest));
  88. AHIios[0]:=AHIio;
  89. AHIios[1]:=AHIiocopy;
  90. writeln('Press CTRL-C to exit...');
  91. SetIoErr(0);
  92. while (not terminate) do begin
  93. { * Let's fill up the buffer with some data * }
  94. fillbuffer;
  95. { * Setting up IO request * }
  96. AHIios[0]^.ahir_Std.io_Message.mn_Node.ln_Pri := 127;
  97. AHIios[0]^.ahir_Std.io_Command := CMD_WRITE;
  98. AHIios[0]^.ahir_Std.io_Data := pb1;
  99. AHIios[0]^.ahir_Std.io_Length := length;
  100. AHIios[0]^.ahir_Std.io_Offset := 0;
  101. AHIios[0]^.ahir_Frequency := FREQUENCY;
  102. AHIios[0]^.ahir_Type := STYPE;
  103. AHIios[0]^.ahir_Volume := $10000; { * Full volume * }
  104. AHIios[0]^.ahir_Position := $8000; { * Centered * }
  105. AHIios[0]^.ahir_Link := link;
  106. SendIO(PIORequest(AHIios[0]));
  107. if link<>nil then begin
  108. { * Wait until the last buffer is finished * }
  109. { * (== the new buffer is started) * }
  110. signals:=Wait(SIGBREAKF_CTRL_C Or (1 Shl AHImp^.mp_SigBit));
  111. { * Check for Ctrl-C and abort if pressed * }
  112. if (signals and SIGBREAKF_CTRL_C)>0 then begin
  113. SetIoErr(ERROR_BREAK);
  114. terminate:=True;
  115. end;
  116. { * Remove the reply and abort on error * }
  117. if (WaitIO(PIORequest(link)))<>0 then begin
  118. SetIoErr(ERROR_WRITE_PROTECTED);
  119. terminate:=True;
  120. end;
  121. end;
  122. link := AHIios[0];
  123. { * Swap buffer and request pointers, and restart * }
  124. tmp := pb1;
  125. pb1 := pb2;
  126. pb2 := tmp;
  127. tmp := AHIios[0];
  128. AHIios[0] := AHIios[1];
  129. AHIios[1] := tmp;
  130. end;
  131. { * Abort any pending IO requests * }
  132. AbortIO(PIORequest(AHIios[0]));
  133. WaitIO(PIORequest(AHIios[0]));
  134. if (link<>nil) then begin
  135. { * Only if the second request was started * }
  136. AbortIO(PIORequest(AHIios[1]));
  137. WaitIO(PIORequest(AHIios[1]));
  138. end;
  139. IOErrCode:=IoErr();
  140. if (IOErrCode<>0) and (IOErrCode<>ERROR_BREAK) then
  141. cleanup('Device I/O error.',20)
  142. else
  143. cleanup('',0);
  144. end.
  145. {
  146. $Log$
  147. Revision 1.1 2005-01-30 20:03:43 karoly
  148. * initial revision
  149. }