Author Topic: Playing WAV sound file from code memory (string data) instead of a file  (Read 4211 times)

0 Members and 1 Guest are viewing this topic.

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
For a long time I've been trying to come up with a way to put small sound files directly in the code instead of using external WAV files to play.  This is what I've come up with that is working, so I thought I'd share it here in case others wanted to put small sound files in their code (for button clicks, etc..).  It uses the PlaySoundA Win API call, so this is for Windows only.

A PlaySoundA example is in the QB64 wiki here, but what's not shown there is the flag that allows the function to play from memory instead of a file (SND_MEMORY).  Below is an example of using the function in that way.  To put the WAV file into the BAS code I converted the  WAV file to string data using the program here.

The code below plays a coyote howling sound until a keypress.

- Dav

Code: QB64: [Select]
  1. '============
  2. 'MEMSOUND.BAS
  3. '============
  4. 'Plays a WAV file from memory (string) using Win API call.
  5. 'Coded by Dav, AUG/2020
  6.  
  7.     FUNCTION PlaySoundA& (lpszName AS STRING, BYVAL hModule AS LONG, BYVAL dwFlags AS LONG)
  8.  
  9. CONST SND_APPLICATION = 128 '  look for application specific association
  10. CONST SND_ALIAS = 65536 '  name is a WIN.INI [sounds] entry
  11. CONST SND_ALIAS_ID = 1114112 '  name is a WIN.INI [sounds] entry identifier
  12. CONST SND_ASYNC = 1 '  play asynchronously
  13. CONST SND_FILENAME = 131072 '  name is a file name
  14. CONST SND_LOOP = 8 '  loop the sound until next PlaySound
  15. CONST SND_MEMORY = 4 '  lpszSoundName points to a memory file
  16. CONST SND_NODEFAULT = 2 '  silence not default, if sound not found
  17. CONST SND_NOSTOP = 16 '  don't stop any currently playing sound
  18. CONST SND_NOWAIT = 8192 '  don't wait if the driver is busy
  19. CONST SND_PURGE = 64 '  purge non-static events for task
  20. CONST SND_RESOURCE = 262148 '  name is a resource name or atom
  21. CONST SND_SYNC = 0 '  play synchronously (default)
  22.  
  23. wav$ = Basfile$ 'load wav data string
  24.  
  25. PRINT "Playing wave file from memory."
  26. PRINT "Press any key to stop sound and exit...."
  27.  
  28. 'Play a sound from memory data...looping it...
  29. x& = PlaySoundA(wav$, 0, SND_MEMORY OR SND_ASYNC OR SND_LOOP)
  30.  
  31.  
  32. x& = PlaySoundA(CHR$(0), 0, SND_MEMORY) '<<<--- stop sound
  33.  
  34.  
  35.  
  36. FUNCTION Basfile$ () 'Coyote howl .WAV file converted with BASFILE.BAS
  37.     A$ = ""
  38.     A$ = A$ + "haIYkm7JKTWUWInWSUSMhfKHPUIPMXWVIXkVdTVdQ4L2f47\g4\=FHK\>IS4"
  39.     A$ = A$ + "FJUF9T4B89:AE5EAm;ZRZ:Z2E29T4B8QT#2;S=f6K\3fT#B8QT#29Q4BXkVd"
  40.     A$ = A$ + "\c`d<m#?LkO];km77L7L__O[Bo`fYgM^kDCCWnE_jm[_O[gkckkkKFJVKNco"
  41.     A$ = A$ + "n[N^cii6ognkST<k7mgL^cM^O2l?oG?oi`ok?iLoEWkOlO8ko`_6hAloon9L"
  42.     A$ = A$ + "bO>DUTLN6_c_Q?E2mCdn9L>?biMTTUUA?3N0lKdW8;[X8SN27;;;lX1jAFAE"
  43.     A$ = A$ + "CCEFPSUVVVQC#BAEA6T^X\VQUUY2Gfd9SW8=UPVEaRiDhIhT=;g\E=3ZP;iJ"
  44.     A$ = A$ + ">GNC:F\E_NediXHeK\ka7]QAhKlIOd7o5c4gJ[kmX3KZBC[gmn_n]?\NlanI"
  45.     A$ = A$ + "WkLo\[4`Lg7oh3KZc;FLWWl^gm`Ja^b?kWlCniS5:g^goNkDECD[n1_h[Oc]"
  46.     A$ = A$ + "=GmloW?goY?jFI:_c]gOm:i;fjPWme_m^UR>aOgOm?mSVH=f2=KgXJm^koXG"
  47.     A$ = A$ + "oE_l`2a_iWm;oUOb4NB:JV_HU>kLgWmfgnX=D3?cEnl;LUKj>LJ6YL=gkQ_j"
  48.     A$ = A$ + "[NcSfb:[o5_e=6oJS?U;_a5ZNh;NoOhm?KgR\9S69`bghROjWMUiRWOoGnko"
  49.     A$ = A$ + "ngoPf:4YYTdhSLc?mGngm5;`dkiOgOnK_G5Z8A9E]<i3Oc;Nn[jV\jQ?me?k"
  50.     A$ = A$ + "PJZl2BBL4QGJjK<O8fl=Jg^A9[2ej]mjUHl=eHC\DJcV]:9ATP#Mm7m^G^Sh"
  51.     A$ = A$ + "RGjcWH=YVmZ#_j\;6?oaOcOleMBM[aVL=aZ=dSL[[=MhR?lknc_^Nli_YKb:"
  52.     A$ = A$ + "M;C<kWOnKTXicoSOgm=3_\?VV7\K^EnToVOah4KmEo`Gg;c2c6:oa_l1E3Na"
  53.     A$ = A$ + "Oa_j6TmNbCgTggRn5glI_j0aIo]OdeB^n[nWNTiBGhCV<C_GmnkKjn;_h2B7"
  54.     A$ = A$ + "lEOcA:^7kB^hjMgm=l>nHcC_mgl?o]KA>gE^fZbMfZJf5_a]2XO`KnZkE>KP"
  55.     A$ = A$ + "ebT[M;=oSmK>o\dKm^odkjAiN6?LmN`mJVNj;LQIbfn1?mPlYmi;SejKgPem"
  56.     A$ = A$ + "4Gg3Gg7oZSc5Lj[=OVZ7Lkf<;>aeMcfkY_maMhl=o21Id<4Bjc38g=>nX^jI"
  57.     A$ = A$ + "S6QD_BESD;<nhcWZjmNgKfC9`Lc7BMSkNkJIVoZGM1b6gia7F8e:;iW^lf7^"
  58.     A$ = A$ + "A^<nF92OiekF8[WIV;P#c3PmP9FMeDFkmT7]9W[?oS_hb2m^g3ge:5#\`n?n"
  59.     A$ = A$ + "AK[^fe_hELUbXA[bJ<4TbEgih3J:5IQK=WgdB66ZRl\\BF=gHSFFL92heGXT"
  60.     A$ = A$ + "T\R`7#::j5ZeZII=jh1l7>5]TVR0MF2JUR]F_K5ah[^TWX\VU[EBS=C:BAS2"
  61.     A$ = A$ + "U:J8T9LP#9Id:E_NeR6:RLdTTDDd\`GPPF4e;D^LA;MINZd9BT<3UPAa:5ei"
  62.     A$ = A$ + "Y8I4e<d1fIHHiUDeeeP?Oi8CU891SP\ZV^I>[REZEaB<CHO[^Fhd\bZ`Ga2:"
  63.     A$ = A$ + "Fej\NW:j<YR4:JB2::b\IYhT=bG8_YV\Mb6Df1hOIXYXXHh5aI5hUd<cWg#V"
  64.     A$ = A$ + "SRPPT5f6;0i3H5ebIUCCPV8340DE1cQUUGa8WY:b_cb:29;ThIi#FTVR8;59"
  65.     A$ = A$ + "5[P<>O2?lO=<1Mbc#3:1RMB2P3cQBlPB2a9Z2?65#PoX\VZ4;IjDIX4ecG<_"
  66.     A$ = A$ + "^83fb4iI#V?B=Z#b;AI8>X`CU9=1ST>H4`O<39_:\RaU=EbddBV5:TC5Y#15"
  67.     A$ = A$ + "MMEAFb\T\BJ66J#n=8>:2I7iAaO>PXY1:N8:QT=X72DH2RZV#P6UC4L1V\YC"
  68.     A$ = A$ + "U>;4^ed=de0_2R0BP=AC1RPV3iPPXAn5TY5U#6WdE#2UZe9o<oYc[;:iTe7R"
  69.     A$ = A$ + "1<#2GFL6G#`X4g`?1Ho1A628<Z0BGG3ig2LXTf#83Ue6T>bL`fLFBJnni[U`"
  70.     A$ = A$ + "TaKH#IaAB2N5`UA?G2iXXo4#52]hllh:5h:0bb`FAJlVBl^LK;AL#a2#_hG7"
  71.     A$ = A$ + ";7T[8IG81I?fUR`Ba^`SS?kDWZOi=k#SXS[fW\C03;KWbClHODdA?HMRHS7E"
  72.     A$ = A$ + "2c^Xg`#i`2XXMdfV3Z0HoBZX#<jc7l:BOaS[JbI7<kC^?of6oP:`3=OK]HGQ"
  73.     A$ = A$ + "M0IW:_B3n9:N2I8egH`UY?Aik1Uf?K]oAnOi?93SnBWaok7d57J:H:3G;`L>"
  74.     A$ = A$ + "b>#7J>6TmB=:BQ=IDeOdG=<D`WihTEhg6n0dd:?T^2BC0OK0I:ha_>\Wd<OY"
  75.     A$ = A$ + "b5=5YB5;D`XIh#91T44TecG6:CWG8EPEF`UWX<6UZE]PV0_Jaf0HQNJQWnRO"
  76.     A$ = A$ + "b_iBcCDJcmfZA>AN]Vgni_mUk[kmBoROj?ocMAFKgK_CCCAQL[oPgmnG^9k\"
  77.     A$ = A$ + "OlOmOe_H<o:MfKg>5D4=JNd[NoK?b:`UoKoYolc?KL]jMKGab\#[mOb[OaAU"
  78.     A$ = A$ + "ROSOkOk_lcV:0S9HLi;gN_7lbGodM=SN[_hglK_d]lUREF;GUe?hQ_l=?oXj"
  79.     A$ = A$ + "\nWKRa^jTci9#\<LFM?jY_mM_h>]DB7`mLC=a4C>cB1bXgihWmfgmlKGG6bg"
  80.     A$ = A$ + "4?TWKM];MUYmBGkhglkoM_JoR\#jidYChOQ97OJ?Y<ghQ_60KDD6]eT9Qk9_"
  81.     A$ = A$ + "lEW9X#S3NdC^o>e=P4XTddIS]f:[j?6]FaJ=JfNm=je^THhI6kbCiS]bVk]I"
  82.     A$ = A$ + "=EbDI4:\nnkge8a2GoJgLeTL:jVi[^nN7Ok>B1Vi6c6BZn6=UFKZaVd[bnOk"
  83.     A$ = A$ + "ofob[J4OZ;NiKi?==lQ>WNm]fKmLTaRcDH[S?\L`;ncn_lKL9OlgoojOi9im"
  84.     A$ = A$ + "?mD[#FXI5e<1G;:[ANEbX1RbGohGmZMIVnSniObbiNlomoegE?l4GhZ[`gHW"
  85.     A$ = A$ + "]ZYaaKDYIIne^eeW=NYknfWe8lHOd7?Ifmn^ogo?NVYW;mKo2GI;fZZ6=1SN"
  86.     A$ = A$ + "ef=bA6LU0D5giPf<^^hWlU;ZL_OhoiOL?f5_fEG8KieKXT`_oD:ejeaR`kRn"
  87.     A$ = A$ + "XZ\gMfDag4GLLo5Ndgo3?>W_[Ma[7dHSmJ[4O]#d`Cg]#Ri7Oj0:KLlfjQ^e"
  88.     A$ = A$ + "eV:Pemo^oiGg<i<S=^7f:]Qb_1R#UOc3gY<G4_nB[gLcfJaGNf529fjfgKo:"
  89.     A$ = A$ + "La37W=GmjFdQFaG6cfk\EQD;=m]FUYm3Okcja^le7g5Ae3OdaUR=chGOQTVm"
  90.     A$ = A$ + "^maKGAUWGBFT>nZ;hbMX\bi[e^CcZ5bYSb:cU=dRglV;6R[b^go1KWW:F2nB"
  91.     A$ = A$ + "MkF8iDOj_n;FPKSWn^W]>OXeR[eiPmK#kj;o\;_0Aa]>l`]KE>_Y^#fh1341"
  92.     A$ = A$ + ":J[8b#bXgL_Sg]LfU_aDN5KO`>5B_bRNYKmT_n5M9Vm:GIN01lQ]UR^jZaDJ"
  93.     A$ = A$ + "OhMfb:S_UmVBYHA5R8QBbGJSmgZ4Y_5G>2?\UYG1V0;]H0j2MjEG;C#_nC8D"
  94.     A$ = A$ + "ZkF[GEQ<Nh898X1Hn4Y9X5=:F_Im:Fj#iK6RH[iamZQb8T_e6Kd^ZU6:_U0I"
  95.     A$ = A$ + "<O^M_FL6E[2i]b1HZ[D0fQhgOL6S:]KEF9Kd#ABaZU_Li26b\Ic#`X0aW^]:"
  96.     A$ = A$ + ":4KiIWeM2eVK_M3UD13VD4]E^RDj#126;33DY2:_ZSB?0k#A5g=PBnJl49RW"
  97.     A$ = A$ + "2PoDXH>9bT9c:HFYFI3^D134WBBGCP:MJ:iBMg\M1Q<aRV8;_X0MZHaBA[T_"
  98.     A$ = A$ + "LUb5`0=8cT>3hKJP5S0aQ#S;Q0L1OY1`B#H8T\[09bAU=;]PJ^RUbKXP0<P`"
  99.     A$ = A$ + "U`CC0X8#MjDZBilJ#YDX#?0F3Q0`<G>3=ETU[:aT:\OoQCc3#e:WG70M9Q#o"
  100.     A$ = A$ + "d7^P8je[2_??EZhaRWRPR1EY7`PEX4`^02P5X>La?03Dn2FjRXe40BSUUQ4M"
  101.     A$ = A$ + "jTY8iah^0D#0N:A4D5MD7J<h>dPg1B13QLB0I4PSA3mfIXUbE^FUl:\ICW6X"
  102.     A$ = A$ + "S0gAc:IDXL9He1`M1h4T5I#XT81H>8iidU9^T:Z^96I:9=6J4P75nOH40XEJ"
  103.     A$ = A$ + "B<4YBA`:KF03<3H1:7`356bBa[HJIUC7P?Pa4P#A1#G09HJQ`AIG>G`6[3Zh"
  104.     A$ = A$ + "20O3`a2<jdNZ]RA#OLPQ084I`L:?0Z1N_XCe9WbM>o7KH\>##hiko7i]am8e"
  105.     A$ = A$ + "7E32l9D<BAZO9LaAkee6>RPC7c31SPA60QL6K\B:8c#Ff1?b0\HPI8>0IdP:"
  106.     A$ = A$ + "ofFV8JO>2J6o8o`ET]MFTh]mFA9<fYn5mi`=XcgW=inmckP=ZOIk1A1WXT=O"
  107.     A$ = A$ + "S2Oa14PmQ98L2fLHL49MB\734^]B7ZYnbJ0>?KKNhC6RIaAnS04Q_?LYcZ[A"
  108.     A$ = A$ + "A7?0mXd9\[cjkWl]565Mh90AaO6fL\bANRS6`k2]a=b=lhCDDe=#P[IA]CAB"
  109.     A$ = A$ + "chPe4g6FnlidQVGbT:Ej<X_=#d1DB5[D]B1Mn\9S6>::_;J?=leNaZ=JeXT6"
  110.     A$ = A$ + "MR0[hfKDX3:dJ_ZEUFK\gNm:;4cc<C>mBQcbSfC9KFKcSNlcN`FVaWO\_hRG"
  111.     A$ = A$ + "OY8D2X?Ed[]cSNkkOmAUB=gUn\?oZck?55H^RFM>n5_oKOinFaWK\;nUC<g:"
  112.     A$ = A$ + "QPT?\6]>hY_k[OmMJ`7deDC>e<^FNEoA8DZ_oSOckNaa]d8R\f:;=o]VJjIm"
  113.     A$ = A$ + "T#[kMOiKOemg\\:\7Q>a:CMi;>f\ab]cCOc;_kjFRl:i:VSfgTOnWN9GI[Nd"
  114.     A$ = A$ + "cOnMG?W0WJaJEeC]`Enl;?ObLm>HoNUeTd:f\E5ih;>i4c5R_HcjiUIX5;]a"
  115.     A$ = A$ + "QkeDH]Y^jDNI[0UmY2i;2OcknR7_>c2SMUISKf^SEVELi9CUklfgM\EXIVJY"
  116.     A$ = A$ + "DV=Z`_f=WdEZ:gm[nVkG<QWEBXe\CnTgl;^h\4]OjgnV]hli>RfVgk`B1^d_"
  117.     A$ = A$ + "jgL;j=OegnT:aGLaHi?lAg>gZGlSobE<^m[NF7RUVe;o6?jAmb>mi_P;QmOk"
  118.     A$ = A$ + "k_SlRGM2_JkodkF`gU?o4AKlRod_K?Zi^b41;LhMkTjjOj5VSKogoWOKc4c?"
  119.     A$ = A$ + "m:2Mg_;glGhC7?LeWlkoj]IFIN_2m^cN6;OnOeiGDk^o^o`mDm>m<aZLl3jA"
  120.     A$ = A$ + ">eWn9CVISgn7OAU8c<]G]mNd]eGhloj;7\fcoQ_oHQUVHjhe>j>=RN]?i2^4"
  121.     A$ = A$ + "?hKnnWG9n2c5>oNgY;aeohOmeBdn5OgG]1ibc0VfC>bHYco[^PkL?lgmeK#\"
  122.     A$ = A$ + "`D;b^m;Oe6a_j7oYc9Nk_m?L7iUWHBoi?nIk;hjBOiLL7l]oY7K^j4S]Xh>?"
  123.     A$ = A$ + "i8EGOai_N\F_h7n^S0M>E\bk_O1oGkc_RKe3oZoYkK6HfiRGnh7]IfaoX?jB"
  124.     A$ = A$ + "n[lho`g^7kbC>7ij?iIKBNc;lIG?Ac7mVkH6Jni3Uk`Wo`Z1_fi_TKUmOa;g"
  125.     A$ = A$ + "Rcc=W;JQm^cfJQVO6?<]^cmjbiM>GQefj1g]FlY_bh[X\kcoZkG8bLg`EVBK"
  126.     A$ = A$ + "]C=6o;_B<UVK_EAb#1B8GKS=ZBkNb9LUZlQ?in]H3\d:I:\eQmdR^\;_YeKd"
  127.     A$ = A$ + "]<CXE3#F4`[fPacdC]HZ2kLdVVYlj9#F0NgVF4nG=8QN]6iY3jNeHb]_mMfd"
  128.     A$ = A$ + ":UkIV?P#Smf_]JL_[4P#_a6[GR?IT4d:i\<48R7;SDQ6=:8V>BhT0jd^M[X#"
  129.     A$ = A$ + "4=JBJI[2VBXSA#d\HI;IZdYb#;KJZbS>=2JaLEKF_PDfhARUQEFg4QE0Z:V["
  130.     A$ = A$ + "#N=^<9RUTP5Z0Z;10lU<:E_Z1EXElh?970MYT639]LQJe\HR\bb[4RdXFkjF"
  131.     A$ = A$ + "2TTlVeJFEW`[[im4S_H[^];K8;YHHI:C6M]e2VPBfZ4Ddf:OQl020jTA25:J"
  132.     A$ = A$ + "JJAX?Z6]KEcBV1#JUUR6go5ULa>Kf[V>Mb4IH4UiB7d[_8<5jMkSf^4K\0Q9"
  133.     A$ = A$ + "Db3Hf2i1RL6]fNkfi5bV>[PQU1G2ONl6QD[LS6U=UhhTdc3QQ\AFcGPhdb6i"
  134.     A$ = A$ + "bIRnU3MD<lIRhO]0#7M:ViAo\\#X?4b4JE?[PPVIF\Jc>8DajRT91LA1RTT2"
  135.     A$ = A$ + "1\ZBmV]JDY04992_bR;]J4:S6K]gf];1^[\V^2CD?^FHUH2U0?XNNE840aOQ"
  136.     A$ = A$ + "H;=md^2aGYgf[GF12e6EJe^\#HGgh6c7P:Gmf]ZVg#7eZINEZX[^h2N2VRC_"
  137.     A$ = A$ + "Dm6=ZE^Da2FVZ\9l]`<ci>4E^FK^cV];3He18K2I2\h\c]L`\j=gNoMkEcB5"
  138.     A$ = A$ + "1IAP:^_UFHieB[DOk3?HS:jl0<9`dX2kNn5F=Q4dkdF=<5IiDbG]NAQ8;<=0"
  139.     A$ = A$ + "gW]#[eQe<55cbX5?A`fVLVEB8T_FmJU;FZF[>][G#8nbC=neWcG:ibMgJ[N]"
  140.     A$ = A$ + "ZG]Je:5eb^j=^dG<nR9ekL`Akg[F1;X[_ZjILOm;?fdNcJeNkMfXEeB517AG"
  141.     A$ = A$ + "P8TkiWOYeB`XG^ej[g^N9;30c6GfXNGJA?1C;FXE_e[WGGEc\#YRjD1VOZYF"
  142.     A$ = A$ + "0<^fmjD[#>;dVU:ZYlLc9_UKLCge[D\#i6Mke]N>VPc=nHC^DL`[gOW^E<ed"
  143.     A$ = A$ + "cG]AESd;=nU^flATZ]i^Ke^JA0\JYLBYmiJ6GNSSPiG1R`UZDYD>5NHQfWWE"
  144.     A$ = A$ + "G;:YBYFMJFD7260DHC^8;>eD^2cIE[I3Xe2ESRPJ=iC7bcB;_JXD\Z5JdMSN"
  145.     A$ = A$ + "=;IX2eCe49l?oDgLADG;k_g6=:JZ3kkPHobX5:2UR=kd^IebX5E=AZ4nM_TK"
  146.     A$ = A$ + "8HHD]C_>#\8W90<U>^_5G`M`dlj#L\NU2`FC0f90??`b^FdG2FcZ]k21^li;"
  147.     A$ = A$ + "EdBU;ChEMkNe0a9U\ZgMc=keZ\1jh<YB^f2c<kbQX;\a]_cQ`F3M0\X89dKg"
  148.     A$ = A$ + "dck;U4dldfKd^Fi2idTH9R^Z[iFHeHd6=fnf7]Gg:iTQ698B6ao:[h;<doDk"
  149.     A$ = A$ + "M>Lo=ZK9ACT9;5IbP[jaK82EHgenmZJRBC`cC5gkB;1=K9E\mFK313C51<jh"
  150.     A$ = A$ + "d[]T[UmWB^fF7N`F=\TH1TnB\am<k=WeC<QZkLlMf[C9M9NNAA^<1#ka3CW["
  151.     A$ = A$ + "cN7MhVe1[PVVUQ<^g5L]T_4REgnhk^O;;A:2B>9IVX^WK^U33]YN_kM#klB<"
  152.     A$ = A$ + "#_#:b\#?];h9<YI[MACGa0<>D[^TAl\`BNSaELS3?JgFi11c0TICh5g0\NCd"
  153.     A$ = A$ + "XH:YRDWX\#f#^M]Xg4BEf4EeG50;c;[Yb6cclck>4Y>jcmF5D18m0X8AVT[i"
  154.     A$ = A$ + "f=:WCm=gMkf5]iFE4Ta2;]6b]gOg>5E]?fH5b#;kJYeBRJMM[^Ec1HgA7N9O"
  155.     A$ = A$ + "j0^GNe`IE[\n>KgYR9di50QGR918IOaH<Z_a>KRE87PhGV>2TPIehXnSQ_]E"
  156.     A$ = A$ + "iaM>Y;THUiVMiPPe1Y7JF#C2O`RLPTGLieB`V[iVk\A3;INl9>bUfoB;h>4Q"
  157.     A$ = A$ + "Bi^K_cjEd5#`jU152kkPH7hQ7\KW2bh8Z64XGIQ5m6W?Ok]gMSJVBXn?UUQd"
  158.     A$ = A$ + "O[jIe`4#VP]g^K53Xi7`I`TGI]HDJEj^e6=;Z9RKb0b81KU2TTeXFgNM#KI#"
  159.     A$ = A$ + "=YX8#5ckbN2TR?G]>MKEb#B0gQ47A4O[h=D6al=F_G[BjRhSdDV=E`EaWYDU"
  160.     A$ = A$ + ">mPPW<jLkTUh9C4dgJQQ>f;18#[0=iSa[cAU:B`##jM8;Jcj5=D#1491FB0g"
  161.     A$ = A$ + "J4X;K]2E[E]4T55gS3dBKdPQRQh^AkVXS?0eWT83DS`[oX4#ibF0I8[4Z_7F"
  162.     A$ = A$ + "bDA2h?DLXEnb=Je\FACEla?W=Ml`137WP7B^gYEUL`70XcTU>Cd0[1#C5]Ze"
  163.     A$ = A$ + "^5H9bH0j8Q4<#hT#Ia:=JF_D>=L7ELd4a37:LR\8EfYIIC5AlI`cT6c=]JYV"
  164.     A$ = A$ + "MkD_X>j4nQ6kY#EV0OG_2TAXFN=9L[1\IS72ZGUNXQ`S`l3L7J=8BaIF[cj]"
  165.     A$ = A$ + "`e7AC>0FK#1`<28W\1\8h\nB6?8HfI40c6YAdGXbZbL4AmkcO<BLN\ZFZP?2"
  166.     A$ = A$ + "MA83IQ`eHe:S]JIT<D6LR2L6UKfZ>DQD4MD[Z#eo`n34U#8G]fPESZI1UWd4"
  167.     A$ = A$ + "9S5H]0a8U;PG30\jB#3^:P63h?HL:ilePh44#`b#V==FSDZ5a[_8cCYoIHPV"
  168.     A$ = A$ + ";Eaj=PJg#C]8^1bA240]91Uj67W#bF6h>04X0k[NmbiDU#74\Q^4ERPn36S#"
  169.     A$ = A$ + "`\BmJUbY:S6=Q^Z04F34>19j49Z2;jbBhY3a3T32;Th3ZXPQ2JL>Hc>F#P<]"
  170.     A$ = A$ + "Bn:E;3k:]931ZHQ2618KE^J9;dWYhQo`V9:\nVRA=OU:XmC8ZX:19aKRIEcG"
  171.     A$ = A$ + "^LaLJ:X1hX[:B1XW2WPP3O8EVJ::hIM;BW2]gV0fKKCFFaF8YR42fbSV07#f"
  172.     A$ = A$ + "fK<d`29B2BPLiBiAU^0Z6ZfN3#F7Z^TgD3#IX[[9cR<K7bU`Ta3=RafK0b56"
  173.     A$ = A$ + "Q\8RLXXY#PX:KC6M#52dYS5>Ldd<BV8^cQ6mT6Vkh0iDDRPUD^X5Jh#X_cH9"
  174.     A$ = A$ + "B49LD\\;G1TR^=g8bHkfN2S6JfZT5mM3n3K4<C<g`V4AFWnDiPFlLl7T\i8C"
  175.     A$ = A$ + "69LhhIHaTk;49^\X0KL<gHQ0^n0ba`Lk8KdD0#29TS#0T]i<<#3f2n`1]<47"
  176.     A$ = A$ + "P#S>^]RFV>TQD<Q1Q`8I0Q`2fTEUi]3:99i1dg8#1j8U:;015lZ62NJ1K^A="
  177.     A$ = A$ + "ZcPlN#WX3E3MlUfBA2kVQS1hYeb10\]0#^8bLTYR5>B\d8P[8YP`o2TiYB7?"
  178.     A$ = A$ + "Jdh8b8QW7Ag0\#j<90bYXh?3iH0Idi[Q5NOb49R69JB26A]n\SV\2=1:3K<K"
  179.     A$ = A$ + "YD\HATAR>QQ\Pk7:Jb\XIf281kCV<Ml8X58T3E\42[K>3``YbP4N2T`cQ9W7"
  180.     A$ = A$ + "4^4XcTaR41Ki#<70^1^Z^2:SLl8P>9iE<QM5Ua\[>Dh\XH^3I7MJ;L#WPD6`"
  181.     A$ = A$ + "=FWI#FN1\`cQ?J4\>SU:[m9MF^HN\5BCT:^So3OC0h7;HQ8c#V9I\X8YP>^E"
  182.     A$ = A$ + "0M<Jo_\0;DM075cRfK1UN0PGjXSnTR0MHZPDT<0b8lHPoP;VT8F\hXC<=OiJ"
  183.     A$ = A$ + "eZD4I:L\dTXeRHP79W[4Z06BWl0IT=RFAQ_QZF_6Zm49dlLQBB8G:;TYX2J>"
  184.     A$ = A$ + "mi`3<fVm8`^85XhCSJU\#V2;3=VmHY#WiJm6e;3I5`WUWcRAF<NDPMD]:N`H"
  185.     A$ = A$ + "bRRj>BY9`>HRl3H7n`5JM\9T4fdA:X=6X?GD1Hi=ZQ]5KY43]a0BF0Xi[F2C"
  186.     A$ = A$ + "6\5`C7DY^NM\7Ic>lmBX8>\S\5d1PbQU>FYj5a\3V^]=RfZW0maHI\dea\cJ"
  187.     A$ = A$ + "7fQhUNQ:XX>TF4OlYXhK<XYCIXm?Xc6TdaTa\3[6Z#JkZ33<IcT<Nl4#od6#"
  188.     A$ = A$ + ">Zbh59NLl=ERh9P_H#I6#k0DAWI;VNBd5M0J;]<TW7=U2\H#lW8EFF#[U]cA"
  189.     A$ = A$ + "S538nDhk>1jTg[0[eh[JP=mDY9IQmSUa69Ni`Fl8fE]IgIi0IUYBV:;B[hDS"
  190.     A$ = A$ + "ff>=Xed9CJ;6h;<lF3di3BSIWP6MaX6b>3J59MZ<TX_OQlGf^:WM#8]mh1`Y"
  191.     A$ = A$ + "BA1BdPXI0aWVP6UF3Hgd1N37Vm<f\Sj74O;3h`QHPMd9_3I76A:D0IdUd0UG"
  192.     A$ = A$ + "3jHJ4fcT9;=?:UTUmR:_SaTQ0Mg8#7P?T>bV>bW#\;XHHQ>FCEkS67K<I`g?"
  193.     A$ = A$ + "RLiQVoejOCbL^Z2RZVf\[hLY1PNR8#:E;W5:Od9HWT1TN>ll16Q\mH3#7l0F"
  194.     A$ = A$ + "Yh11P]Ad5Q0M[<6856\D`BgNV<fgk08>R\AJ>9ESL>\KOBoTP]h85Tdk;6T="
  195.     A$ = A$ + "R^n48YP53VNOfAG>5TD>1kIA\KP;IH?d0\WRRSSJS>BY_aH>T<Go`^8VM\Dj"
  196.     A$ = A$ + "[DLP1L9C7URYK[D^Q:efW#cNQT6JWAgV<Nh4XQMP>LJ6kkMS3M4k3U>^8h8B"
  197.     A$ = A$ + "93FjhcfFcQ>BY8K4e:W^3IiQ635Ze:mQ\?#jlf3gaFYf63ff5aGS5ME7[Q7O"
  198.     A$ = A$ + "2jDfgY6Snl317TaS_l#XA3R8[<dWX0YS#F>PM\aCTU4C73gANHTl4dE6\\J7"
  199.     A$ = A$ + "b68Nd;8LBa[Y>Rhk?c`ndE`c[Q1MiQ`3DB73LbnnSP6cT3ME]QT5\WO8869<"
  200.     A$ = A$ + "^c1FS3M>7j:SHm8[QC4OoS\Sl>kQQHS=MEWUK7jhkQdXR__Ja\ggJTkkEhkh"
  201.     A$ = A$ + "ST]EZ<HY2=VaA\Bl=AbfL`3k3?U\nT79hdOjI>gEXQFYj8gW9Knk;NKWZo>4"
  202.     A$ = A$ + "Z1FY3M97jDXK<VLoHSLo]g3]NenTQe#deHc98hH=H_eAlH[di6=9i\Tk#7_8"
  203.     A$ = A$ + "i<Y9kPf`;0EoK_3\oVJ#X__d?imUBen2C=REfG[8jbHiQYSMZn\gO2Pfg6[n"
  204.     A$ = A$ + "c#\oM6J#`ai2m>:MD\DdFK37T8JL_XIH?:kfdY]795V^Tc5Q2CWQ[?mnF9V^"
  205.     A$ = A$ + "chGMXKgO??GokAEOaH?8DDiA4MW1c9eo6?Q5_=MWIAJKm3X?H6UmYS^dJ3hO"
  206.     A$ = A$ + "#`L`lAabOhUHVcI0\>;ZfF_\mmEK4jmigNlc3_bc>SEeNHc\S<bdnS7]ohon"
  207.     A$ = A$ + "0da:##LDcdNlf\lS#_ON4fCa^b9\OWaL:;gOLc>fo9XK?ELkk\]cCTMT_ClY"
  208.     A$ = A$ + "omlFJ`W:hh9k9Y;i`_3M7c\_mHkH>cLFI`]aSUm\dafIOiHCFeI<jl37LZmH"
  209.     A$ = A$ + "dEM>f8k5NWa[K?<MW;H8WSmKoIhPT9>gBm1?h4d7HomcI8HOSnhLV_lYYk<g"
  210.     A$ = A$ + "Lini<\ELo;?Pl9Ygofago1>9m6\TeoCGk;n87[S1=be04SIb9b=Ri?d1j_7e"
  211.     A$ = A$ + "A<BBWTn0mn1Y?`c>QL>9MF7mNB1i\WFjd6DomjW`3`X4?SP47AaWA1h#g8d?"
  212.     A$ = A$ + "m;8MjUPA4TdYhOd7<`B?a;<d3h7nidXd7m_Z3S#GL`=\HXY>l1LWV^STh6\i"
  213.     A$ = A$ + "J4;EH`M6iTFj`]SRWHgk7dD57n2Wf56i_77WbW?]YN29=Z;LJ9=jgISn2W98"
  214.     A$ = A$ + "=b;8n1XO2KEDLdPaIl]oMe`Il=79m1YcOJ5LB1mSXiQGWTQoMZY_6>U;`oQL"
  215.     A$ = A$ + "Q?XV7ABoHV:oI<engXVcZiANQ?\YNb[43F>OPGHT7l1d#oK2cIl1nC8XCm2W"
  216.     A$ = A$ + "T^S6n`V:o7dD7AcSN5NnaY?j3T>[T>]Y:nO0mCKAWBcRo8J^oO7\>;m1o>ee"
  217.     A$ = A$ + ";JD1eo2BM21MZGh4^V`O92j7CcO0j2WVn?j;LR[D^d8?h4dU>aKLjk]e09mQ"
  218.     A$ = A$ + "\dODGh3nPoSNQcXJQ1Jn3l2oXVjI^jISBo3:Xc8Y1G`\o_dTngEeo5JBooV?"
  219.     A$ = A$ + "l1N`O1do;k5na=Yo?HD2%%h1"
  220.  
  221.     btemp$ = ""
  222.     FOR i& = 1 TO LEN(A$) STEP 4: B$ = MID$(A$, i&, 4)
  223.         IF INSTR(1, B$, "%") THEN
  224.             FOR C% = 1 TO LEN(B$): F$ = MID$(B$, C%, 1)
  225.                 IF F$ <> "%" THEN C$ = C$ + F$
  226.             NEXT: B$ = C$: END IF: FOR j = 1 TO LEN(B$)
  227.             IF MID$(B$, j, 1) = "#" THEN
  228.         MID$(B$, j) = "@": END IF: NEXT
  229.         FOR t% = LEN(B$) TO 1 STEP -1
  230.             B& = B& * 64 + ASC(MID$(B$, t%)) - 48
  231.             NEXT: X$ = "": FOR t% = 1 TO LEN(B$) - 1
  232.             X$ = X$ + CHR$(B& AND 255): B& = B& \ 256
  233.     NEXT: btemp$ = btemp$ + X$: NEXT
  234.     Basfile$ = _INFLATE$(btemp$): btemp$ = ""
  235.  
  236.  
