Autres articles / Other articles

Utiliser des fichiers wav avec PlaySoundA

publication: 21 novembre 2026 / mis à jour 21 novembre 2026

Read this page in english

 

Le mot PlaySoundA

PlaySoundA est un mot qui donne accès à l'API multimédia Windows (Win32) utilisée pour lire des fichiers audio WAV, des sons système ou des ressources audio. Il ne prend pas en charge les fichiers MP3.

Pour accéder aux fonctions de l'API Multimédia, il faut utiliser le contenu de ce fichier:
Winmm definitions for musics vocabulary

Si vous respectez localement la structure des fichiers de ce dépôt sur votre espace de travail, vous pourrez intégrer le contenu de Kernel32-definitions.fs de cette manière:

\ stop compilation before end of included file 
: <EOF> ( -- ) 
    #tib @ >in ! ; 
 
\ load dump tool extensions 
needs tools/dumpTool.fs 
 
\ load Winmm extensions 
needs extensions/Winmm-definitions.fs 

Lecture directe d'un fichier WAV

La plupart des fichiers audio de forme d’onde utilisent l'extension de nom de fichier WAV.

Voici la définition de PlaySoundA:

\ ** PlaySound flags ** 
$00000000 constant SND_SYNC 
$00000001 constant SND_ASYNC 
$00000002 constant SND_NODEFAULT 
$00000004 constant SND_MEMORY 
$00000008 constant SND_LOOP 
$00000010 constant SND_NOSTOP 
$00000040 constant SND_PURGE 
$00000080 constant SND_APPLICATION 
$00002000 constant SND_NOWAIT 
$00010000 constant SND_ALIAS 
$00020000 constant SND_FILENAME 
$00040004 constant SND_RESOURCE 
 
\ Plays a sound specified by the given file name, resource, or alias 
z" PlaySoundA" 3 Winmm PlaySoundA ( pszSound hmod fdwSound -- fl ) 

Le mot PlaySoundA accepte 3 paramètres:

Concernant Le paramètre pszSound:

Concernant Le paramètre fdwSound:

Voici plusieurs définitions qui jouent le contenu d'un fichier WAV:

\ play a .wav file, return immediately 
: PlayWav ( zFile -- ) 
    0 SND_FILENAME SND_ASYNC or PlaySoundA mm-success 
  ; 
 
\ play a .wav file, wait until the end 
: PlayWavSync ( zFile -- ) 
    0 SND_FILENAME PlaySoundA mm-success 
  ; 
 
\ play a .wav file in loop 
: PlayWavLoop ( zFile -- ) 
    0 SND_FILENAME SND_ASYNC or SND_LOOP or PlaySoundA mm-success 
  ; 
 
\ stop any sound played by PlaySound 
: StopSound ( -- ) 
    0 0 0 PlaySoundA mm-success 
  ; 

Exemple de traitement d'un fichier WAV:

musics 
z" MUSICS	ests\cockcrow.wav" PlayWav 
musics 
 
\ set main volume 
$ffff $ffff SetVolume 
 
: play-alias ( zStr -- ) 
    0                 \ hmod = NULL 
    SND_ALIAS SND_ASYNC or 
    PlaySoundA mm-success  
  ; 
 
z" SystemAsterisk"  play-alias 
z" SystemExclamation" play-alias 
z" SystemHand"      play-alias 
 
\ Fichier .wav, lecture asynchrone (rend la main tout de suite) 
z" C:\Windows\Media	ada.wav" 0 SND_FILENAME SND_ASYNC or PlaySoundA mm-abort 
 
\ Fichier .wav, lecture asynchrone (rend la main tout de suite) 
z" C:\Users\mpmp9\Downloads\cockcrow.wav" 0 SND_FILENAME SND_ASYNC or PlaySoundA  
 
z" C:\Users\mpmp9\OneDrive\Bureauforth\MUSICS	ests\cockcrow.wav" 0 SND_FILENAME SND_ASYNC or PlaySoundA  
 
cd tests 
z" cockcrow.wav" 0 SND_FILENAME SND_ASYNC or PlaySoundA  
 
 
 
\ Même chose avec le mot utilitaire 
z" C:\Windows\Media	ada.wav" PlayWav 
 
\ Lecture synchrone : le mot ne rend la main qu'à la fin du son 
z" C:\Windows\Media	ada.wav" PlayWavSync 
 
\ Lecture en boucle, puis arrêt après 2 secondes 
z" C:\Windows\Media\chimes.wav" PlayWavLoop 
2000 ms 
StopSound 

Legal: site web personnel sans commerce / personal site without seling