Skip to content

Instantly share code, notes, and snippets.

@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 / 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

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 / 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;
@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;