MP3Player.Mod 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. MODULE MP3Player; (** AUTHOR "TF,PL"; PURPOSE "MP3 Player"; *)
  2. IMPORT
  3. SoundDevices, Codecs, WMDialogs, KernelLog, Streams, Files, Commands;
  4. CONST
  5. Title = "MP3 Player";
  6. TYPE
  7. Player*= OBJECT
  8. VAR decoder : Codecs.AudioDecoder;
  9. soundDevice : SoundDevices.Driver;
  10. playChannel : SoundDevices.Channel;
  11. bufferPool : SoundDevices.BufferPool;
  12. buffer : SoundDevices.Buffer;
  13. in : Streams.Reader;
  14. channels, rate, bits : LONGINT;
  15. ready, paused, finished : BOOLEAN;
  16. pos, oldPos : LONGINT;
  17. setup* : PROCEDURE {DELEGATE} (canSeek: BOOLEAN; totTime, totSamp : LONGINT);
  18. update* : PROCEDURE {DELEGATE} (status: BOOLEAN; pos, displayTime: LONGINT);
  19. eof* : PROCEDURE {DELEGATE} (sender, data: ANY);
  20. (* Initialize Player with the given File *)
  21. PROCEDURE &Init*(in : Streams.Reader; length: LONGINT);
  22. VAR i: LONGINT; res: WORD;
  23. BEGIN
  24. ready := FALSE; finished := FALSE; paused := FALSE; oldPos := 0;
  25. SELF.in := in;
  26. decoder := Codecs.GetAudioDecoder("MP3");
  27. IF decoder = NIL THEN
  28. WMDialogs.Error("Error", "MP3 decoder not installed");
  29. RETURN
  30. END;
  31. soundDevice := SoundDevices.GetDefaultDevice();
  32. NEW(bufferPool, 10);
  33. FOR i := 0 TO 9 DO
  34. NEW(buffer); buffer.len := 4096; NEW(buffer.data, 4096);
  35. bufferPool.Add(buffer)
  36. END;
  37. decoder.Open(in, res);
  38. IF res # 0 THEN
  39. WMDialogs.Error(Title, "File not compatible.");
  40. RETURN
  41. END;
  42. (* Pass the length Information to the Decoder... *)
  43. decoder.SetStreamLength(length);
  44. decoder.GetAudioInfo(channels, rate, bits);
  45. soundDevice.OpenPlayChannel(playChannel, rate, bits, channels, SoundDevices.FormatPCM, res);
  46. IF playChannel = NIL THEN
  47. WMDialogs.Error(Title, "Could not open play channel");
  48. RETURN
  49. END;
  50. playChannel.RegisterBufferListener(bufferPool.Add);
  51. playChannel.SetVolume(255);
  52. ready := TRUE
  53. END Init;
  54. PROCEDURE Play*;
  55. BEGIN {EXCLUSIVE}
  56. playChannel.Start
  57. END Play;
  58. PROCEDURE Stop*;
  59. VAR res : WORD;
  60. BEGIN {EXCLUSIVE}
  61. (* playChannel.Stop *)
  62. playChannel.Pause;
  63. decoder.SeekSample(0, FALSE, res);
  64. pos := decoder.GetCurrentTime();
  65. IF update # NIL THEN update(paused, pos, pos) END
  66. END Stop;
  67. PROCEDURE Pause*;
  68. BEGIN {EXCLUSIVE}
  69. IF paused THEN playChannel.Start; paused := FALSE
  70. ELSE playChannel.Pause; paused := TRUE END
  71. END Pause;
  72. (* set in 1/10 sec *)
  73. PROCEDURE SetPos*(pos: LONGINT);
  74. VAR res : WORD;
  75. BEGIN {EXCLUSIVE}
  76. pos := ENTIER(pos / 10 * rate);
  77. decoder.SeekSample(pos, FALSE, res);
  78. pos := decoder.GetCurrentTime();
  79. IF update # NIL THEN update(paused, pos, pos) END
  80. END SetPos;
  81. (* get in 1/10 sec *)
  82. PROCEDURE GetPos*(): LONGINT;
  83. BEGIN {EXCLUSIVE}
  84. RETURN decoder.GetCurrentTime()
  85. END GetPos;
  86. PROCEDURE Close*;
  87. BEGIN {EXCLUSIVE}
  88. finished := TRUE;
  89. END Close;
  90. BEGIN {ACTIVE}
  91. IF ready THEN
  92. IF setup # NIL THEN setup(TRUE, ENTIER(decoder.GetTotalSamples()/rate*10), ENTIER(decoder.GetTotalSamples()/rate*10)) END;
  93. WHILE decoder.HasMoreData() & ~finished DO
  94. buffer := bufferPool.Remove();
  95. decoder.FillBuffer(buffer);
  96. playChannel.QueueBuffer(buffer);
  97. pos := decoder.GetCurrentTime();
  98. IF (update # NIL) & (pos # oldPos) THEN update(paused, pos, pos) END;
  99. oldPos := pos
  100. END;
  101. playChannel.Close;
  102. KernelLog.String("finished playing..."); KernelLog.Ln;
  103. IF eof # NIL THEN eof(SELF, NIL) END
  104. END
  105. END Player;
  106. VAR player : Player;
  107. PROCEDURE Open*(context : Commands.Context);
  108. VAR
  109. filename : ARRAY 256 OF CHAR;
  110. file : Files.File;
  111. in : Streams.Reader;
  112. BEGIN
  113. context.arg.String(filename);
  114. in := Codecs.OpenInputStream(filename);
  115. IF in = NIL THEN
  116. WMDialogs.Error(Title, "File not found");
  117. RETURN;
  118. END;
  119. file := Files.Old(filename);
  120. NEW(player, in, file.Length());
  121. player.Play;
  122. END Open;
  123. PROCEDURE Stop*;
  124. BEGIN
  125. player.Stop;
  126. END Stop;
  127. END MP3Player.
  128. ------------------------------------------------------------
  129. i810Sound.Install ~
  130. System.Free MP3Player ~
  131. MP3Player.Open htf_44_128.mp3~
  132. MP3Player.Open track.mp3~
  133. MP3Player.Stop~
  134. MP3Player.Open "FAT:/Audio/MP3/MVP/H01 Kopie.mp3"~
  135. MP3Player.Open "FAT:/Audio/MP3/Mpegger/H01 Kopie 1.mp3"~
  136. MP3Player.Open "FAT:/Audio/MP3/AMAZING/H01 Kopie 1.mp3"~