今回はオウディオの可逆圧縮をやってみよう。
いろんな形式があって迷うんだけど、ABでやるならapeが一番簡単かな。

Monkey's Audio からSDKをダウソ。MACDll.dllをシステムフォルダに投げる。

つぎにABでイカのコードをコピペ。
test.wavをHirai-Kimihiko.apeにエンコする。

#console
#include <api_mmsys.sbp>
Typedef APE_COMPRESS_HANDLE = VoidPtr

Declare Function c_APECompress_Create Lib"MACDll.dll" (pErrorCode As *Long) As APE_COMPRESS_HANDLE

Declare Sub      c_APECompress_Destroy Lib"MACDll.dll" (hAPECompress As APE_COMPRESS_HANDLE)
Declare Function c_APECompress_Start Lib"MACDll.dll" (
  hAPECompress As APE_COMPRESS_HANDLE,
  pOutputFilename As BytePtr, pwfeInput As *WAVEFORMATEX,
  nMaxAudioBytes As Long, nCompressionLevel As Long,
  pHeaderData As *Byte, nHeaderBytes As Long) As Long

Declare Function c_APECompress_AddData Lib"MACDll.dll" (hAPECompress As APE_COMPRESS_HANDLE, pData As *Byte , nBytes As Long) As Long
Declare Function c_APECompress_Finish Lib"MACDll.dll" (hAPECompress As APE_COMPRESS_HANDLE, pTerminatingData As *Byte , nTerminatingBytes As Long, nWAVTerminatingBytes As Long) As Long

Const COMPRESSION_LEVEL_HIGH = 3000
Const CREATE_WAV_HEADER_ON_DECOMPRESSION = -1

Const infile = "test.wav"
Const WAVE_HEADSIZE = 44

'main
main()
Sub main()
  Dim wfe As WAVEFORMATEX
  Dim r As Long
  Dim ha As APE_COMPRESS_HANDLE

  Dim hF As HANDLE
  Dim siz As DWord
  Dim read As DWord, rt As DWord
  Dim buf As *Byte



  hF = CreateFile(infile, GENERIC_READ, 0, ByVal 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
  siz = GetFileSize(hF, NULL)-WAVE_HEADSIZE
  SetFilePointer(hF, WAVE_HEADSIZE,0,FILE_BEGIN )'ヘッダをとばす
  wfe.cbSize = siz
  wfe.nSamplesPerSec = 44100
  wfe.wBitsPerSample = 16
  wfe.nChannels = 2
  wfe.wFormatTag = 1

  wfe.nBlockAlign = (wfe.wBitsPerSample / 8) * wfe.nChannels
  wfe.nAvgBytesPerSec = wfe.nBlockAlign * wfe.nSamplesPerSec

  buf = malloc(wfe.nAvgBytesPerSec)

  ha = c_APECompress_Create(VarPtr(r))
  c_APECompress_Start(ha, "Hirai-Kimihiko.ape", VarPtr(wfe), siz, 
    COMPRESSION_LEVEL_HIGH, NULL, CREATE_WAV_HEADER_ON_DECOMPRESSION)

  read = 1
  While read
    ReadFile( hF, buf, wfe.nAvgBytesPerSec,VarPtr(read), ByVal 0)
    r = c_APECompress_AddData(ha, buf, read)
    rt = rt + read
    Print rt;" / ";siz
  Wend
  c_APECompress_Finish(ha, NULL, 0, 0)
  
  c_APECompress_Destroy(ha)
  CloseHandle(hF)
  
  Input "ok";buf
  free(buf)
End Sub