Commit 80ef7608 authored by Robozman's avatar Robozman

Merge branch 'master' of gitlab.com:ColoradoSchoolOfMines/pyvideomusicifier into HEAD

parents 738ba039 d07c19ed
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[]\n",
"Beat 0.04159283638000488\n",
"[877 961]\n",
"Beat 0.50146484375\n",
"[884 954]\n",
"[883 901 937 955]\n",
"[ 790 827 845 864 882 884 901 937 954 956 974 993 1011 1048]\n",
"Beat 1.0590598583221436\n",
"[ 826 846 849 863 882 900 938 956 975 989 992 1012]\n",
"[ 826 857 882 884 954 956 981 1012]\n",
"[ 780 835 863 881 883 887 890 946 949 953 955 973 1001 1056]\n",
"Beat 1.5921337604522705\n",
"[ 765 826 857 863 883 887 949 953 973 979 1010 1071]\n",
"[888 950]\n",
"[846 883 888 919 950 955 992]\n",
"[845 881 955 991]\n",
"Beat 2.3201138973236084\n",
"[881 955]\n",
"[ 771 816 836 877 881 897 939 955 959 1000 1020 1065]\n",
"[877 882 897 939 954 959]\n",
"Beat 2.8561689853668213\n",
"[845 881 897 939 955 991]\n",
"[878 882 898 940 956 960]\n",
"[882 897 939 954]\n",
"Beat 3.3609108924865723\n",
"[ 751 834 876 882 954 960 1002 1085]\n",
"[878 960]\n",
"[881 955]\n",
"Beat 4.013133764266968\n",
"[846 882 956 992]\n",
"[876 881 897 939 955 960]\n",
"[876 897 939 960]\n",
"Beat 4.552753925323486\n",
"[840 888 950 998]\n",
"Beat 5.647111892700195\n",
"[863 890 946 973]\n",
"Beat 5.792519807815552\n",
"[]\n",
"[883 900 936 953]\n",
"Beat 6.171686887741089\n",
"[905 931]\n",
"[ 836 877 882 897 900 936 939 954 959 1000]\n",
"Beat 6.614023923873901\n",
"[ 753 836 864 883 891 898 940 947 955 974 1002 1085]\n",
"Beat 6.872577667236328\n",
"[ 752 771 789 826 863 881 900 936 955 973 1010 1047 1065 1084]\n",
"[ 814 884 901 937 954 1024]\n",
"Beat 7.456484794616699\n",
"[ 752 807 835 863 883 890 900 936 946 953 973 1001 1029 1084]\n",
"[ 742 777 825 835 862 883 887 949 953 974 1001 1011 1059 1094]\n",
"Beat 7.985226631164551\n",
"[849 882 884 888 950 954 956 989]\n",
"[857 884 888 950 954 981]\n",
"Beat 8.542191743850708\n",
"[ 771 845 877 881 887 897 939 949 955 959 991 1065]\n",
"[880 882 897 939 954 956]\n",
"Beat 9.063288688659668\n",
"[882 898 940 956]\n",
"[ 822 836 849 863 877 881 891 897 904 932 939 945 955 959 973\n",
" 987 1000 1014]\n",
"Beat 9.564114809036255\n",
"[ 745 780 815 884 954 1023 1058 1093]\n",
"[884 954]\n",
"Beat 10.157659769058228\n",
"[881 895 941 955]\n",
"[ 825 856 881 955 980 1011]\n",
"Beat 10.645607948303223\n",
"[888 950]\n"
]
},
{
"ename": "AttributeError",
"evalue": "'NoneType' object has no attribute 'attributes'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-2-a69785fce7a6>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 111\u001b[0m \u001b[0mfig\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcanvas\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdraw\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 112\u001b[0m \u001b[0mfig\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcanvas\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mflush_events\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 113\u001b[0;31m \u001b[0mfig\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m/Users/jaredlincenberg/anaconda3/lib/python3.6/site-packages/matplotlib/figure.py\u001b[0m in \u001b[0;36mshow\u001b[0;34m(self, warn)\u001b[0m\n\u001b[1;32m 435\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mmanager\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 436\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 437\u001b[0;31m \u001b[0mmanager\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 438\u001b[0m \u001b[0;32mreturn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 439\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mNonGuiException\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/Users/jaredlincenberg/anaconda3/lib/python3.6/site-packages/matplotlib/backends/_backend_tk.py\u001b[0m in \u001b[0;36mshow\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 566\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcanvas\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdraw_idle\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 567\u001b[0m \u001b[0;31m# Raise the new window.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 568\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcanvas\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmanager\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwindow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mattributes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'-topmost'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 569\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcanvas\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmanager\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwindow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mattributes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'-topmost'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 570\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_shown\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'attributes'"
]
}
],
"source": [
"import matplotlib\n",
"matplotlib.use('TkAgg')\n",
"import time\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import scipy.io.wavfile as wavfile\n",
"from scipy import signal\n",
"import sounddevice\n",
"import time\n",
"import math\n",
"import msaf\n",
"import librosa\n",
"import sys\n",
"from functools import lru_cache\n",
"import pickle\n",
"\n",
"sys.argv[1]=\"violin.wav\"\n",
"\n",
"load_from_file = False\n",
"file_name = None\n",
"wave_type = 0\n",
"fs=44100\n",
"\n",
"\n",
"def mkBounds(name):\n",
" try:\n",
" f = open(name + \".cache\", \"rb\")\n",
" return pickle.load(f)\n",
" except:\n",
" of = open(name + \".cache\", \"wb\")\n",
" result = msaf.process(name)\n",
" pickle.dump(result, of)\n",
" return result\n",
"\n",
"\n",
"def mkBeat(name):\n",
" try:\n",
" f = open(name + \".beat\", \"rb\")\n",
" return pickle.load(f)\n",
" except:\n",
" of = open(name + \".beat\", \"wb\")\n",
" audio = librosa.load(name, sr=None)[0]\n",
" audio_harmonic, audio_percussive = librosa.effects.hpss(audio)\n",
" tempo, frames = librosa.beat.beat_track(y=audio_harmonic,\n",
" sr=fs, hop_length=1024)\n",
" result = librosa.frames_to_time(frames, sr=fs,\n",
" hop_length=1024)\n",
" pickle.dump(result, of)\n",
" return result\n",
"\n",
"if len(sys.argv) == 1:\n",
" print(\"Provide a file name or an integer as an argument\")\n",
" exit()\n",
"else:\n",
" try:\n",
" wave_type = int(sys.argv[1])\n",
" except:\n",
" load_from_file = True\n",
" file_name = sys.argv[1]\n",
"\n",
"if load_from_file == False:\n",
" wav_data, rate = generate_waves.generate_waves(wave_type, 52)\n",
"elif load_from_file == True:\n",
" #rate, wav_data = wavfile.read(sys.argv[1], mmap=True)\n",
" # wav_data = samples\n",
" wav_data, rate = getWave(fileName=sys.argv[1])\n",
" boundaries, labels = mkBounds(sys.argv[1])\n",
" beat_times = mkBeat(sys.argv[1])\n",
"\n",
"plt.ion()\n",
"fig = plt.figure(1)\n",
"fft_plot = fig.add_subplot(111)\n",
"\n",
"sounddevice.play(wav_data, rate)\n",
"\n",
"timestep = 1/24\n",
"t = time.time()\n",
"origin = time.time()\n",
"nextBeat = 0\n",
"while t - origin < len(wav_data) / rate:\n",
" if load_from_file == True:\n",
" if time.time() - origin >= beat_times[nextBeat]:\n",
" nextBeat += 1\n",
" print(\"Beat \",t - origin)\n",
"\n",
" while (time.time() - t < timestep):\n",
" pass\n",
"\n",
" t = time.time()\n",
"\n",
" second = wav_data[math.floor((t - origin) * rate) : math.floor((t - origin + timestep) * rate)]\n",
"\n",
" # print(second)\n",
"\n",
" transform = np.abs(np.fft.fftshift(np.fft.fft(second[:])))\n",
"\n",
" peaks, _ = signal.find_peaks(transform, prominence=20)\n",
" print(peaks)\n",
"\n",
" fft_plot.clear()\n",
" if load_from_file == True:\n",
" fft_plot.plot(transform) # np.abs(np.fft.fftshift(np.fft.fft(second[:])))\n",
" else:\n",
" fft_plot.plot(transform) # np.abs(np.fft.fftshift(np.fft.fft(second[:])))\n",
"\n",
" for x in peaks:\n",
" fft_plot.plot(x,transform[x], marker='x')\n",
"\n",
" fft_plot.set_ylim([0,300])\n",
"\n",
" fig.canvas.draw()\n",
" fig.canvas.flush_events()\n",
" fig.show()"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/jaredlincenberg/anaconda3/lib/python3.6/site-packages/numba/errors.py:104: UserWarning: Insufficiently recent colorama version found. Numba requires colorama >= 0.3.9\n",
" warnings.warn(msg)\n"
]
}
],
"source": [
"import time\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import scipy.io.wavfile as wavfile\n",
"import sounddevice\n",
"import time\n",
"import librosa\n",
"\n",
"#getWave() : Asks for filename\n",
"#getWave(fileName) : Passes a file name\n",
"#getWave(fileName,b=0) : Asks for a range of times may not be working right.\n",
"\n",
"def getWave(fileName=None,b=None,sample_rate=44100):\n",
" if b is not None:\n",
" fileName=input(\"File Name: \")\n",
" start=input(\"Start time: \")\n",
" end=input(\"End time: \")\n",
" sample_rate=input(\"Sample rate: \")\n",
" samples, sample_rate =librosa.load(fileName,offset=float(start),duration=float(end)-float(start), sr=int(sample_rate))\n",
" elif fileName is not None:\n",
" samples, sample_rate =librosa.load(fileName,sr=int(sample_rate))\n",
" else:\n",
" fileName=input(\"File Name: \")\n",
" samples, sample_rate =librosa.load(fileName, sr=int(sample_rate))\n",
" sounddevice.play(y,sr)\n",
" #sounddevice.play(y,sr)\n",
" return samples, sample_rate\n",
" #Comment"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment