Last active
April 25, 2017 13:26
-
-
Save gaziya/e5d40b0df1a65510af81b124c4ecf4cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from OpenGL.GL import *\n", | |
"from OpenGL.WGL import *\n", | |
"from ctypes import *\n", | |
"import numpy\n", | |
"import pyaudio\n", | |
"import wave\n", | |
"import array" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"def shader2data(duration, src): # 出力時間、shader music ソース\n", | |
" sampleRate = 48000\n", | |
" numSamples = duration*sampleRate\n", | |
" numSamplesC = numSamples*2\n", | |
" samples = (c_float * numSamplesC)()\n", | |
" hWnd = windll.user32.CreateWindowExA(0,0xC018,0,0,0,0,0,0,0,0,0,0)\n", | |
" hDC = windll.user32.GetDC(hWnd)\n", | |
" pfd = PIXELFORMATDESCRIPTOR(0,1,0,32,0,0,0,0,0,0,0,0,0,0,0,0,0,32,0,0,0,0,0,0,0)\n", | |
" SetPixelFormat(hDC,ChoosePixelFormat(hDC, pfd), pfd)\n", | |
" hGLrc = wglCreateContext(hDC)\n", | |
" wglMakeCurrent(hDC, hGLrc)\n", | |
" program = glCreateProgram()\n", | |
" shader = glCreateShader(GL_VERTEX_SHADER)\n", | |
" glShaderSource(shader, src)\n", | |
" glCompileShader(shader)\n", | |
" if glGetShaderiv(shader, GL_COMPILE_STATUS) != GL_TRUE:\n", | |
" raise RuntimeError(glGetShaderInfoLog(shader).decode())\n", | |
" glAttachShader(program, shader)\n", | |
" outs = cast((c_char_p*1)(b\"gain\"), POINTER(POINTER(c_char)))\n", | |
" glTransformFeedbackVaryings(program, 1, outs, GL_INTERLEAVED_ATTRIBS)\n", | |
" glLinkProgram(program)\n", | |
" glUseProgram(program)\n", | |
" vbo = glGenBuffers(1)\n", | |
" glBindBuffer(GL_ARRAY_BUFFER, vbo)\n", | |
" glBufferData(GL_ARRAY_BUFFER, sizeof(samples), None, GL_STATIC_DRAW)\n", | |
" glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, vbo)\n", | |
" glUniform1f(glGetUniformLocation(program, \"sampleRate\"), sampleRate)\n", | |
" glEnable(GL_RASTERIZER_DISCARD)\n", | |
" glBeginTransformFeedback(GL_POINTS)\n", | |
" glDrawArrays(GL_POINTS, 0, numSamples)\n", | |
" glEndTransformFeedback()\n", | |
" glDisable(GL_RASTERIZER_DISCARD)\n", | |
" glGetBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(samples), byref(samples))\n", | |
" wglMakeCurrent(0, 0);\n", | |
" wglDeleteContext(hGLrc);\n", | |
" windll.user32.ReleaseDC(hWnd, hDC);\n", | |
" windll.user32.PostQuitMessage(0);\n", | |
" return numpy.frombuffer(samples, dtype=numpy.float32)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def writeWav(filename, data): \n", | |
" sampleRate = 48000\n", | |
" w = wave.Wave_write(filename)\n", | |
" w.setnchannels(2)\n", | |
" w.setsampwidth(2)\n", | |
" w.setframerate(sampleRate)\n", | |
" w.writeframes(array.array('h', data*32767).tobytes())\n", | |
" w.close()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def readWav(filename): \n", | |
" w = wave.open(filename, 'rb')\n", | |
" print(\"params :\", w.getparams())\n", | |
" print(\"duration:\", w.getnframes() / w.getframerate(), \"sec\")\n", | |
" p = pyaudio.PyAudio()\n", | |
" stream = p.open(format=p.get_format_from_width(w.getsampwidth()),\n", | |
" channels=w.getnchannels(),\n", | |
" rate=w.getframerate(),\n", | |
" output=True)\n", | |
" stream.write(w.readframes(w.getnframes()))\n", | |
" stream.stop_stream()\n", | |
" stream.close()\n", | |
" p.terminate()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def playSound(data): \n", | |
" sampleRate = 48000\n", | |
" p = pyaudio.PyAudio()\n", | |
" stream = p.open(rate=sampleRate, channels=2, format=pyaudio.paFloat32, output=True)\n", | |
" stream.write(array.array('f', data).tobytes())\n", | |
" stream.close()\n", | |
" p.terminate()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"data = shader2data(2, \"\"\"\n", | |
"#version 300 es\n", | |
"\n", | |
"out vec2 gain; \n", | |
"uniform float sampleRate; \n", | |
"\n", | |
"vec2 mainSound( float time )\n", | |
"{\n", | |
" return vec2(sin(6.2831*440.0*time)); ;\n", | |
"}\n", | |
"\n", | |
"void main() {\n", | |
" float time = float(gl_VertexID) / sampleRate;\n", | |
" gain =mainSound(time); \n", | |
"}\n", | |
"\"\"\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"filename = \"output.wav\"\n", | |
"writeWav(filename, data)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"params : _wave_params(nchannels=2, sampwidth=2, framerate=48000, nframes=96000, comptype='NONE', compname='not compressed')\n", | |
"duration: 2.0 sec\n" | |
] | |
} | |
], | |
"source": [ | |
"filename = \"output.wav\"\n", | |
"readWav(filename)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"playSound(data)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"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.4.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment