Recent

Author Topic: Can I play an mp3 sound file on Windows without relying on a dll?  (Read 1984 times)

EganSolo

  • Sr. Member
  • ****
  • Posts: 290
Not sure if I should ask this question in the Windows subforum or the multimedia one ...
Anyway, like the subject says: could I play an mp3 sound file without loading a DLL on Windows 10 and 11?

TRon

  • Hero Member
  • *****
  • Posts: 2503
Re: Can I play an mp3 sound file on Windows without relying on a dll?
« Reply #1 on: May 04, 2023, 07:34:49 am »
Nope.

Or actually yes, but in that case you will not have any sound output.

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Can I play an mp3 sound file on Windows without relying on a dll?
« Reply #2 on: May 04, 2023, 08:17:50 am »
Hello,
on Windows you can try to use the windows media player COM object :
Example :
Code: Pascal  [Select][+][-]
  1. var
  2.   Form1: TForm1;
  3.   OWMP: OleVariant;
  4. implementation
  5.  uses ComObj;
  6. {$R *.lfm}
  7. { TForm1 }
  8. procedure TForm1.FormCreate(Sender: TObject);
  9. begin
  10.    OWMP := CreateOleObject('WMPlayer.OCX.7');
  11. end;
  12. procedure TForm1.BtPlayClick(Sender: TObject);
  13. begin
  14.    OWMP.Url := 'D:\Zic\Merci Patron.mp3';
  15. end;
  16. procedure TForm1.BtStopClick(Sender: TObject);
  17. begin
  18.   OWMP.Controls.Stop;
  19. end;
  20.  

Friendly, J.P
                 
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

TRon

  • Hero Member
  • *****
  • Posts: 2503
Re: Can I play an mp3 sound file on Windows without relying on a dll?
« Reply #3 on: May 04, 2023, 08:27:20 am »
@Jurassic Pork:
Still loading DLL's there (whether it be directly or indirectly)  ;D

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Can I play an mp3 sound file on Windows without relying on a dll?
« Reply #4 on: May 04, 2023, 08:29:38 am »
@Jurassic Pork:
Still loading DLL's there (whether it be directly or indirectly)  ;D
but invisible and no dll install necessary  :-X
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

TRon

  • Hero Member
  • *****
  • Posts: 2503
Re: Can I play an mp3 sound file on Windows without relying on a dll?
« Reply #5 on: May 04, 2023, 08:36:20 am »
but invisible and no dll install necessary  :-X
True but, not what was asked for (though I do not know the intention of TS with the phrase "without loading a dll").

I would personally prefer to use mmsystem for playing sound (but that's just me).

Thaddy

  • Hero Member
  • *****
  • Posts: 14363
  • Sensorship about opinions does not belong here.
Re: Can I play an mp3 sound file on Windows without relying on a dll?
« Reply #6 on: May 04, 2023, 10:31:12 am »
Yes, you can, since a native MP3 in Pascal is available for years. (About 20, mine in 2003 for Delphi, based on German Magazine code and I did just KOL)
I translated it to KOL, but I can also translate it back. (Since the original disappeared because of then current rights, nowadays there are no restrictions)

BUT: under Windows you will always rely on some dll's from Windows audio drivers as explained above, but that is basically the OS.

Note that my code is basically only of interest to explain how MP3 is encoded/decoded. I would not recommend it since Windows supports mp3 natively. It may be of interest for cross-platform.
My code also contains a small bug, since the encoding uses signed integer, so 1 bit is lost.(Usually not audible)

Ping me if you want that code. Note it is very complex so if you are not a very, very capable programmer it would be useless to you.
The code only compiles in $mode delphi, but it works. I will have a look on some old disks if you can not google it. (20 years ago is a long time)
« Last Edit: May 04, 2023, 11:05:47 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: Can I play an mp3 sound file on Windows without relying on a dll?
« Reply #7 on: May 04, 2023, 12:04:45 pm »
With Windows you have a built in video/audio player.
For audio, with your own path to a file:
Code: Pascal  [Select][+][-]
  1.   function mciSendString(s: pchar; p1: pchar; p2: integer; p3: integer): integer;
  2.   cdecl external 'Winmm.dll' Name 'mciSendStringA';
  3.  
  4. function playaudio(filename:ansistring):integer;
  5. var
  6. open:ansistring;
  7. begin
  8. Open := 'open  ' + filename + ' type mpegvideo alias file1';
  9. mciSendString(pchar(open), Nil, 0, 0);
  10.  exit(mciSendString('play file1', nil, 0,0));
  11. end;
  12.  
  13.  
  14. begin
  15. writeln(playaudio('C:\Users\Computer\LeavingOnAJetPlane.mp3'));
  16. readln;
  17. end.
  18.  
Oops ..., just realized that I use a dll, albeit a built in one.
Do you mean write a decoder from first principles?


« Last Edit: May 04, 2023, 12:19:15 pm by BobDog »

EganSolo

  • Sr. Member
  • ****
  • Posts: 290
Re: Can I play an mp3 sound file on Windows without relying on a dll?
« Reply #8 on: May 04, 2023, 08:37:28 pm »
Thanks, everyone for your thorough and detailed answer.
As @Tron pointed out, I was looking for a solution that would be pure Pascal. We have solutions to load images that do not require any DLL so I thought (perhaps naively) that the same could be achieved without sound.
@Jurassic Pork: To your point, relying on a native DLL is, in my mind, better than loading a library, so thank you for your code. The same goes for @Bobdog for the code. Yes, both of these snippets rely on native DLLs but it may be better than loading a different library.

@Thaddy: Is there even a sliver of a chance that your code could be turned into a pure Pascal mp3 player component? I thought I should ask!

TRon

  • Hero Member
  • *****
  • Posts: 2503
Re: Can I play an mp3 sound file on Windows without relying on a dll?
« Reply #9 on: May 04, 2023, 08:49:09 pm »
@EganSolo:
Decoding MP3, turning it into waveforms all that can be done 'natively' in Pascal.

The part that definitely can not be done in Pascal without relying on some OS functionality (on Windows that does mean relying on a dll) is outputting that waveform so that it actually generates sound that you can listen to. No problem storing the waveforms f.e. to a wav file.

edit Since you asked (and for Windows only) , perhaps try https://sourceforge.net/projects/delphimpeg/ ?
« Last Edit: May 04, 2023, 10:46:02 pm by TRon »

 

TinyPortal © 2005-2018