Wednesday, October 22, 2014

[Lua]Get BPM/Tempo from WAV file

How do i get the tempo from WAV file? nah some Lua code can do that.

local function num2float (c)	-- http://stackoverflow.com/questions/18886447/convert-signed-ieee-754-float-to-hexadecimal-representation
	if c == 0 then return 0.0 end
	local c = string.gsub(string.format("%X", c),"(..)",function (x) return string.char(tonumber(x, 16)) end)
	local b1,b2,b3,b4 = string.byte(c, 1, 4)
	local sign = b1 > 0x7F
	local expo = (b1 % 0x80) * 0x2 + math.floor(b2 / 0x80)
	local mant = ((b2 % 0x80) * 0x100 + b3) * 0x100 + b4
	if sign then
		sign = -1
	else
		sign = 1
	end
	local n
	if mant == 0 and expo == 0 then
		n = sign * 0.0
	elseif expo == 0xFF then
		if mant == 0 then
			n = sign * math.huge
		else
			n = 0.0/0.0
		end
	else
		n = sign * math.ldexp(1.0 + mant / 0x800000, expo - 0x7F)
	end
	return n
end
 
local function int2char(int)
	return string.char(int%256)..string.char(math.floor(int/256%256))..string.char(math.floor(int/256/256%256))..string.char(math.floor(int/256/256/256))
end
 
local function char2int(char)
	return char:sub(1,1):byte()+char:sub(2,2):byte()*256+char:sub(3,3):byte()*65536+char:sub(4,4):byte()*16777216
end
 
function getWAVTempo(wav,verbose)
	local vp=function() end
	local f
	if verbose then vp=print end
	vp("getWAVTempo start!")
	if type(wav)=="userdata" then
		vp("#1 type is userdata. Assuming it's a file type!")
		f=wav
	else
		vp("#1 type is string. It's path to wav file")
		f=assert(io.open(wav,"rb"))
	end
	if verbose then vp=function(txt) print("["..string.format("%08X",f:seek("cur")).."] "..txt) end end
	local curSeek=f:seek("cur")
	local fileSize=f:seek("end")
	f:seek("set")
	if f:read(12)=="RIFF"..int2char(fileSize-8).."WAVE" then
		vp("WAV File header correct. Reading chunks!")
		while(f:seek("cur")~=fileSize)do
			local chunk=f:read(4)
			if(chunk=="acid")then
				vp("\"acid\" chunk found. Getting tempo data!")
				f:read(24)
				tempo=num2float(char2int(f:read(4)))
				vp("Tempo data found!")
				if type(wav)=="string" then f:close()
				else f:seek("set",curSeek) end
				return tempo
			else
				local size=char2int(f:read(4))
				vp("\""..chunk.."\" chunk found with size of "..size..". Skipping...")
				if size%2==1 then size=size+1 end
				f:seek("cur",size)
			end
		end
	else
		error("Invalid wav file!")
	end
	if verbose then print("Tempo data not found!") end
	if type(wav)=="userdata"then f:seek(curSeek)
	else f:close() end
	return 0
end

What do you need to do is:
  1. Save code above to file
  2. dofile it
  3. call getWAVTempo(file[,verbose])
    getWAVTempo Parameters:
    file - file handle(from io.open) or string to filename
    verbose - show more message. If you set this to true, you can see what's going on.
  4. a. If it returns 0, then the wav file does not come with embedded tempo data
    b. If it returns value more than 0, then that's the tempo. Please note that the decimals is stripped on return.
That's it.
Feel free to use some (or all) parts of code above.

Monday, October 20, 2014

