1. --!movie
  2. --!encoding=utf-8
  3.  
  4. -- ************************************************************************
  5. -- Software: SOUND MEMBER EXPORTER
  6. -- Version:  0.2 - D11.5+ version
  7. -- Date:     2015-08-12
  8. -- Author:   Valentin Schmidt
  9. --
  10. -- Requirements/Dependencies:
  11. -- * Xtra "FileIO"
  12. -- * Xtra "Crypto" (http://valentin.dasdeck.com/xtras/crypto_xtra/win/d11.5/)
  13. -- * Xtra "MP3Xtra" (http://valentin.dasdeck.com/xtras/mp3_xtra/win/)
  14. --
  15. -- Usage:
  16. -- ok = exportSound (member("wav"), _movie.path, "mysound")
  17. -- ok = exportSound (member("mp3"), _movie.path)
  18. -- ok = exportSound (member("swa"), _movie.path)
  19. --
  20. -- ************************************************************************
  21.  
  22. ----------------------------------------
  23. -- Exports internal sound member as file
  24. -- Compressed members are exported as MP3 or SWA, uncompressed members as WAV files
  25. --
  26. -- @param {string} tMemRef
  27. -- @param {string} tFolder
  28. -- @param {string} [tBaseName] - optional, if omitted, memberName is used
  29. -- @return {bool} success
  30. ----------------------------------------
  31. on exportSound (tMemRef, tFolder, tBaseName)
  32.   if tMemRef.type<>#sound then return false
  33.   if voidP(tBaseName) then tBaseName = tMemRef.name
  34.   pd = the last char of _movie.path
  35.   if the last char of tFolder<>pd then put pd after tFolder
  36.  
  37.   ok = false
  38.  
  39.   data = cx_member_to_bytes(tMemRef, TRUE)
  40.  
  41.   useCompressed = false
  42.   if data.length>=25 then useCompressed = (data[25]>0)
  43.  
  44.   if useCompressed then
  45.  
  46.     data.position = 29
  47.     dataType = data.readRawString(data[25])
  48.     data.position = data.position+8
  49.  
  50.     case (dataType) of
  51.  
  52.       "kMoaCfFormat_MPEG3": -- EXPORT AS MP3
  53.         len = data.readInt32()
  54.         ok = file_put_bytes(tFolder&tBaseName&".mp3", data.readByteArray(len))
  55.  
  56.       "kMoaCfFormat_SWA": -- EXPORT AS SWA
  57.         len = data.readInt32()
  58.         ok = file_put_bytes(tFolder&tBaseName&".swa", data.readByteArray(len))
  59.  
  60.     end case
  61.  
  62.   else -- EXPORT AS WAV
  63.  
  64.     mx = xtra("MP3Xtra").new()
  65.     ok = mx.mem2wav(tMemRef, tFolder&tBaseName&".wav")
  66.     mx = 0
  67.  
  68.   end if
  69.  
  70.   return ok
  71. end
  72.  
  73. ----------------------------------------
  74. -- Saves byteArray as file
  75. -- @param {string} tFile
  76. -- @param {byteArray} tByteArray
  77. -- @return {bool} success
  78. ----------------------------------------
  79. on file_put_bytes (tFile, tByteArray)
  80.   fp = xtra("fileIO").new()  
  81.   fp.openFile(tFile, 1)
  82.   err = fp.status()
  83.   if (err=0) then fp.delete()
  84.   else if (err <> -37) then return false
  85.   fp.createFile(tFile)
  86.   err = fp.status()
  87.   if (err<>0) then return false
  88.   fp.openFile(tFile, 2)
  89.   err = fp.status()
  90.   if (err<>0) then return false
  91.   ok = fp.writeByteArray(tByteArray)
  92.   fp.closeFile()
  93.   fp = 0
  94.   return (err=0)
  95. end
  96.  
[raw code]