Skip to content

Instantly share code, notes, and snippets.

@pabloko
pabloko / WebBrowser.cpp
Last active March 16, 2016 21:00
Awesomium on Direct3D9 C++ class
#include "main.h"
#include "WebBrowser.h"
#include <direct.h>
CWebBrowser::CWebBrowser()
{
script = NULL;
webCore = NULL;
webView = NULL;
tWebPNG = NULL;
@pabloko
pabloko / AndroidJSInterfaceComplexTypes.java
Created December 8, 2018 13:20
Lets have any JSON serializable type and methods to callback on Android WebView's JSInterface
//How it works: all the script relies on a tiny javascript stub injected to webView's document that uses Proxy api and json serialicing stuff. Calls to our internal objects are proxified to a single method "__mm_handle_method", that using reflection find the target method and populate arguments exchanged in json, plus a custom interface for methods passed as argument.
package es.pabloko.webviewbridge;
import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.JavascriptInterface;

Fast short previews from videos

Hi! as I needed to create bulk previews of long videos, like short cuts merged on one scaled video. As i searched for some examples to do this using ffmpeg, stumbled upon a common problem on every snippet there and here... long story short: seeking will make you waste a lot of time unless you seek cleverly.

This script will do the only fast seeking method seeming to work, -ss from input, pipe a chunk and exit that process. Several chunks are piped the encoder process that writes final file. Theres no temporary files, and if your source is h264, also no transcoding on input reading.

@pabloko
pabloko / 01.(JS) Half duplex XHR socket interface.md
Last active October 9, 2019 23:41
(JS) Half duplex XHR socket interface

(JS) Half duplex XHR socket interface

Advice: if you're looking to use this on production, better take a look at https://github.com/eBay/jsonpipe

Keeping the clients updated in real time apps is quite challenging sometimes, websockets or webrtc are great for this task, but usually what the developer want is to send most of the data well timed to the client, then recive a few commands from the client, so having full duplex socket in this scenario is quite wasteful, and may pose a security risk, at allowing the user to continuosly send arbitrary data. Polled ajax request, in the other hand cant be in any way correctly timed, so this function acts as an intermediante point between ajax requests and websockets.

This function needs a request that is kept alive then chunked data is being sent. It can be binary data, strings, or serializable json objects. You will have to place chunked headers Transfer-Encoding: chunked and ping regularly (5s should be fine) sending some data in order to maintain the conne

@pabloko
pabloko / 01.(PHP) Pre-processing MP3 Audio waveforms with big size.md
Created December 14, 2019 12:44
(PHP) Pre-processing MP3 Audio waveforms with big size

What?

When want to display audio waveform from audio file, you can find browser libraries like WavesurferJs, Squiggl, or whatever, everyone works the same:

  1. Load audio (in full) file and get its AudioContext
  2. Get all the raw PCM samples from one channel in Float32Array (huge) with something like ctx.decodeAudioData().getChannelData(0)
  3. Use drawing routine and average samples

Doing this process on the users browser is quite slow and wasteful in a magnitude proportional to the audio file size, as you have to fully load it then get huge amount of samples calculated in the drawing stage.

So one option to overcome this is to dump a shorter version of PCM data on a static file with an amount of samples enough to render the waveform and having a reasonable size by averaging the samples.

@pabloko
pabloko / readme.md
Created March 18, 2020 18:49
(C/C++) Embed file assets into binaries automaticly with VisualStudio build tasks

(C/C++) Embed file assets into binaries automaticly with VisualStudio build tasks

Sometimes developing apps, we need to reduce the build to a single exe containing handful of asset files, in this scenario, you can use the great library PhysFS that will let you zip everithing and load those files on runtime. We can further automatize this process by adding a VisualStudio Pre-build event that zips a custom folder, but also packs it on the final executable.

To start, locate your project folder and add a www asset directory and vendor directory, where we will put neccessary files 7z.exe, 7z.dll, objcopy.exe, www.bat 7zip will pack the files on a generated "resources.zip" this can be edited or even removed, it may even include the usage of zip password as PhysFS supports it. Objcopy, its part of some VS distributions and generated a .lib static library with the asset, exporting its start and end address.

Pre-build event: call "$(MSBuildProjectDirectory)\vendor\www.bat"

/vendor/www.bat (notice you w

@pabloko
pabloko / functions.php
Created April 20, 2020 13:32
[PHP/Wordpress] Define a default post password with filters and hooks
add_filter( 'post_password_required', function( $returned, $post )
{
//Define here your conditional logic, example filter posts without password from custom post type "photo_gallery"
if (empty($post->post_password) && $post->post_type=="photo_galery")
$post->post_password='Secretpw123';
//End of conditional logic. Returning true will ask for password, false will display the content.
require_once ABSPATH . WPINC . '/class-phpass.php';
$hasher = new PasswordHash( 8, true );
@pabloko
pabloko / ChooseFont0.md
Last active May 16, 2020 21:37
(C) ChooseFont color picker with extended color dialog

The ChooseFont dialog color picker extended

Using ChooseFont you will notice it only support 15 predefined an ugly colors. The color palette is very limited and theres no way to use more colors without subclassing it. Here is a good start point to subclass it, this mod will:

  • Display the color in HTML format
  • Display a full featured ChooseColor dialog when color ComboBox is clicked

@pabloko
pabloko / 1-D3D9IEOverlay.md
Last active September 29, 2022 16:34
D3D9 Overlay with IE and alpha

D3D9 Overlay with IE and alpha

Many times working with Direct3D9, you want to build a simple GUI to overlay over your graphics, many libraries does this in some way but at some point you may want to use a web browser and options are much more limited. CEF is great, but heavy and IPC can be a real pain to handle, ultralight stopped supporting x86 arch... few other webui libs out there, they all have its pros and cons but we lost the sense of simplicity we wanted on first hand.

Then theres MSHTML (Internet Explorer) that is shipped on every windows os, enough versatile and fast to be used as overlay. Shamefully for Microsoft, this control never supported transparent background, even on .net webforms/WPF webbrowser depends from MSHTML and always cast a background.

This demo explores two ways to overcome this long standing issue and get 1bit alpha or 255bit alpha by color difference interpolation.

  • 1bit alpha: Uses green rgb(0,255,0) background that is simply keyed, can be used on boxed UI with less re
@pabloko
pabloko / serial.cpp
Created April 27, 2021 19:09
Lua 5.1 / luajit - Serial port library (rs232) [win/mac/linux]
//Using rs232 library: https://github.com/mrh1997/rs232/ by Frédéric Meslin, Florent Touchard
#include <Windows.h>
#pragma comment(lib, "kernel32")
#pragma comment(lib, "lua5.1.lib")
#include <lua.hpp>
#include "rs232.h"
char buffer[0xFFF] = { 0 };
BOOL luaopen_serial (lua_State* L)
{