├── player ├── public │ ├── lib │ ├── pyodide │ ├── favicon.ico │ ├── robots.txt │ ├── mstile-150x150.png │ ├── apple-touch-icon.png │ ├── android-chrome-72x72.png │ ├── highlightWorker.js │ ├── manifest.json │ ├── safari-pinned-tab.svg │ ├── index.html │ ├── worklet.js │ └── worker.js ├── src │ ├── react-app-env.d.ts │ ├── index.css │ ├── setupTests.ts │ ├── App.test.tsx │ ├── reportWebVitals.ts │ ├── index.tsx │ ├── logo.svg │ ├── App.css │ └── App.tsx ├── craco.config.js ├── .gitignore ├── tailwind.config.js ├── tsconfig.json ├── package.json └── README.md ├── .gitignore ├── templates ├── pd │ ├── main.wasm │ └── main.js ├── wav │ └── main.wasm ├── chuck │ └── main.wasm ├── rtcmix │ └── main.wasm ├── vorbis │ └── main.wasm ├── csound │ └── libcsound.wasm └── python │ └── main.js ├── README.md ├── src ├── wav │ └── loader.c ├── vorbis │ └── loader.c └── pd │ ├── Makefile │ └── loader.c ├── bundle.py └── LICENSE.md /player/public/lib: -------------------------------------------------------------------------------- 1 | ../../lib -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | lib/* 2 | pyodide/* 3 | -------------------------------------------------------------------------------- /player/public/pyodide: -------------------------------------------------------------------------------- 1 | ../../pyodide -------------------------------------------------------------------------------- /player/src/react-app-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /player/src/index.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | -------------------------------------------------------------------------------- /templates/pd/main.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ijc8/alternator/HEAD/templates/pd/main.wasm -------------------------------------------------------------------------------- /templates/wav/main.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ijc8/alternator/HEAD/templates/wav/main.wasm -------------------------------------------------------------------------------- /player/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ijc8/alternator/HEAD/player/public/favicon.ico -------------------------------------------------------------------------------- /player/public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /templates/chuck/main.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ijc8/alternator/HEAD/templates/chuck/main.wasm -------------------------------------------------------------------------------- /templates/rtcmix/main.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ijc8/alternator/HEAD/templates/rtcmix/main.wasm -------------------------------------------------------------------------------- /templates/vorbis/main.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ijc8/alternator/HEAD/templates/vorbis/main.wasm -------------------------------------------------------------------------------- /templates/csound/libcsound.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ijc8/alternator/HEAD/templates/csound/libcsound.wasm -------------------------------------------------------------------------------- /player/public/mstile-150x150.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ijc8/alternator/HEAD/player/public/mstile-150x150.png -------------------------------------------------------------------------------- /player/public/apple-touch-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ijc8/alternator/HEAD/player/public/apple-touch-icon.png -------------------------------------------------------------------------------- /player/public/android-chrome-72x72.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ijc8/alternator/HEAD/player/public/android-chrome-72x72.png -------------------------------------------------------------------------------- /player/src/setupTests.ts: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom'; 6 | -------------------------------------------------------------------------------- /player/craco.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | babel: { 3 | plugins: ["@babel/plugin-proposal-logical-assignment-operators"], 4 | }, 5 | style: { 6 | postcss: { 7 | plugins: [ 8 | require('tailwindcss'), 9 | require('autoprefixer'), 10 | ], 11 | }, 12 | }, 13 | } 14 | -------------------------------------------------------------------------------- /player/public/highlightWorker.js: -------------------------------------------------------------------------------- 1 | importScripts("//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.4.0/highlight.min.js") 2 | 3 | self.onmessage = (event) => { 4 | console.log("Got", event.data) 5 | const result = hljs.highlightAuto(event.data) 6 | self.postMessage({ language: result.language, value: result.value }) 7 | } 8 | -------------------------------------------------------------------------------- /player/src/App.test.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { render, screen } from '@testing-library/react'; 3 | import App from './App'; 4 | 5 | test('renders learn react link', () => { 6 | render(); 7 | const linkElement = screen.getByText(/learn react/i); 8 | expect(linkElement).toBeInTheDocument(); 9 | }); 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Alternator: A General-Purpose Generative Music Player 2 | 3 | Demo: https://ijc8.me/alternator/ 4 | 5 | Paper: https://doi.org/10.5281/zenodo.6767436 6 | 7 | Video: https://www.youtube.com/watch?v=ceSlGrpMINA 8 | 9 | Related repos: 10 | - https://github.com/ijc8/demo-reel 11 | - https://github.com/ijc8/example-album 12 | -------------------------------------------------------------------------------- /player/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Alternator", 3 | "short_name": "Generative music player", 4 | "icons": [ 5 | { 6 | "src": "/android-chrome-72x72.png", 7 | "sizes": "72x72", 8 | "type": "image/png" 9 | } 10 | ], 11 | "theme_color": "#ffffff", 12 | "background_color": "#ffffff", 13 | "display": "standalone" 14 | } 15 | -------------------------------------------------------------------------------- /player/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /src/wav/loader.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define DR_WAV_IMPLEMENTATION 4 | #include "dr_wav.h" 5 | 6 | drwav wav; 7 | 8 | EMSCRIPTEN_KEEPALIVE 9 | void setup(int sample_rate) { 10 | drwav_init_file(&wav, "main.wav", NULL); 11 | } 12 | 13 | EMSCRIPTEN_KEEPALIVE 14 | int process(float *output, int length) { 15 | drwav_uint64 read = drwav_read_pcm_frames_f32(&wav, length, output); 16 | return read; 17 | } 18 | -------------------------------------------------------------------------------- /player/tailwind.config.js: -------------------------------------------------------------------------------- 1 | const colors = require('tailwindcss/colors') 2 | 3 | module.exports = { 4 | mode: 'jit', 5 | purge: ['./src/**/*.{js,jsx,ts,tsx}', './public/index.html'], 6 | theme: { 7 | extend: { 8 | backgroundImage: { 9 | "gradient-split": `linear-gradient(to right, ${colors.gray['500']} 50%, rgba(255, 255, 255, 0) 50%)`, 10 | }, 11 | }, 12 | colors, 13 | }, 14 | plugins: [], 15 | } 16 | -------------------------------------------------------------------------------- /src/vorbis/loader.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "stb_vorbis.c" 4 | 5 | stb_vorbis *vorbis; 6 | 7 | EMSCRIPTEN_KEEPALIVE 8 | void setup(int sample_rate) { 9 | // TODO: Resample as needed. 10 | int error; 11 | vorbis = stb_vorbis_open_filename("main.ogg", &error, NULL); 12 | } 13 | 14 | EMSCRIPTEN_KEEPALIVE 15 | int process(float *output, int length) { 16 | return stb_vorbis_get_samples_float_interleaved(vorbis, 2, output, length); 17 | } 18 | -------------------------------------------------------------------------------- /src/pd/Makefile: -------------------------------------------------------------------------------- 1 | LIBPD_DIR = ../../../ 2 | 3 | SRC_FILES = loader.c 4 | TARGET = loader.js 5 | 6 | CFLAGS = -I$(LIBPD_DIR)/pure-data/src -I$(LIBPD_DIR)/libpd_wrapper -O3 7 | LDFLAGS = -L$(LIBPD_DIR)/build/libs -lpd -lm 8 | 9 | .PHONY: clean clobber 10 | 11 | $(TARGET): $(SRC_FILES) 12 | emcc $(CFLAGS) -o $(TARGET) $(SRC_FILES) --closure 1 -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s FORCE_FILESYSTEM=1 $(LDFLAGS) -s EXPORTED_FUNCTIONS=["_malloc","_free"] 13 | 14 | clean: 15 | rm $(TARGET) $(TARGET:.js=.wasm) 16 | -------------------------------------------------------------------------------- /player/src/reportWebVitals.ts: -------------------------------------------------------------------------------- 1 | import { ReportHandler } from 'web-vitals'; 2 | 3 | const reportWebVitals = (onPerfEntry?: ReportHandler) => { 4 | if (onPerfEntry && onPerfEntry instanceof Function) { 5 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { 6 | getCLS(onPerfEntry); 7 | getFID(onPerfEntry); 8 | getFCP(onPerfEntry); 9 | getLCP(onPerfEntry); 10 | getTTFB(onPerfEntry); 11 | }); 12 | } 13 | }; 14 | 15 | export default reportWebVitals; 16 | -------------------------------------------------------------------------------- /player/src/index.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import reportWebVitals from './reportWebVitals'; 6 | 7 | ReactDOM.render( 8 | 9 | 10 | , 11 | document.getElementById('root') 12 | ); 13 | 14 | // If you want to start measuring performance in your app, pass a function 15 | // to log results (for example: reportWebVitals(console.log)) 16 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals 17 | reportWebVitals(); 18 | -------------------------------------------------------------------------------- /player/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "dom", 6 | "dom.iterable", 7 | "esnext" 8 | ], 9 | "allowJs": true, 10 | "skipLibCheck": true, 11 | "esModuleInterop": true, 12 | "allowSyntheticDefaultImports": true, 13 | "strict": true, 14 | "forceConsistentCasingInFileNames": true, 15 | "noFallthroughCasesInSwitch": true, 16 | "module": "esnext", 17 | "moduleResolution": "node", 18 | "resolveJsonModule": true, 19 | "isolatedModules": true, 20 | "noEmit": true, 21 | "jsx": "react-jsx" 22 | }, 23 | "include": [ 24 | "src" 25 | ] 26 | } 27 | -------------------------------------------------------------------------------- /src/pd/loader.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include "z_libpd.h" 5 | 6 | int done; 7 | 8 | void finish(const char *s) { 9 | done = 1; 10 | } 11 | 12 | EMSCRIPTEN_KEEPALIVE 13 | void setup(int sample_rate) { 14 | done = 0; 15 | 16 | // initialize libpd 17 | libpd_init(); 18 | libpd_init_audio(0, 1, sample_rate); 19 | 20 | libpd_bind("finish"); 21 | libpd_set_banghook(finish); 22 | 23 | // compute audio [; pd dsp 1( 24 | libpd_start_message(1); // one entry in list 25 | libpd_add_float(1.0f); 26 | libpd_finish_message("pd", "dsp"); 27 | 28 | // open patch [; pd open file folder( 29 | libpd_openfile("main.pd", "."); 30 | } 31 | 32 | EMSCRIPTEN_KEEPALIVE 33 | int process(float *output, int length) { 34 | if (done) { 35 | return 0; 36 | } 37 | // Assumes length is a multiple of libpd_blocksize(). 38 | int blocksize = libpd_blocksize(); 39 | int i; 40 | for (i = 0; i < length; i += blocksize) { 41 | libpd_process_float(1, NULL, &output[i]); 42 | } 43 | if (i > length) { 44 | fprintf(stderr, "buffer overflow: %d > %d\n", i, length); 45 | } 46 | return length; 47 | } 48 | -------------------------------------------------------------------------------- /player/public/safari-pinned-tab.svg: -------------------------------------------------------------------------------- 1 | 2 | 4 | 7 | 8 | Created by potrace 1.14, written by Peter Selinger 2001-2017 9 | 10 | 12 | 19 | 21 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /player/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "player", 3 | "version": "0.1.0", 4 | "private": true, 5 | "homepage": ".", 6 | "dependencies": { 7 | "@craco/craco": "^6.3.0", 8 | "@headlessui/react": "^1.4.1", 9 | "@testing-library/jest-dom": "^5.14.1", 10 | "@testing-library/react": "^11.2.7", 11 | "@testing-library/user-event": "^12.8.3", 12 | "@types/jest": "^26.0.24", 13 | "@types/node": "^12.20.27", 14 | "@types/react": "^17.0.24", 15 | "@types/react-dom": "^17.0.9", 16 | "magic-bytes.js": "^1.0.6", 17 | "react": "^17.0.2", 18 | "react-dom": "^17.0.2", 19 | "react-icons": "^4.4.0", 20 | "react-scripts": "4.0.3", 21 | "typescript": "^4.4.3", 22 | "web-vitals": "^1.1.2" 23 | }, 24 | "scripts": { 25 | "start": "TAILWIND_MODE=watch craco start", 26 | "build": "craco build", 27 | "test": "craco test", 28 | "eject": "react-scripts eject" 29 | }, 30 | "eslintConfig": { 31 | "extends": [ 32 | "react-app", 33 | "react-app/jest" 34 | ] 35 | }, 36 | "browserslist": { 37 | "production": [ 38 | ">0.2%", 39 | "not dead", 40 | "not op_mini all" 41 | ], 42 | "development": [ 43 | "last 1 chrome version", 44 | "last 1 firefox version", 45 | "last 1 safari version" 46 | ] 47 | }, 48 | "devDependencies": { 49 | "autoprefixer": "^9.8.7", 50 | "postcss": "^7.0.38", 51 | "tailwindcss": "npm:@tailwindcss/postcss7-compat@^2.2.16" 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /templates/python/main.js: -------------------------------------------------------------------------------- 1 | importScripts('pyodide/pyodide.js') 2 | 3 | async function setupPyodide() { 4 | self.pyodide = await loadPyodide({ indexURL: 'pyodide/' }) 5 | const metadata = await (await fetch(self.path + "track.json")).json() 6 | const blob = await (await fetch(self.path + "bundle.data")).blob() 7 | self.pyodide.FS.mkdir("alternator") 8 | self.pyodide.FS.mount(self.pyodide.FS.filesystems.WORKERFS, { 9 | packages: [{ metadata, blob }] 10 | }, "/alternator") 11 | // Aleatora setup 12 | self.pyodide.runPython(setupCode) 13 | await self.pyodide.loadPackagesFromImports("import micropip") 14 | await self.pyodide.runPythonAsync( 15 | 'import asyncio, micropip\n\ 16 | await asyncio.wait([micropip.install(f"lib/{name}.whl") for name in \ 17 | ["oscpy-0.6.0-py2.py3-none-any", "mido-1.2.10-py2.py3-none-any", "sounddevice-0.4.2-py3-none-any"]])\n\ 18 | await micropip.install("lib/aleatora-0.2.0a0-py3-none-any.whl")') 19 | } 20 | 21 | const setupPyodidePromise = setupPyodide() 22 | 23 | const setupCode = ` 24 | import sys 25 | 26 | # HACK: Shim "sounddevice" for aleatora. 27 | import sys 28 | import types 29 | sounddevice = types.ModuleType("sounddevice") 30 | sounddevice.query_devices = lambda: print("* 0 Audio Worklet Output Buffer") 31 | sys.modules["sounddevice"] = sounddevice 32 | ` 33 | 34 | let audioCallback 35 | 36 | async function setup(sampleRate) { 37 | await setupPyodidePromise 38 | self.pyodide.runPython(` 39 | import os 40 | os.chdir("/alternator") 41 | import sys 42 | sys.path.append(".") 43 | import aleatora.streams.audio 44 | aleatora.streams.audio.SAMPLE_RATE = ${sampleRate} 45 | from main import main`) 46 | audioCallback = self.pyodide.runPython(` 47 | print("Playing:", main) 48 | samples = iter(main) 49 | def callback(outdata, frames): 50 | i = -1 51 | for i, sample in zip(range(frames), samples): 52 | outdata[i] = sample 53 | return i + 1 54 | callback 55 | `) 56 | console.log("Setup done") 57 | } 58 | 59 | function process(output) { 60 | return audioCallback(output, output.length) 61 | } 62 | -------------------------------------------------------------------------------- /player/README.md: -------------------------------------------------------------------------------- 1 | # Getting Started with Create React App 2 | 3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 4 | 5 | ## Available Scripts 6 | 7 | In the project directory, you can run: 8 | 9 | ### `npm start` 10 | 11 | Runs the app in the development mode.\ 12 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 13 | 14 | The page will reload if you make edits.\ 15 | You will also see any lint errors in the console. 16 | 17 | ### `npm test` 18 | 19 | Launches the test runner in the interactive watch mode.\ 20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 21 | 22 | ### `npm run build` 23 | 24 | Builds the app for production to the `build` folder.\ 25 | It correctly bundles React in production mode and optimizes the build for the best performance. 26 | 27 | The build is minified and the filenames include the hashes.\ 28 | Your app is ready to be deployed! 29 | 30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 31 | 32 | ### `npm run eject` 33 | 34 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 35 | 36 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 37 | 38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 39 | 40 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 41 | 42 | ## Learn More 43 | 44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 45 | 46 | To learn React, check out the [React documentation](https://reactjs.org/). 47 | -------------------------------------------------------------------------------- /player/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 14 | 15 | 16 | 17 | 21 | 22 | 31 | 32 | 33 | 34 | 35 | 36 | Alternator 37 | 38 | 39 | 40 |
41 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /player/src/logo.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /player/public/worklet.js: -------------------------------------------------------------------------------- 1 | console.log("AudioWorklet: start") 2 | 3 | class DoubleBufferProcessor extends AudioWorkletProcessor { 4 | constructor(options) { 5 | console.log("AudioWorklet: constructor") 6 | super() 7 | this.numFrames = options.processorOptions.numFrames 8 | this.currentBuffer = new Float32Array(this.numFrames * options.outputChannelCount[0]) 9 | this.nextBuffer = new Float32Array(this.numFrames * options.outputChannelCount[0]) 10 | this.frameIndex = 0 11 | this.underrun = false 12 | 13 | this.port.onmessage = (e) => { 14 | this.statusPort = e.ports[0] 15 | this.port.onmessage = (e) => { 16 | this.nextBuffer = e.data 17 | this.nextBufferReady = true 18 | } 19 | } 20 | 21 | this.swapBuffers() 22 | } 23 | 24 | swapBuffers() { 25 | [this.currentBuffer, this.nextBuffer] = [this.nextBuffer, this.currentBuffer] 26 | this.frameIndex = 0 27 | this.nextBufferReady = false 28 | // Send next buffer for the Web Worker to fill. 29 | this.port.postMessage(this.nextBuffer, [this.nextBuffer.buffer]) 30 | } 31 | 32 | process(inputs, outputs, parameters) { 33 | const channels = outputs[0] 34 | if (this.frameIndex >= this.numFrames) { 35 | // Currently in underrun. 36 | if (!this.underrun) { 37 | // Signal the start of an underrun. 38 | this.statusPort.postMessage(true) 39 | this.underrun = true 40 | } 41 | for (const channel of channels) { 42 | channel.fill(0) 43 | } 44 | } else { 45 | if (this.underrun) { 46 | // Signal the end of an underrun. 47 | this.statusPort.postMessage(false) 48 | this.underrun = false 49 | } 50 | // TODO: Maybe deinterleave in WebAssembly? 51 | const numChannels = channels.length 52 | const numFrames = channels[0].length 53 | for (let f = 0; f < numFrames; f++) { 54 | const start = (this.frameIndex + f) * numChannels 55 | for (let c = 0; c < numChannels; c++) { 56 | channels[c][f] = this.currentBuffer[start + c] 57 | } 58 | } 59 | } 60 | // NOTE: This assumes this.frameSize is a multiple of the channels[i].length (128). 61 | this.frameIndex += channels[0].length 62 | if (this.frameIndex >= this.numFrames && this.nextBufferReady) { 63 | this.swapBuffers() 64 | } 65 | return true 66 | } 67 | } 68 | 69 | registerProcessor("doublebuffer", DoubleBufferProcessor) 70 | -------------------------------------------------------------------------------- /bundle.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import json 3 | import os 4 | import shutil 5 | import sys 6 | 7 | if len(sys.argv) < 3: 8 | name = os.path.basename(sys.argv[0]) 9 | exit(f""" 10 | {name} is a utility to bundle up your composition for Alternator. 11 | 12 | Usage: {name}