Skip to content

Instantly share code, notes, and snippets.

View lyleaf's full-sized avatar
💭
I may be slow to respond.

Liu Yiling lyleaf

💭
I may be slow to respond.
  • Google
  • Australia
View GitHub Profile
@lyleaf
lyleaf / tempo_animation_prototype.py
Created January 7, 2020 00:41
Create animation which is synced up to the music tempo.
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)
@lyleaf
lyleaf / stream_beat_v2.py
Created November 10, 2019 23:37
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()
@lyleaf
lyleaf / stream_beat.py
Created November 10, 2019 07:15
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 November 10, 2019 07:14
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 October 2, 2019 06:10
Convert geojson to get schema to upload to bigquery
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 October 2, 2019 06:14
Create geojson from shape file
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 September 30, 2019 03:49
Loop an array asynchronously with map
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 August 24, 2016 18:20
sorted array去重只需要O(n) time, O(1) space
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]);
}
}
/**
我的方法用了一个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 August 23, 2016 11:52
一个vector里3个数加在一起离target最近的值。Two Pointers
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){