ahitest.pas 4.5 KB

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