[C#]Synaptics custom touchpad gesture code.

Nah, my laptop cannot use updated version of the synaptics touchpad driver. The driver that comes with it does lack of using some windows 8 touchpad gesture. I'm start to think, maybe i can access the Synaptics API and yes, that's possible. After a lot search in google, i have a code that allows to switch to Windows apps and show Charm bar with touchpad gesture.

If someone need it, feel free to check my source code(well, it's dirty written)(Requires Input Simulator)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
using SYNCTRLLib;
using WindowsInput;
 
namespace mytest2 {
    class MyPoint {
        public int x;
        public int y;
        public MyPoint(int a,int b) {
            x=a;
            y=b;
        }
        public MyPoint() {
            x=0;
            y=0;
        }
        public void Reset() {
            x=0;
            y=0;
        }
    }
    public struct Point {
        public int x,y;
    }
    class Program {
        // FingerState==589824=tap
        static MyPoint WinAppsPoint=new MyPoint(0,2000);
        static MyPoint CharmBarPoint=new MyPoint(5000,6143);
        static MyPoint FirstLast=new MyPoint();
        static SynAPICtrl apictrl=new SynAPICtrl();
        static SynDeviceCtrl devctrl=new SynDeviceCtrl();
        static SynPacketCtrl packetctrl=new SynPacketCtrl();
        static bool is_press=false;
        static int devh;
        [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
        public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
        [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
        public static extern int GetCursorPos(ref Point _);
        [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
        public static extern int SetCursorPos(int x,int y);
        [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
        public static extern int SendInput(uint n,Input[] list,int size);
        static double tempy1=0;
        static double tempy2=0;
        static void mycallback() {
            if(devctrl.LoadPacket(packetctrl)==1) {
                if(is_press) {
                    if(packetctrl.FingerState==589824) {    // Tap
                        FirstLast.Reset();
                        is_press=false;
                        return;
                    }
                    else if(packetctrl.FingerState<=512) {  // OK
                        is_press=false;
                        int myx=Math.Min(FirstLast.x,FirstLast.y),myy=Math.Max(FirstLast.x,FirstLast.y);
                        int r=(int)Math.Atan2(tempy2-tempy1,FirstLast.y-FirstLast.x);
                        Debug.WriteLine("Rotation: {0}",r);
                        if(myx>=WinAppsPoint.x && myy<=WinAppsPoint.y && FirstLast.x<FirstLast.y) {
                            Debug.WriteLine("Windows Apps Scroll");
                            Point temp=new Point();
                            GetCursorPos(ref temp);
                            SetCursorPos(0,0);
                            mouse_event(2|4,0,0,0,0);
                            SetCursorPos(temp.x,temp.y);
                        } else if(myx>=CharmBarPoint.x && myy<=CharmBarPoint.y && FirstLast.x>FirstLast.y && (r==3||r==(-3)||r==2||r==(-2))) {
                            Debug.WriteLine("Charm Bar");
                            InputSimulator myinpt=new InputSimulator();
                            myinpt.Keyboard.KeyDown(WindowsInput.Native.VirtualKeyCode.LWIN);
                            myinpt.Keyboard.KeyDown(WindowsInput.Native.VirtualKeyCode.VK_C);
                            myinpt.Keyboard.KeyUp(WindowsInput.Native.VirtualKeyCode.LWIN);
                            myinpt.Keyboard.KeyUp(WindowsInput.Native.VirtualKeyCode.VK_C);
                        }
                        return;
                    }
                    FirstLast.y=packetctrl.X;
                    tempy2=packetctrl.Y;
                }
                else if(packetctrl.FingerState>512 && packetctrl.FingerState!=589824) {
                    Debug.WriteLine("Press");
                    is_press=true;
                    FirstLast.x=packetctrl.X;
                    tempy1=packetctrl.Y;
                }
                //Console.WriteLine(packetctrl.FingerState);
                //Console.WriteLine("{0} {1}",packetctrl.X,packetctrl.Y);
            }
        }
        static void Main(string[] args) {
            apictrl.Initialize();
            apictrl.Activate();
            devh=apictrl.FindDevice(SynConnectionType.SE_ConnectionAny,SynDeviceType.SE_DeviceTouchPad,0);
            devctrl.Select(devh);
            devctrl.Activate();
            devctrl.OnPacket+=mycallback;
            while(true) {
                Thread.Sleep(16);
            }
        }
    }
}

Saturday, October 18, 2014

GTA San Andreas WeqWeq si Bebek skin

WeqWeq si Bebek.
Original Model by mr_weq/BayuProd
Conversion to SA by AuahDark

Download link in video description(direct)

Visit WeqWeq si Bebek Fanpage: https://www.facebook.com/WeqWeqSiBebek