Saturday, May 16, 2015

Thinking out Loud "creepy" sound explained

Yesterday(15 May 2015) i have chat with my friends about Thinking out Loud song by Ed Sheeran, he says that it has "creepy" sound in it and he says that the news has been spreading in the world, specially in Indonesia (i think). I don't think that there's a "creepy" sound in it, so solve this "mystery"

First, i load up FL Studio and WaveCandy plugin in Master track, also loaded preset "Wine" with disabled "Blur Behind"(because i use Windows 8). Also extending the width of the Visualizer

Then i load Thinking out Loud song in Playlist as Audio Clip by dragging the file from File Explore to FL Studio 12 Playlist

My friend says that the "creepy" sound is right after Ed Sheeran says "baby my heart" so i'm listening the song and i reach it, i send this sound sample to my friend first.
My friend then respond to the sample that i send and he says "yes, that's it, i can hear it", i try to increase my speaker's volume, try to find where is the sound, and i hear something weird.
It's very likely that his(Ed Sheeran) sound looks weird while he's sliding his sound, then i upload one more sample again to my friend.


Then i see spectrum and this image is generated when i play the song(lyric: "Baby my, heart")
I mark the "creepy" sound in blue in picture above.

That's it, so i hear it again and again and i can sure that it's just Ed Sheeran voice sound weird after he says "Heart".

Summary: It's just his sound that sound weird for us, my friend also says "And there's nothing wrong in the recording he says". So the "creepy" in this post is just his voice sliding and sound weird.

That's my opinion about this, your opinion might be different

Friday, January 2, 2015

Run Windows 8 Metro Application from Desktop Application

After failed to create Rainmeter plugin which launch windows 8 application, i decide to make an application which launch windows 8 application from console application.

File: IApplicationAcitvationManager.h (For GCC/G++ Compiler)
// IApplicationActivationManager for MinGW
#pragma once
#ifdef _MSC_VER
#error "Not for Microsoft Compiler!"
#endif
#ifndef _SHLOBJIDL_H 
#include <shobjidl.h>
#endif
 
#ifdef __cplusplus
extern "C" {
#endif
const IID IID_IApplicationActivationManager={0x2e941141,0x7f97,0x4756,{0xba,0x1d,0x9d,0xec,0xde,0x89,0x4a,0x3d}};  // Visual Studio 2012 ShObjIdl.h:9159
const CLSID CLSID_ApplicationActivationManager={0x45BA127D,0x10A8,0x46EA,{0x8A,0xB7,0x56,0xEA,0x90,0x78,0x94,0x3C}}; // Visual Studio 2012 ShObjIdl.h:9399
 
typedef enum ACTIVATEOPTIONS {
 AO_NONE=0,
 AO_DESIGNMODE=1,
 AO_NOERRORUI=2,
 AO_NOSPLASHSCREEN=4
} ACTIVATEOPTIONS;
 
#define INTERFACE IApplicationActivationManager
DECLARE_INTERFACE_(IApplicationActivationManager,IUnknown) {
 STDMETHOD(QueryInterface)(THIS_ REFIID,PVOID*) PURE;
 STDMETHOD_(ULONG,AddRef)(THIS) PURE;
 STDMETHOD_(ULONG,Release)(THIS) PURE;
 // Activates the specified Windows Store app for the generic launch contract (Windows.Launch) in the current session.
 STDMETHOD(ActivateApplication)(THIS_ const wchar_t*,const wchar_t*,ACTIVATEOPTIONS,unsigned long*) PURE;
 // Activates the specified Windows Store app for the file contract (Windows.File).
 // IShellItem doesn't exist. Replace with LPVOID
 STDMETHOD(ActivateForFile)(THIS_ const wchar_t*,void*,const wchar_t*,unsigned long*) PURE;
 // Activates the specified Windows Store app for the protocol contract (Windows.Protocol).
 // IShellItem doesn't exist. Replace with LPVOID
 STDMETHOD(ActivateForProtocol)(THIS_ const wchar_t*,void*,unsigned long*) PURE;
};
#undef INTERFACE
#ifdef __cplusplus
}
#endif

And here's the application to launch Windows 8 Application from desktop(File Apptest.cpp)
#include <stdio.h>
#include <Windows.h>
#include <ShObjIdl.h>
#include <string>
#ifndef _MSC_VER
#include "IApplicationActivationManager.h"
extern "C" __declspec(dllimport) HRESULT __stdcall CoAllowSetForegroundWindow(IUnknown *pUnk,LPVOID lpvReserved);
#endif
 
int main(int argc,char** argv) {
 IApplicationActivationManager* _;
 std::string __;
 size_t ___;
 if(argc<2) {
  printf("usage: %s <app package id>\r\n",argv[0]);
  return 1;
 }
 __=argv[1];
 CoInitializeEx(nullptr,COINIT_APARTMENTTHREADED);
 CoCreateInstance(CLSID_ApplicationActivationManager,nullptr,CLSCTX_LOCAL_SERVER,IID_IApplicationActivationManager,(void**)&_);
 CoAllowSetForegroundWindow(_,nullptr);
 _->ActivateApplication(std::wstring(__.begin(),__.end()).c_str(),nullptr,AO_NONE,(DWORD*)&___);
 return 0;
}

Command Line i used to Compile: g++ -std=c++11 -o ConsoleApplication1.exe -L"%VSLIBDIR%" -static-libgcc -static-libstdc++ ConsoleApplication1/AppTest.cpp -lole32 -loleaut32
Set VSLIBDIR to your Windows 8 SDK Lib directory(for CoInitializeEx and CoAllowSetForegroundWindow)(Windows 8 SDK)

Note: Tested in Visual Studio 2012 and GCC v4.8.1
Note2: Enum app package id powershell command(source):
$installedapps = get-AppxPackage
foreach ($app in $installedapps)
{
    foreach ($id in (Get-AppxPackageManifest $app).package.applications.application.id)
    {
        $app.packagefamilyname + "!" + $id
    }
}

Sunday, December 14, 2014

How to stream music with Windows Media Player 12

I recently getting problem in soundcloud where i can't play some tracks. After trying 3 times, i decided to open Web Console(Ctrl+Shift+K in Firefox) which shows me some security warnings. One of them is refer to "api dot soundcloud dot com / bla bla.. / streams" that says Cross-Origin Request is blocked. I'm spending about half-hour to activate CORS but Firefox seems not support that thing.



But...
I remember Internet Download Manager that sometimes shows "Download This Audio". Maybe i can do some kind of experiment with those links, ok, i decide to open the link and it shows some kind of JSON data like this:
{"http_mp3_128_url":"some_links_here","preview_mp3_128_url":"some_links_here_2"}
 
Maybe the "some_links_here" contains the mp3 file which can be used to stream the mp3 from WMP instead.
I try to copy the "some_links_here" link and paste it to WMP for stream and it worked. Here's how
1. Open Windows Media Player
1.a. For faster, just press Win+R and type "wmplayer"
2. Press Ctrl+U and paste the link at there
3. Press OK.
4. Wait for some time and it will start to stream the mp3 from your WMP.
4.a. If it shows error, maybe the link already expired, in that case, try to reopen the soundcloud page.
Well, after some investigation that i do myself, it seems that it only throws security warning when it try to use "OPTIONS" request method instead of "POST"(maybe) or "GET".

Okay, one more question. How to do this in OneDrive?
Okay i also have a story about this today. I'm uploading 3 files to my OneDrive and only 1 file is uplaoded with very slow speed.
Then, after the file has beed uploaded successfully, it shows that the rest of 2 files(that failed) has beed uploaded successfully too.
I need to verify that those 2 files doesn't corrupted first so i do these things:
1. Right click the file you want to stream
2. Click Download
3. When download dialog pops up, try to copy only the link.
3.a. If it doesn't allow you to do so(like bulit-in firefox download), try to press "Download" and then abort it(also read 3.b for next steps for this)
3.b. Shows your download(Ctrl+J) and you find your aborted download. Right click on it and press "Copy Download Link"(or something similar)
4. Repeat step 1-4 above(in soundcloud stream method)

