Skip to content

Instantly share code, notes, and snippets.

Avatar

Liu Yiling lyleaf

  • Google
  • Australia
View GitHub Profile
@lyleaf
lyleaf / tempo_animation_prototype.py
Created Jan 7, 2020
Create animation which is synced up to the music tempo.
View tempo_animation_prototype.py
import cv2
import librosa
import librosa.display
import wave
import pyaudio
from cv2 import VideoWriter, VideoWriter_fourcc
VIDEO_PATH = 'Demo_Full_1.mp4'
video = cv2.VideoCapture(VIDEO_PATH)
View stream_beat_v2.py
import pyaudio
import librosa
import numpy as np
import requests
from pyo import *
import time
from numpy_ringbuffer import RingBuffer
pa = pyaudio.PyAudio()
View stream_beat.py
"""PyAudio Example: Play a wave file (callback version)."""
import pyaudio
import wave
import time
import sys
import librosa
import numpy as np
import scipy
import matplotlib.pyplot as plt
@lyleaf
lyleaf / pyaudio_recorder.py
Created Nov 10, 2019
pyaudio_recorder.py
View pyaudio_recorder.py
import pyaudio
import wave
sample_format = pyaudio.paFloat32 # 16 bits per sample
channels = 1
fs = 44100 # Record at 44100 samples per second
seconds = 10
filename = "output.wav"
chunk = 2048#fs*5 # Record in chunks of 1024 samples
@lyleaf
lyleaf / geojson2bigquery.py
Created Oct 2, 2019
Convert geojson to get schema to upload to bigquery
View geojson2bigquery.py
import json
with open('HGURiau_EoF2019ATR2014Bappenas2011.json', 'r') as ifp:
with open('to_load.json', 'w') as ofp:
features = json.load(ifp)['features']
print(features)
# new-line-separated JSON
schema = None
for obj in features:
props = obj['properties'] # a dictionary
props['geometry'] = json.dumps(obj['geometry']) # make the geometry a string
@lyleaf
lyleaf / shape2geojson.py
Last active Oct 2, 2019
Create geojson from shape file
View shape2geojson.py
import shapefile
# read the shapefile
reader = shapefile.Reader("hello.shp")
fields = reader.fields[1:]
field_names = [field[0] for field in fields]
buffer = []
for sr in reader.shapeRecords():
atr = dict(zip(field_names, sr.record))
geom = sr.shape.__geo_interface__
@lyleaf
lyleaf / loop.js
Created Sep 30, 2019
Loop an array asynchronously with map
View loop.js
exports.getTranslations = functions.https.onRequest(async (req, res) => {
const english_words = req.body.english_words || [];
console.log(english_words);
const collectionRef = admin.firestore().collection("translations");
const createResponse = (res) => {
var data = {
english_word: (res === undefined) ? '' : res.english_word ,
translation: (res === undefined) ? '' : res.translation ,
transliteration: (res === undefined) ? '' : res.transliteration,
sound_link: (res === undefined) ? '' : res.sound_link
@lyleaf
lyleaf / 26. Remove Duplicates from Sorted Array.cpp
Last active Aug 24, 2016
sorted array去重只需要O(n) time, O(1) space
View 26. Remove Duplicates from Sorted Array.cpp
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.empty()) return 0;
int cur = 0;
for (int i=1;i<nums.size();i++){
if (nums[i] != nums[cur]){
swap(nums[i],nums[++cur]);
}
}
View 117. Populating Next Right Pointers in Each Node II.cpp
/**
我的方法用了一个queue,再用一个NULL指针来分辨是不是一层的。
看了答案以后发现optimized的解法是,不需要用queue,因为这个next的link已经可以serve as queue.
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
@lyleaf
lyleaf / 16. 3Sum Closest.cpp
Created Aug 23, 2016
一个vector里3个数加在一起离target最近的值。Two Pointers
View 16. 3Sum Closest.cpp
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int res = target + 1000;
sort(nums.begin(),nums.end());
for (int i=0;i<nums.size()-2;i++){
int target2 = target - nums[i];
int j=i+1;
int k=nums.size()-1;
while (j<k){