#include-once #include #include #include #include #include global $waveform_display_version = "1.3.3" global const $WAVE_COLUMNS = 1200 global const $WAVE_PROBES = 16 global const $WAVE_BG = 0xFFF0F0F0 global const $WAVE_PEAK = 0xFF7A7A7A global const $WAVE_CENTER = 0xFFBDBDBD global const $WAVE_CAPTION_COLOR = 0xFF808080 global const $WAVE_FONT = "Segoe UI" global const $WAVE_MENU_WAVE = "Show waveform" global const $WAVE_MENU_TEXT = "Show DROP-ZONE text" global const $WAVE_TIP = "Waveform view. Right-click to switch back to DROP-ZONE text. Drops work in both views." global const $WAVE_INI_KEY = "wave_view" global const $WAVE_TEXT = "DROP-ZONE" global const $WAVE_FONTNAME = "Arial Black" global const $WAVE_FONTSIZE = 50 global const $WAVE_FONTWEIGHT = 600 global const $WAVE_FONTATTR = 1 global const $WAVE_TEXTCOLOR = 11579568 global const $WAVE_SWP_REDRAW = 39 global const $WAVE_RDRAW = 1285 global const $WAVE_WM_SETTEXT = 12 global const $WAVE_F32_LEVELS = 8388608 global const $WAVE_F32_BIAS = 127 global const $WAVE_F32_SUB = 1.17549435082229e-38 global const $WAVE_F32_MAX = 255 global const $WAVE_F64_HI_SCALE = 1048576 global const $WAVE_F64_LO_SCALE = 4294967296 global const $WAVE_F64_MANT_SCALE = 4503599627370496 global const $WAVE_F64_BIAS = 1023 global const $WAVE_F64_MAX = 2047 global const $WAVE_SIGN_BIT32 = 2147483648 global const $WAVE_FORMAT_PCM = 1 global const $WAVE_FORMAT_FLOAT = 3 global const $WAVE_FORMAT_EXTENSIBLE = 65534 global const $WAVE_FORMAT_ADPCM_MS = 2 global const $WAVE_ADPCM_FULL = 49152 global const $WAVE_ADPCM_BLOCKS = 4000 global const $WAVE_NEG32 = 4294967296 global const $WAVE_TOP_PAD = 6 global const $WAVE_BOTTOM_PAD = 16 global $wave_label = 0 global $wave_gui = 0 global $wave_ini_path = "" global $wave_ini_section = "" global $wave_mode = false global $wave_pending = false global $wave_gdiplus = false global $wave_bitmap = 0 global $wave_signature = "" global $wave_file = "" global $wave_key_base = "" global $wave_caption = "" global $wave_columns_done = 0 global $wave_peaks = 0 global $wave_octets = DllStructCreate("byte[64]") global $wave_menu_to_wave_label = 0 global $wave_menu_to_text_label = 0 ; Prepares wave view on the DROP-ZONE label itself plus its right-click ; menu. Reads the saved view from the ini so the last used view returns on ; launch. Call once from MakeGUI after $lab_dropzone exists. Returns the ; label control id. Leaves GDIPlus running until Wave_Shutdown, stores the ; gui, label, ini path and section in $wave_ globals for the other Wave_ ; functions. Needs OnEventMode on, the menu calls Wave_ShowWave and ; Wave_ShowText directly. func Wave_Init($gui, $drop_label, $ini_path, $ini_section) $wave_gui = $gui $wave_label = $drop_label $wave_ini_path = $ini_path $wave_ini_section = $ini_section _GDIPlus_Startup() $wave_gdiplus = true local $menu = GUICtrlCreateContextMenu($drop_label) $wave_menu_to_wave_label = GUICtrlCreateMenuItem($WAVE_MENU_WAVE, $menu) GUICtrlSetOnEvent(-1, "Wave_ShowWave") $wave_menu_to_text_label = GUICtrlCreateMenuItem($WAVE_MENU_TEXT, $menu) GUICtrlSetOnEvent(-1, "Wave_ShowText") $wave_mode = Wave_IsTrue(IniRead($ini_path, $ini_section, $WAVE_INI_KEY, "false")) Wave_ApplyView() return $wave_label endfunc ; Loads peaks for $file and paints them when wave view is active. Reads ; $wave_label for the guard only. Returns true when peaks are ready, false ; when the file is missing, not a wav, or unparsable, clearing the stored ; peaks first so a stale wave never shows. Same file twice only redraws. func Wave_NotifyFile($file) if $wave_label = 0 then return false if $file = "" or not FileExists($file) or Wave_Extension($file) <> "wav" then Wave_ClearPeaks() Wave_Redraw() return false endif if $file = $wave_file and $wave_columns_done > 0 then Wave_Redraw() return true endif local $ok = Wave_LoadPeaks($file) $wave_signature = "" Wave_Redraw() return $ok endfunc ; Repaints after a window resize from stored peaks without rereading the ; file. Reads the label size and $wave_signature. Returns nothing. Cheap ; when the size is unchanged, the signature check exits at once. func Wave_NotifyResize() if $wave_label = 0 then return if not $wave_mode then return Wave_Redraw() endfunc ; Flips between waveform and DROP-ZONE text from either menu. Reads and ; writes $wave_mode plus the ini key. Returns nothing. Paints at once so ; the new view appears under the still open menu. func Wave_ToggleView() if $wave_mode then Wave_ShowText() else Wave_ShowWave() endif endfunc ; Requests waveform view from the menu. Records the request and returns ; at once so the menu finishes dismissing first. The deferred worker ; below does the real switch outside the modal menu loop. Called by the ; menu, never call with parentheses in OnEvent mode. func Wave_ShowWave() if $wave_mode then return $wave_pending = true AdlibRegister("Wave_ApplyPending", 100) endfunc ; Sets the label text past AutoIt. Takes the string. Sends WM_SETTEXT ; straight to the handle because GUICtrlSetData misroutes text once the ; style bits were flipped by hand, proven headless. The system repaints ; on every set. Returns nothing. func Wave_SetLabelText($text) if $wave_label = 0 then return local $h = GUICtrlGetHandle($wave_label) if $h = 0 then return DllCall("user32.dll", "ptr", "SendMessageW", "hwnd", $h, "uint", $WAVE_WM_SETTEXT, "ptr", 0, "wstr", $text) endfunc ; Frees the stored bitmap handle after leaving wave view. The style ; flip in Wave_ApplyView already hides the image, so no swap is sent: ; sending a null image auto-shrinks the static to zero size, which is ; what blanked and killed the text view. Reads the stored handle. ; Returns nothing. Call after Wave_ApplyView, never before it. func Wave_ClearImage() if $wave_label = 0 then return if $wave_bitmap then Wave_DeleteObject($wave_bitmap) $wave_bitmap = 0 $wave_signature = "" endfunc ; Requests DROP-ZONE text view from the menu. Records the request and ; returns at once so the menu finishes dismissing first. The deferred ; worker below does the real switch outside the modal menu loop. func Wave_ShowText() if not $wave_mode then return $wave_pending = false AdlibRegister("Wave_ApplyPending", 100) endfunc ; Performs a requested view switch outside the menu modal loop. Paints ; issued while the menu is up get eaten on dismissal, which is why this ; waits. Reads $wave_pending, writes $wave_mode and the ini key. Skips ; quietly if the window is already gone. Returns nothing, fires once ; per request. func Wave_ApplyPending() AdlibUnRegister("Wave_ApplyPending") if $wave_gui = 0 or not WinExists($wave_gui) then return $wave_mode = $wave_pending if $wave_mode then IniWrite($wave_ini_path, $wave_ini_section, $WAVE_INI_KEY, "true") else IniWrite($wave_ini_path, $wave_ini_section, $WAVE_INI_KEY, "false") endif if $wave_mode then Wave_ApplyView() Wave_Redraw() else Wave_ApplyView() Wave_ClearImage() endif endfunc ; Flips the label between bitmap and text form. Takes true for wave ; view, false for DROP-ZONE text. Only the low four style bits change, ; notify and parent drag stay untouched. Framechanged forces an instant ; repaint. Returns nothing. Text is cleared going in, restored by the ; caller through Wave_ApplyView. func Wave_LabelImage($image_mode) if $wave_label = 0 then return local $h = GUICtrlGetHandle($wave_label) if $h = 0 then return local $style = DllCall("user32.dll", "long", "GetWindowLongW", "hwnd", $h, "int", -16) local $bits = $SS_CENTER if $image_mode then $bits = $SS_BITMAP DllCall("user32.dll", "long", "SetWindowLongW", "hwnd", $h, "int", -16, "long", BitOR(BitAND($style[0], -16), $bits)) DllCall("user32.dll", "bool", "SetWindowPos", "hwnd", $h, "hwnd", 0, "int", 0, "int", 0, "int", 0, "int", 0, "uint", $WAVE_SWP_REDRAW) DllCall("user32.dll", "bool", "RedrawWindow", "hwnd", $h, "ptr", 0, "ptr", 0, "uint", $WAVE_RDRAW) endfunc ; Switches the label between bitmap and text form, then ticks the ; matching menu item. Reads $wave_mode, $wave_label and the two menu ; item ids. Returns nothing. Text, font and colour are reasserted on ; every switch back so the DROP-ZONE look returns exactly. Never ; draws, drawing stays in Wave_Redraw. func Wave_ApplyView() if $wave_label = 0 then return if $wave_mode then Wave_SetLabelText("") Wave_LabelImage(true) GUICtrlSetState($wave_menu_to_wave_label, $GUI_CHECKED) GUICtrlSetState($wave_menu_to_text_label, $GUI_UNCHECKED) else Wave_LabelImage(false) Wave_SetLabelText($WAVE_TEXT) GUICtrlSetState($wave_label, $GUI_HIDE) GUICtrlSetState($wave_label, $GUI_SHOW) GUICtrlSetState($wave_menu_to_wave_label, $GUI_UNCHECKED) GUICtrlSetState($wave_menu_to_text_label, $GUI_CHECKED) endif endfunc ; Mirrors the busy state onto the label so drops stay dead while the ; main controls are disabled. Call with true from SetDropOn, false from ; SetDropOff. Returns nothing. Ignored before Wave_Init runs. func Wave_SetDrop($accept) if $wave_label = 0 then return if $accept then GUICtrlSetState($wave_label, $GUI_ENABLE) GUICtrlSetState($wave_label, $GUI_DROPACCEPTED) else GUICtrlSetState($wave_label, $GUI_NODROPACCEPTED) GUICtrlSetState($wave_label, $GUI_DISABLE) endif endfunc ; Mirrors the tooltip preference onto the label. Call from SetShowTips ; with ($show_tips = $ON). Shows the wave tip in wave view, leaves the ; main tip alone in text view, clears when tips are off. Returns nothing. ; Ignored before Wave_Init. func Wave_ApplyTips($show) if $wave_label = 0 then return if not $show then GUICtrlSetTip($wave_label, "") elseif $wave_mode then GUICtrlSetTip($wave_label, $WAVE_TIP) endif endfunc ; Frees the stored bitmap and stops GDIPlus. Reads $wave_bitmap and ; $wave_gdiplus, clears both. Returns nothing. Call once from DoQuit ; before GUIDelete, drawing after this point does nothing. func Wave_Shutdown() if $wave_bitmap then Wave_DeleteObject($wave_bitmap) $wave_bitmap = 0 endif if $wave_gdiplus then _GDIPlus_Shutdown() $wave_gdiplus = false endif endfunc ; Paints stored peaks onto the label sized to its current client area. ; Reads $wave_peaks, $wave_columns_done, $wave_caption and the signature. ; Returns nothing. Skips all work when hidden, tiny, unchanged, or when ; GDIPlus is down. Swaps the new bitmap in with STM_SETIMAGE and deletes ; the previous handle, the @EQ response graph pattern. func Wave_Redraw() if $wave_label = 0 then return if not $wave_mode then return if not $wave_gdiplus then return local $handle = GUICtrlGetHandle($wave_label) if $handle = 0 then return local $size = WinGetClientSize($handle) if @error or not IsArray($size) then return local $w = $size[0] local $h = $size[1] if $w < 40 or $h < 20 then return endif local $key = $wave_key_base & "|" & $w & "x" & $h if $key = $wave_signature then return endif local $image = _GDIPlus_BitmapCreateFromScan0($w, $h) if not $image then return endif local $graphics = _GDIPlus_ImageGetGraphicsContext($image) _GDIPlus_GraphicsClear($graphics, $WAVE_BG) local $peak_pen = _GDIPlus_PenCreate($WAVE_PEAK, 1) local $center_pen = _GDIPlus_PenCreate($WAVE_CENTER, 1) local $plot = $h - $WAVE_TOP_PAD - $WAVE_BOTTOM_PAD if $plot < 8 then $plot = 8 local $mid = $WAVE_TOP_PAD + $plot / 2 _GDIPlus_GraphicsDrawLine($graphics, 0, $mid, $w, $mid, $center_pen) if IsArray($wave_peaks) and $wave_columns_done > 0 then local $half = $plot / 2 for $x = 0 to $w - 1 local $col = Int($x / $w * $wave_columns_done) if $col >= $wave_columns_done then $col = $wave_columns_done - 1 local $y1 = $mid - $wave_peaks[$col][1] * $half local $y2 = $mid - $wave_peaks[$col][0] * $half if $y2 - $y1 < 1 then $y2 = $y1 + 1 _GDIPlus_GraphicsDrawLine($graphics, $x, $y1, $x, $y2, $peak_pen) next _GDIPlus_GraphicsDrawString($graphics, $wave_caption, 6, $h - 14, $WAVE_FONT, 8, Default, $WAVE_CAPTION_COLOR) else _GDIPlus_GraphicsDrawString($graphics, "No waveform", 6, $mid - 8, $WAVE_FONT, 9, Default, $WAVE_CAPTION_COLOR) endif local $fresh = _GDIPlus_BitmapCreateHBITMAPFromBitmap($image) if $fresh then local $old = GUICtrlSendMsg($wave_label, $STM_SETIMAGE, $IMAGE_BITMAP, $fresh) if $old and $old <> $wave_bitmap then Wave_DeleteObject($old) if $wave_bitmap then Wave_DeleteObject($wave_bitmap) $wave_bitmap = $fresh $wave_signature = $key endif _GDIPlus_PenDispose($center_pen) _GDIPlus_PenDispose($peak_pen) _GDIPlus_GraphicsDispose($graphics) _GDIPlus_ImageDispose($image) endfunc ; Parses one wav file into min and max peak pairs. Reads the RIFF header ; and fmt chunk for format, channels, block size and bits, then probes ; $WAVE_PROBES frames per column across the data chunk. Writes $wave_peaks ; as [column][min, max], plus file, caption and key base. Returns true on ; success, false on any short, strange or unsupported file, clearing first. func Wave_LoadPeaks($file) Wave_ClearPeaks() if not FileExists($file) then return false local $total = FileGetSize($file) if $total < 44 then return false local $handle = FileOpen($file, $FO_BINARY) if $handle = -1 then return false local $head = FileRead($handle, 12) if @error or BinaryLen($head) < 12 then FileClose($handle) return false endif if BinaryToString(BinaryMid($head, 1, 4)) <> "RIFF" or BinaryToString(BinaryMid($head, 9, 4)) <> "WAVE" then FileClose($handle) return false endif local $offset = 12 local $format = 0 local $channels = 0 local $block = 0 local $bits = 0 local $found = false local $data_offset = 0 local $data_bytes = 0 while $offset + 8 <= $total and $offset < 1048576 FileSetPos($handle, $offset, $FILE_BEGIN) local $chunk = FileRead($handle, 8) if @error or BinaryLen($chunk) < 8 then exitloop local $id = BinaryToString(BinaryMid($chunk, 1, 4)) local $len = Wave_U32($chunk, 5) if $id = "fmt " then local $fmt = FileRead($handle, 16) if @error or BinaryLen($fmt) < 16 then exitloop $format = Wave_U16($fmt, 1) $channels = Wave_U16($fmt, 3) $block = Wave_U16($fmt, 13) $bits = Wave_U16($fmt, 15) if $format = $WAVE_FORMAT_EXTENSIBLE and $len >= 40 then local $ext = FileRead($handle, 24) if @error or BinaryLen($ext) < 24 then exitloop $format = Wave_U16($ext, 9) local $valid = Wave_U16($ext, 3) if $valid > 0 and $valid <= $bits then $bits = $valid endif $found = true elseif $id = "data" then $data_offset = $offset + 8 $data_bytes = $len if $found then exitloop endif $offset += 8 + $len if Mod($len, 2) = 1 then $offset += 1 wend local $ok = $found and $data_offset > 0 and $data_bytes > 0 and $channels >= 1 and $channels <= 8 and $block > 0 if $ok then if $format = $WAVE_FORMAT_PCM then $ok = $bits = 8 or $bits = 16 or $bits = 24 or $bits = 32 elseif $format = $WAVE_FORMAT_FLOAT then $ok = $bits = 32 or $bits = 64 elseif $format = $WAVE_FORMAT_ADPCM_MS then $ok = $bits = 4 and $channels <= 2 and $block > 7 * $channels and $block <= 65536 and Mod(($block - 7 * $channels) * 2, $channels) = 0 else $ok = false endif endif if not $ok then FileClose($handle) return false endif if $format = $WAVE_FORMAT_ADPCM_MS then local $adpcm_ok = Wave_LoadADPCM($handle, $data_offset, $data_bytes, $block, $channels, $file, $total) FileClose($handle) return $adpcm_ok endif local $frames = Int($data_bytes / $block) if $frames < 32 then FileClose($handle) return false endif local $columns = $WAVE_COLUMNS if $columns > $frames then $columns = $frames local $peaks[$columns][2] for $c = 0 to $columns - 1 local $lo = 1.0 local $hi = -1.0 for $p = 0 to $WAVE_PROBES - 1 local $frame = Int(($c + ($p + 0.5) / $WAVE_PROBES) / $columns * $frames) if $frame >= $frames then $frame = $frames - 1 FileSetPos($handle, $data_offset + $frame * $block, $FILE_BEGIN) local $raw = FileRead($handle, $block) if @error or BinaryLen($raw) < $block then continueloop local $v = Wave_FrameValue($raw, $format, $channels, $bits) if $v < $lo then $lo = $v if $v > $hi then $hi = $v next $peaks[$c][0] = $lo $peaks[$c][1] = $hi next FileClose($handle) $wave_peaks = $peaks $wave_columns_done = $columns $wave_file = $file $wave_caption = Wave_BaseName($file) $wave_key_base = $file & "|" & $total & "|" & FileGetTime($file, 0, 1) & "|" & $columns return true endfunc ; Decodes MS ADPCM into peak pairs. Short files decode fully, long ; files sample block headers across the whole length so every column ; still gets points. Takes the open handle, data location, block size, ; channels, file path and total size. Writes the same peak, file, ; caption and key globals as the PCM loader. Returns true, false under ; 32 frames. Leaves the handle open for the caller to close. func Wave_LoadADPCM($handle, $data_offset, $data_bytes, $block, $channels, $file, $total) Wave_ClearPeaks() local $per = 2 + Int(($block - 7 * $channels) * 2 / $channels) local $blocks = Int($data_bytes / $block) local $frames = $blocks * $per if $frames < 32 then return false local $columns = $WAVE_COLUMNS if $columns > $frames then $columns = $frames local $peaks[$columns][2] for $c = 0 to $columns - 1 $peaks[$c][0] = 0.0 $peaks[$c][1] = 0.0 next local $coef1[7] = [256, 512, 0, 192, 240, 460, 392] local $coef2[7] = [0, -256, 0, 64, 0, -208, -232] local $adapt[16] = [230, 230, 230, 230, 307, 409, 512, 739, 605, 469, 532, 654, 516, 382, 351, 450] local $full = $frames <= $WAVE_ADPCM_FULL local $take = $blocks if not $full and $take > $WAVE_ADPCM_BLOCKS then $take = $WAVE_ADPCM_BLOCKS local $bin = 0 if $full then $bin = DllStructCreate("byte[" & $block & "]") for $b = 0 to $take - 1 local $idx = $b if $take < $blocks then $idx = Int($b * $blocks / $take) FileSetPos($handle, $data_offset + $idx * $block, $FILE_BEGIN) local $hdr = FileRead($handle, 7 * $channels) if @error or BinaryLen($hdr) < 7 * $channels then continueloop Wave_SetBytes($hdr) local $pred[2] = [0, 0] local $delta[2] = [16, 16] local $s1[2] = [0, 0] local $s2[2] = [0, 0] for $ch = 0 to $channels - 1 $pred[$ch] = Wave_GetByte(1 + $ch) if $pred[$ch] < 0 then $pred[$ch] = 0 if $pred[$ch] > 6 then $pred[$ch] = 6 $delta[$ch] = Wave_GetByte(1 + $channels + $ch * 2) + Wave_GetByte(2 + $channels + $ch * 2) * 256 if $delta[$ch] < 16 then $delta[$ch] = 16 $s1[$ch] = Wave_GetByte(1 + 3 * $channels + $ch * 2) + Wave_GetByte(2 + 3 * $channels + $ch * 2) * 256 if $s1[$ch] >= 32768 then $s1[$ch] -= 65536 $s2[$ch] = Wave_GetByte(1 + 5 * $channels + $ch * 2) + Wave_GetByte(2 + 5 * $channels + $ch * 2) * 256 if $s2[$ch] >= 32768 then $s2[$ch] -= 65536 next for $h = 0 to 1 local $hs = 0.0 for $ch = 0 to $channels - 1 if $h = 0 then $hs += $s1[$ch] / 32768 else $hs += $s2[$ch] / 32768 endif next Wave_PutPeak($peaks, Int(($idx * $per + $h) / $frames * $columns), $columns, $hs / $channels) next if not $full then continueloop FileSetPos($handle, $data_offset + $idx * $block + 7 * $channels, $FILE_BEGIN) local $raw = FileRead($handle, $block - 7 * $channels) if @error or BinaryLen($raw) < $block - 7 * $channels then continueloop DllStructSetData($bin, 1, $raw) for $k = 2 to $per - 1 local $j = $k - 2 if $channels = 1 then $j = Int($j / 2) local $byte = DllStructGetData($bin, 1, 1 + $j) local $sum = 0.0 for $ch = 0 to $channels - 1 local $unib = Mod($byte, 16) if $channels > 1 and $ch = 0 then $unib = Int($byte / 16) if $channels = 1 and Mod($k, 2) = 0 then $unib = Int($byte / 16) local $v = $unib if $v >= 8 then $v -= 16 local $s = Wave_Sar8($s1[$ch] * $coef1[$pred[$ch]] + $s2[$ch] * $coef2[$pred[$ch]]) + $v * $delta[$ch] if $s > 32767 then $s = 32767 if $s < -32768 then $s = -32768 $delta[$ch] = Wave_Sar8($adapt[$unib] * $delta[$ch]) if $delta[$ch] < 16 then $delta[$ch] = 16 $s2[$ch] = $s1[$ch] $s1[$ch] = $s $sum += $s / 32768 next Wave_PutPeak($peaks, Int(($idx * $per + $k) / $frames * $columns), $columns, $sum / $channels) next next $wave_peaks = $peaks $wave_columns_done = $columns $wave_file = $file $wave_caption = Wave_BaseName($file) $wave_key_base = $file & "|" & $total & "|" & FileGetTime($file, 0, 1) & "|" & $columns return true endfunc ; Stores one mono level into a column peak pair. Takes the peaks ; array by reference plus column, column count and level. Clamps the ; column into range then widens min and max. Returns nothing. Keeps ; the three peak recording loops sharing one update rule. func Wave_PutPeak(byref $peaks, $col, $columns, $v) if $col < 0 then $col = 0 if $col >= $columns then $col = $columns - 1 if $v < $peaks[$col][0] then $peaks[$col][0] = $v if $v > $peaks[$col][1] then $peaks[$col][1] = $v endfunc ; Shifts right by 8 keeping the sign, exact for negatives. Takes any ; whole number. Returns the floor of division by 256. Used by the ; ADPCM decoder where C style arithmetic shift is required. func Wave_Sar8($v) if $v >= 0 then return Int($v / 256) return -Int((-$v + 255) / 256) endfunc ; Decodes one interleaved frame to a mono level between -1 and 1. Takes ; the raw frame bytes plus the fmt values, averages all channels so a ; loud side never hides a quiet one. Returns the level, 0 for anything ; unreadable. Handles integer PCM 8 to 32 bit and float 32 and 64 bit. func Wave_FrameValue($raw, $format, $channels, $bits) Wave_SetBytes($raw) local $sum = 0.0 local $bps = Int($bits / 8) for $ch = 0 to $channels - 1 local $o = $ch * $bps + 1 local $s = 0.0 if $format = $WAVE_FORMAT_PCM then switch $bits case 8 $s = (Wave_GetByte($o) - 128) / 128 case 16 local $w16 = Wave_GetByte($o) + Wave_GetByte($o + 1) * 256 if $w16 >= 32768 then $w16 -= 65536 $s = $w16 / 32768 case 24 local $w24 = Wave_GetByte($o) + Wave_GetByte($o + 1) * 256 + Wave_GetByte($o + 2) * 65536 if $w24 >= 8388608 then $w24 -= 16777216 $s = $w24 / 8388608 case 32 local $w32 = Wave_GetByte($o) + Wave_GetByte($o + 1) * 256 + Wave_GetByte($o + 2) * 65536 + Wave_GetByte($o + 3) * 16777216 if $w32 >= $WAVE_SIGN_BIT32 then $w32 -= $WAVE_NEG32 $s = $w32 / $WAVE_SIGN_BIT32 endswitch else if $bits = 32 then local $u32 = Wave_GetByte($o) + Wave_GetByte($o + 1) * 256 + Wave_GetByte($o + 2) * 65536 + Wave_GetByte($o + 3) * 16777216 $s = Wave_Float32($u32) else local $lo32 = Wave_GetByte($o) + Wave_GetByte($o + 1) * 256 + Wave_GetByte($o + 2) * 65536 + Wave_GetByte($o + 3) * 16777216 local $hi32 = Wave_GetByte($o + 4) + Wave_GetByte($o + 5) * 256 + Wave_GetByte($o + 6) * 65536 + Wave_GetByte($o + 7) * 16777216 $s = Wave_Float64($lo32, $hi32) endif endif $sum += $s next local $v = $sum / $channels if $v > 1 then $v = 1 if $v < -1 then $v = -1 return $v endfunc ; Decodes 32 bit IEEE bits to a level. Takes the frame sample as an ; unsigned 32 bit number. Returns the value clamped to -1 to 1, 0 for ; not a number. Subnormals collapse near zero, infinities clamp. func Wave_Float32($u) local $sign = 1.0 if $u >= $WAVE_SIGN_BIT32 then $sign = -1.0 $u -= $WAVE_SIGN_BIT32 endif local $exp = Int($u / $WAVE_F32_LEVELS) local $mant = $u - $exp * $WAVE_F32_LEVELS if $exp = 0 then return $sign * $mant / $WAVE_F32_LEVELS * $WAVE_F32_SUB if $exp = $WAVE_F32_MAX then if $mant = 0 then return $sign return 0.0 endif local $v = $sign * (1 + $mant / $WAVE_F32_LEVELS) * (2 ^ ($exp - $WAVE_F32_BIAS)) if $v > 1 then $v = 1 if $v < -1 then $v = -1 return $v endfunc ; Decodes 64 bit IEEE bits to a level. Takes the low and high 32 bit ; words of one little endian sample. Returns the value clamped to -1 to ; 1, 0 for not a number. Subnormals return 0, infinities clamp. func Wave_Float64($lo, $hi) local $sign = 1.0 if $hi >= $WAVE_SIGN_BIT32 then $sign = -1.0 $hi -= $WAVE_SIGN_BIT32 endif local $exp = Int($hi / $WAVE_F64_HI_SCALE) local $mant = ($hi - $exp * $WAVE_F64_HI_SCALE) * $WAVE_F64_LO_SCALE + $lo if $exp = 0 then return 0.0 if $exp = $WAVE_F64_MAX then if $mant = 0 then return $sign return 0.0 endif local $v = $sign * (1 + $mant / $WAVE_F64_MANT_SCALE) * (2 ^ ($exp - $WAVE_F64_BIAS)) if $v > 1 then $v = 1 if $v < -1 then $v = -1 return $v endfunc ; Reads a little endian unsigned 16 bit number. Takes binary data and a ; 1 based offset. Returns the value. Used for the wav fmt fields. func Wave_U16($data, $pos) Wave_SetBytes($data) return Wave_GetByte($pos) + Wave_GetByte($pos + 1) * 256 endfunc ; Reads a little endian unsigned 32 bit number. Takes binary data and a ; 1 based offset. Returns the value. Used for chunk lengths and samples. func Wave_U32($data, $pos) Wave_SetBytes($data) return Wave_GetByte($pos) + Wave_GetByte($pos + 1) * 256 + Wave_GetByte($pos + 2) * 65536 + Wave_GetByte($pos + 3) * 16777216 endfunc ; Copies raw bytes into the shared frame struct. Takes any binary up to ; 64 bytes, the largest frame this parser accepts. Returns nothing. One ; copy per frame keeps the per byte reads below cheap struct lookups. func Wave_SetBytes($data) DllStructSetData($wave_octets, 1, $data) endfunc ; Reads one byte from the shared frame struct. Takes a 1 based position. ; Returns 0 to 255. Asc cannot read binary bytes, this struct can, which ; is why every sample and header number flows through here. func Wave_GetByte($pos) return DllStructGetData($wave_octets, 1, $pos) endfunc ; Returns the file name without folders. Takes any full path with either ; slash style. Returns the tail after the last slash, or the input when ; no slash exists. Used for the caption drawn under the waveform. func Wave_BaseName($path) local $a = StringInStr($path, "\", 0, -1) local $b = StringInStr($path, "/", 0, -1) if $b > $a then $a = $b if $a > 0 then return StringMid($path, $a + 1) return $path endfunc ; Returns the lower case extension without the dot. Takes any path. ; Returns "" when no dot follows the file name. Keeps the include free ; of the shared GetExtension helper so it stays self contained. func Wave_Extension($path) local $base = Wave_BaseName($path) local $dot = StringInStr($base, ".", 0, -1) if $dot <= 0 then return "" return StringLower(StringMid($base, $dot + 1)) endfunc ; Decides an ini truth word. Takes the raw ini string. Returns true for ; 1, true, yes and on in any case with spaces, false for the rest. Used ; once at init for the saved wave_view key. func Wave_IsTrue($raw) local $clean = StringLower(StringStripWS($raw, 3)) if $clean = "1" or $clean = "true" or $clean = "yes" or $clean = "on" then return true return false endfunc ; Forgets the current file and peaks. Clears file, caption, key base, ; signature and the peak array. Returns nothing. Called before every load ; and on bad input so a stale wave can never linger. func Wave_ClearPeaks() $wave_peaks = 0 $wave_columns_done = 0 $wave_file = "" $wave_caption = "" $wave_key_base = "" $wave_signature = "" endfunc ; Deletes one GDI bitmap handle. Takes the HBITMAP from the STM_SETIMAGE ; swap. Returns nothing. Small wrapper around gdi32 so this include needs ; no WinAPI include of its own. func Wave_DeleteObject($bitmap) DllCall("gdi32.dll", "bool", "DeleteObject", "handle", $bitmap) endfunc