Skip to content

Instantly share code, notes, and snippets.

View speratus's full-sized avatar
🚀
Always

Andrew Luchuk speratus

🚀
Always
View GitHub Profile
@speratus
speratus / processor.js
Last active April 26, 2023 21:13
The AudioWorkletProcessor for my Web Synth Prototype
class WebSynthProcessor extends AudioWorkletProcessor {
constructor(options) {
super(options);
this.cycler = 0;
this.transport = 0;
this.port.onmessage = event => this.onmessage(event.data);
console.log(sampleRate);
}
@speratus
speratus / index.js
Last active April 26, 2023 21:09
The Main JS file for my Web Synth Prototype
let startBtn = document.getElementById('start-sound');
async function setup() {
const bin = await WebAssembly.compileStreaming(await fetch('/www/wasm_demo.wasm'));
// const buf = await bin.arrayBuffer();
let context = new AudioContext();
console.log("adding module");
await context.audioWorklet.addModule('processor.js');
let node = new AudioWorkletNode(context, 'web-synth-proto');
@speratus
speratus / commands.sh
Created May 29, 2020 13:46
An example of a systemd service for starting and stopping a postgres server installed from Homebrew.
#!/bin/bash
# Run these commands after creating your service file to get it installed and running.
# Once the service file is finished, copy it to the correct directory.
# In Ubuntu based systems, this is usually /etc/systemd/system.
sudo cp postgres.service /etc/systemd/service
# Now try starting the service:
sudo systemctl start postgres.service
@speratus
speratus / AwesomePopup.js
Created February 11, 2020 03:29
An example of a semantic-ui-react search component.
//Import all the things
class AwesomePopup extends React.Component {
state = {
results: [],
value: ''
}
onSearchChange = (e, {value}) => {
@speratus
speratus / uploading form.html
Last active January 20, 2020 19:09
A form that could be used in uploading a file to an API
<form id='form'>
<label for="name">Name:</label>
<input type="text" name="name" placeholder="Please enter a name">
<label for='avatar'>Image:</label>
<input type="file" name="avatar">
<input type="submit" value="Submit new User">
</form>
@speratus
speratus / rails controller to handle file uploads.rb
Created January 20, 2020 16:16
A controller to handle an uploaded file and pass it to ActiveStorage correctly.
def create
#If you set up your frontend request properly, params[:avatar] will be an UploadedFile instance, which is what we want
user = User.new(name: params[:name], avatar: params[:avatar])
#passing the file in to the constructor causes ActiveStorage to attach the file automatically.
if user.save
puts "The user saved successfully"
render json: user
else
puts "The user failed to save"
render json: {message: "Error"}
@speratus
speratus / final ajax request to send image.js
Created January 20, 2020 16:03
The correct way to send a file to a server via a backend server via AJAX
document.querySelector("#form").addEventListener(e => {
e.preventDefault()
//Get the file from the file input type
const file = e.target['avatar'].files[0]
const name = e.target['name'].value
const formData = new FormData()
//append the file directly to formData. It's read in automatically
formData.append('avatar', file)
formData.append('name', name)
def create
user = User.new(name: params[:name])
user.avatar.attach(params[:avatar])
if user.save
puts "The user saved successfully"
render json: user
else
puts "The user failed to save"
render json: {message: "Error"}
end
@speratus
speratus / Basic Model with ActiveStorage attachment.rb
Created January 20, 2020 15:24
A very basic model to demonstrate ActiveStorage attachments
class User < ApplicationRecord
has_one_attached :avatar
end
@speratus
speratus / base64 data upload via ajax.js
Created January 20, 2020 14:54
The form event listener to upload an image to a server in base64
document.querySelector("#form").addEventListener('submit', (e) => {
e.preventDefault()
const file = e.target['file'].files[0]
const reader = new FileReader();
reader.addEventListener('load', (e) => {
console.log(e)
console.log('The data is', e.target.result)
fetch("http://localhost:3000/images", {