Monday, November 24, 2014

[C++|Windows 8]Get Accent & Background Color from Desktop Application

Windows 8 has some great features. Specially in the Metro design with some nice colors. This code will show how to get current used(selected) color in your start screen(personalization)


#include <windows.h>
#include <iostream>
 
typedef struct PersonalizationColor {
 int r;
 int g;
 int b;
} PersonalizationColor;
 
static const unsigned char ColorSet_Version3[25][2][3] = {    // Table for Accent & Background color in Windows 8
 {{37,37,37},{244,179,0}},
 {{37,37,37},{120,186,0}},
 {{37,37,37},{38,115,236}},
 {{37,37,37},{174,17,61}},
 {{46,23,0},{99,47,0}},
 {{78,0,0},{176,30,0}},
 {{78,0,56},{193,0,79}},
 {{45,0,78},{114,0,172}},
 {{31,0,104},{70,23,180}},
 {{0,30,78},{0,106,193}},
 {{0,77,96},{0,130,135}},
 {{0,74,0},{25,153,0}},
 {{21,153,42},{0,193,63}},
 {{229,108,25},{255,152,29}},
 {{184,27,27},{255,46,18}},
 {{184,27,108},{255,29,119}},
 {{105,27,184},{170,64,255}},
 {{27,88,184},{31,174,255}},
 {{86,156,227},{86,197,255}},
 {{0,170,170},{0,216,204}},
 {{131,186,31},{145,209,0}},
 {{211,157,9},{225,183,0}},
 {{224,100,183},{255,118,188}},
 {{105,105,105},{0,164,164}},
 {{105,105,105},{255,125,35}}
};
 
typedef struct windows_version {
 int major;    // CurrentVersion
 int minor;    // CurrentVersion
 int build_number;  // CurrentBuildNumber
 char name[256];   // ProductName
 char service_pack[256]; // CSDVersion(if exist)
 char edition[128];  // EditionID(if exist)
} windows_version;
 
int get_winver(windows_version* ptr) {
 HKEY temp;
 unsigned char CurrentVersion[8];
 long long long_temp=8;
 if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows NT\\CurrentVersion",0,KEY_READ,&temp)==ERROR_SUCCESS) {
  int a=0;
  if(RegQueryValueEx(temp,"CurrentVersion",0,nullptr,CurrentVersion,(LPDWORD)&long_temp)!=ERROR_SUCCESS)
   return 0;
  double current_version=strtod((char*)CurrentVersion,nullptr);
  ptr->major=(int)floor(current_version);
  ptr->minor=(int)floor(current_version*10.0)-ptr->major*10;
  long_temp=8;
  memset(CurrentVersion,0,8);
  if((a=RegQueryValueEx(temp,"CurrentBuildNumber",0,nullptr,CurrentVersion,(LPDWORD)&long_temp))!=ERROR_SUCCESS)
   return 0;
  ptr->build_number=atoi((char*)CurrentVersion);
  long_temp=256;
  if(RegQueryValueEx(temp,"ProductName",0,nullptr,(LPBYTE)ptr->name,(LPDWORD)&long_temp)!=ERROR_SUCCESS)
   return 0;
  int* from_lt=(int*)&long_temp+4;
  long_temp=256;
  *from_lt=RegQueryValueEx(temp,"CSDVersion",0,nullptr,(LPBYTE)ptr->service_pack,(LPDWORD)&long_temp);
  if(*from_lt==ERROR_FILE_NOT_FOUND)
   memset(ptr->service_pack,0,256);
  else if(*from_lt!=ERROR_SUCCESS)
   return 0;
  long_temp=128;
  *from_lt=RegQueryValueEx(temp,"EditionID",0,nullptr,(LPBYTE)ptr->edition,(LPDWORD)&long_temp);
  if(*from_lt==ERROR_FILE_NOT_FOUND)
   memset(ptr->edition,0,128);
  else if(*from_lt!=ERROR_SUCCESS)
   return 0;
 } else
  return 0;
 RegCloseKey(temp);
 return 1;
}
 
