Skip to content

Instantly share code, notes, and snippets.

Richard Tier richtier

Block or report user

Report or block richtier

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@richtier
richtier / javascript.js
Created Sep 18, 2018
JavaScript webaudio to wav encoder
View javascript.js
class WavAudioEncoder {
encode = buffer => {
const channelData = buffer.getChannelData(0);
const length = buffer.length;
const data = []
for (let i = 0; i < length; ++i) {
let x = channelData[i] * 0x7fff;
data.push(Math.round(x < 0 ? Math.max(x, -0x8000) : Math.min(x, 0x7fff)));
}
View float_to_16_bit_pcm.py
import array
import struct
def float_to_16_bit_pcm(raw_floats):
floats = array.array('f', raw_floats)
samples = [sample * 32767 for sample in floats]
raw_ints = struct.pack("<%dh" % len(samples), *samples)
return raw_ints
@richtier
richtier / thing.jsx
Last active Apr 4, 2016
react material-ui form field validation redux es6
View thing.jsx
// NewProductDescriptionField.jsx
import React from 'react';
import { connect } from 'react-redux';
import TextField from 'material-ui/lib/text-field';
import {
setFormFieldValidityMessage,
setNewProductData,
} from '../../actions';
@richtier
richtier / gist:8484200
Created Jan 18, 2014
append url with slash if not present
View gist:8484200
function appendSlash(url){
return url + /\/$/.test(url) ? '' : '/';
}
@richtier
richtier / non-model-serialize
Last active Jan 3, 2016
serialize non django model
View non-model-serialize
from rest_framework.views import APIView
from rest_framwrork import serializers
class NonDjangoModelSerializer(serializers.Serializer):
class Meta:
fields = ('field1', 'field1',)
field1 = serializers.Field()
field1 = serializers.Field()
@richtier
richtier / jquery templating
Created Jan 8, 2014
quick and dirty jquery templating
View jquery templating
// render playlist of songs
var template = '<li/><a>{{title}} by {{artist}}</a></li>';
context = [
{title: 'Killing Me Softly', artist: 'Roberta Flack'},
{title: 'Run Around Sue', artist: 'Dion'},
{title: 'Will You Still Love Me Tomorrow', artist: 'Shirelles'}
];
$('<ul/>', {text: 'songs'}).html(
$.map(context, function(item){
@richtier
richtier / querrystring to object
Last active Dec 30, 2015
querrystring to object
View querrystring to object
/**
* converts querystring to object. Allows multiple values per key
* @param {String} querystring - optional querystring. default address bar
* @return {object}
*/
self.querystringToObject = function(querystring){
var params = {};
(querystring || window.location.search)
.replace(/([^?=&]+)(=([^&]*))?/g, function($0, $1, $2, $3) {
if (params[$1]){
@richtier
richtier / ajax and CSRF
Last active Dec 25, 2015
I was asked "I am running into the 403 forbidden issue with django and ajax and was wondering if you could help me a little."
View ajax and CSRF
(note, the concept definitely works, but the implementation may have some bug, spelling mistakes etc).
Your problem is your request is missing the Cross Site Request Forgery token (CSRF).
This is a precaution django provide automatically to prevent others doing nefarious stuff.
Non-POST requests don't need the CSRF token, but for POST we need to do some magic:
we need to pass the CSRF token from django to javascript. This is best done like so:
You can’t perform that action at this time.