« Last Edit: August 04, 2020, 11:30:39 pm by Dav »

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Useful code Dav, especially when using it in conjunction with Petr's Mod2Wav converting routine using XMP.
Thanks for sharing...
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Yes! Hi RhoSigma, Hi Dav,

Here is next upgraded source code, now with direct windows playing :)

32bit IDE is required
Code: QB64: [Select]
  1. PRINT "Direct playing MOD/XM/S3M....and much more music formats using Windows librarieswithout QB64 music statements. Thanks to RhoSigma and Dav!"
  2. XMPPLAY "awesome.s3m"
  3.  
  4.  
  5. SUB XMPPLAY (file AS STRING)
  6.     DECLARE DYNAMIC LIBRARY "libxmp"
  7.         FUNCTION xmp_create_context%& ()
  8.         SUB xmp_free_context (BYVAL xmp_context%&)
  9.         FUNCTION xmp_load_module& (BYVAL xmp_context%&, path$)
  10.         SUB xmp_release_module (BYVAL xmp_context%&)
  11.         FUNCTION xmp_start_player& (BYVAL xmp_context%&, BYVAL rate&, BYVAL format&)
  12.         FUNCTION xmp_play_buffer& (BYVAL xmp_context%&, BYVAL buffer AS _OFFSET, BYVAL size AS _OFFSET, BYVAL loops&)
  13.         SUB xmp_end_player (BYVAL xmp_context%&)
  14.         SUB xmp_get_frame_info (BYVAL xmp_context%&, BYVAL info%&)
  15.     END DECLARE
  16.  
  17.  
  18.     DECLARE DYNAMIC LIBRARY "winmm"
  19.         FUNCTION PlaySoundA& (lpszName AS STRING, BYVAL hModule AS LONG, BYVAL dwFlags AS LONG)
  20.     END DECLARE
  21.  
  22.     CONST SND_APPLICATION = 128 '  look for application specific association
  23.     CONST SND_ALIAS = 65536 '  name is a WIN.INI [sounds] entry
  24.     CONST SND_ALIAS_ID = 1114112 '  name is a WIN.INI [sounds] entry identifier
  25.     CONST SND_ASYNC = 1 '  play asynchronously
  26.     CONST SND_FILENAME = 131072 '  name is a file name
  27.     CONST SND_LOOP = 8 '  loop the sound until next PlaySound
  28.     CONST SND_MEMORY = 4 '  lpszSoundName points to a memory file
  29.     CONST SND_NODEFAULT = 2 '  silence not default, if sound not found
  30.     CONST SND_NOSTOP = 16 '  don't stop any currently playing sound
  31.     CONST SND_NOWAIT = 8192 '  don't wait if the driver is busy
  32.     CONST SND_PURGE = 64 '  purge non-static events for task
  33.     CONST SND_RESOURCE = 262148 '  name is a resource name or atom
  34.     CONST SND_SYNC = 0 '  play synchronously (default)
  35.  
  36.  
  37.  
  38.     '--- the (rudimentary) frame info type ---
  39.     TYPE xmp_frame_info '      /* Current frame information */
  40.         posi AS LONG '         /* Current position */
  41.         pattern AS LONG '      /* Current pattern */
  42.         row AS LONG '          /* Current row in pattern */
  43.         num_rows AS LONG '     /* Number of rows in current pattern */
  44.         frame AS LONG '        /* Current frame */
  45.         speed AS LONG '        /* Current replay speed */
  46.         bpm AS LONG '          /* Current bpm */
  47.         time AS LONG '         /* Current module time in ms */
  48.         total_time AS LONG '   /* Estimated replay time in ms*/
  49.         frame_time AS LONG '   /* Frame replay time in us */
  50.         'we don't use the frame buffer, as we play to an
  51.         'external buffer using xmp_play_buffer&()
  52.         buffer AS _OFFSET '    /* Pointer to sound buffer */
  53.         buffer_size AS LONG '  /* Used buffer size */
  54.         total_size AS LONG '   /* Total buffer size */
  55.         '----------
  56.         volume AS LONG '       /* Current master volume */
  57.         loop_count AS LONG '   /* Loop counter */
  58.         virt_channels AS LONG '/* Number of virtual channels */
  59.         virt_used AS LONG '    /* Used virtual channels */
  60.         sequence AS LONG '     /* Current sequence */
  61.         'the following is not yet detailed, its just a safety
  62.         'margin for now to avoid memory protection faults
  63.         xmp_channel_info AS STRING * 8192 '/* Current channel information */
  64.     END TYPE
  65.     ctx%& = xmp_create_context%&
  66.     xerr& = xmp_load_module&(ctx%&, file$ + CHR$(0))
  67.     xerr2& = xmp_start_player&(ctx%&, _SNDRATE, 0)
  68.     DIM xfi AS xmp_frame_info
  69.     DIM m AS _MEM: m = _MEM(xfi)
  70.     xmp_get_frame_info ctx%&, m.OFFSET
  71.     _MEMFREE m
  72.  
  73.     Tim& = xfi.total_time / 1000
  74.     Time& = _SNDRATE * Tim& * 4
  75.  
  76.     DIM Buf AS _MEM
  77.     Buf = _MEMNEW(Time&)
  78.     buf& = xmp_play_buffer&(ctx%&, Buf.OFFSET, Buf.SIZE, 0)
  79.     raw$ = SPACE$(Time&)
  80.     _MEMGET Buf, Buf.OFFSET, raw$
  81.     Head$ = Head16S$(Time&)
  82.     x& = PlaySoundA(Head$ + raw$, 0, SND_MEMORY OR SND_ASYNC OR SND_LOOP)
  83.     xmp_end_player ctx%&
  84.     xmp_release_module ctx%&
  85.     xmp_free_context ctx%&
  86.     _MEMFREE Buf
  87.  
  88. FUNCTION Head16S$ (lenght AS LONG) 'add wave header to wave raw audio data
  89.     Head16S$ = "RIFF" + MKL$(lenght + 44) + "WAVEfmt " + MKL$(16) + MKI$(1) + MKI$(2) + MKL$(_SNDRATE) + MKL$(_SNDRATE * 2 * 16 / 8) + MKI$(4) + MKI$(16) + "data" + MKL$(lenght)
  90.  

* awesome.s3m (Filesize: 28.01 KB, Downloads: 153)
* libxmp.dll (Filesize: 706 KB, Downloads: 167)
« Last Edit: August 05, 2020, 10:37:50 am by Petr »

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Thanks guys.  Looks nice, Petr.  When I get home tonight I will try your program.

- Dav

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Awesome program Dav!

Petr, for some reason it isn't detecting the DLL. I put all the files in one folder but it won't see it for some reason.

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Awesome program Dav!

Petr, for some reason it isn't detecting the DLL. I put all the files in one folder but it won't see it for some reason.

According to the source the DLL should be in the same folder as the compiled EXE, hence it depends if you've set the IDE to compile into the source folder or into the qb64 folder. And important is to use the 32bit version of QB64, cause the DLL is 32 bits only. The 64 bit version of QB64 will NOT recognize the DLL.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Ah that's why then, I use the 64 bit QB64. Oh well, thanks. I looked right past his 32 Bit Required notice. lol