bool Win8GetAccentColor(PersonalizationColor* col) {
 windows_version win;
 get_winver(&win);
 if(win.major>=6 && (win.minor>=2 && win.minor<=3)) {
  HKEY temp;
  int r=0;
  if((r=RegOpenKeyExA(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Accent",0,KEY_READ,&temp))==ERROR_SUCCESS) {
   auto accent_color=0UL;
   auto buf=4UL;
   if(win.minor==2) {
    if((r=RegQueryValueExA(temp,"ColorSet_Version3",nullptr,nullptr,(LPBYTE)&accent_color,&buf))==ERROR_SUCCESS) {
     col->r=ColorSet_Version3[accent_color][1][0];
     col->g=ColorSet_Version3[accent_color][1][1];
     col->b=ColorSet_Version3[accent_color][1][2];
     RegCloseKey(temp);
     return true;
    } else {
     RegCloseKey(temp);
     return false;
    }
   } else {
    if((r=RegQueryValueExA(temp,"AccentColor",nullptr,nullptr,(LPBYTE)&accent_color,&buf))==ERROR_SUCCESS) {
     col->r=GetRValue(accent_color);
     col->g=GetGValue(accent_color);
     col->b=GetBValue(accent_color);
     RegCloseKey(temp);
     return true;
    } else {
     RegCloseKey(temp);
     return false;
    }
   }
  } else
   return false;
 } else
  return false;
}
 
bool Win8GetBackgroundColor(PersonalizationColor* col) {
 windows_version win;
 get_winver(&win);
 if(win.major>=6 && (win.minor>=2 && win.minor<=3)) {
  HKEY temp;
  int r=0;
  if((r=RegOpenKeyExA(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Accent",0,KEY_READ,&temp))==ERROR_SUCCESS) {
   auto accent_color=0UL;
   auto buf=4UL;
   if(win.minor==2) {
    if((r=RegQueryValueExA(temp,"ColorSet_Version3",nullptr,nullptr,(LPBYTE)&accent_color,&buf))==ERROR_SUCCESS) {
     col->r=ColorSet_Version3[accent_color][0][0];
     col->g=ColorSet_Version3[accent_color][0][1];
     col->b=ColorSet_Version3[accent_color][0][2];
     RegCloseKey(temp);
     return 3;
    } else {
     RegCloseKey(temp);
     return true;
    }
   } else {
    if((r=RegQueryValueExA(temp,"StartColor",nullptr,nullptr,(LPBYTE)&accent_color,&buf))==ERROR_SUCCESS) {
     col->r=GetRValue(accent_color);
     col->g=GetGValue(accent_color);
     col->b=GetBValue(accent_color);
     RegCloseKey(temp);
     return true;
    } else {
     RegCloseKey(temp);
     return false;
    }
   }
  } else {
   return false;
  }
 } else
  return false;
}
 
int main() {
 PersonalizationColor accent;
 PersonalizationColor background;
 if(!Win8GetAccentColor(&accent)) {
  std::cerr << "Cannot get accent color!" << endl;
  return 1;
 }
 if(!Win8GetBackgroundColor(&background)) {
  std::cerr << "Cannot get background color!" << endl;
  return 1;
 }
 std::cout << "Accent: " << accent.r << "," << accent.g << "," << accent.g << endl << "Background: " << background.r << "," << background.g << "," << background.b << endl;
 return 0;
}

Note1: It also work in Windows 8.1 without any edits
Note2: It can be ported to C with some editing.
Note3: I give a bonus code. You can use get_winver to get windows version(which i think is better than GetVersionEx)

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.