micrecord.pp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. program micrecord;
  2. {$mode objfpc}
  3. uses
  4. ctypes, nds9, maxmod9;
  5. const
  6. //the record sample rate
  7. sample_rate = 8000;
  8. var
  9. //buffer to hold sound data for playback
  10. sound_buffer: pcuint16 = nil;
  11. //buffer which is written to by the arm7
  12. mic_buffer: pcuint16 = nil;
  13. //the length of the current data
  14. data_length: cuint32 = 0;
  15. //enough hold 5 seconds of 16bit audio
  16. sound_buffer_size: cuint32 = sample_rate * 2 * 5;
  17. //the mic buffer sent to the arm7 is a double buffer
  18. //every time it is half full the arm7 signals us so we can read the
  19. //data. I want the buffer to swap about once per frame so i chose a
  20. //buffer size large enough to hold two frames of 16bit mic data
  21. mic_buffer_size: cuint32 = sample_rate * 2 div 30;
  22. //mic stream handler
  23. procedure micHandler(data: pointer; length: cint);
  24. begin
  25. if (sound_buffer = nil) or (data_length > sound_buffer_size) then
  26. exit;
  27. DC_InvalidateRange(data, length);
  28. dmaCopy(data, pcuint8(sound_buffer) + data_length, length);
  29. data_length := data_length + length;
  30. iprintf('.');
  31. end;
  32. procedure rec();
  33. begin
  34. data_length := 0;
  35. soundMicRecord(mic_buffer, mic_buffer_size, MicFormat_12Bit, sample_rate, @micHandler);
  36. end;
  37. procedure play();
  38. begin
  39. soundMicOff();
  40. soundEnable();
  41. iprintf('data length: %li'#10, data_length);
  42. soundPlaySample(sound_buffer, SoundFormat_16Bit, data_length, sample_rate, 127, 64, false, 0);
  43. end;
  44. var
  45. key: cint;
  46. recording: cbool = false;
  47. begin
  48. getmem(sound_buffer, sound_buffer_size);
  49. getmem(mic_buffer, mic_buffer_size);
  50. consoleDemoInit();
  51. iprintf('Press A to record / play'#10);
  52. while true do
  53. begin
  54. scanKeys();
  55. key := keysDown();
  56. if(key and KEY_A )<> 0 then
  57. begin
  58. if recording then
  59. begin
  60. play();
  61. iprintf('playing');
  62. end else
  63. begin
  64. rec();
  65. iprintf('recording');
  66. end;
  67. recording := not recording;
  68. end;
  69. //-----------------------------------------------------
  70. // START: exit
  71. //-----------------------------------------------------
  72. if (keys and KEY_START) <> 0 then break;
  73. swiWaitForVBlank();
  74. end;
  75. end.