Skip to content

Instantly share code, notes, and snippets.

View Apiko-tutorials's full-sized avatar

Code Tutorials [by Apiko] Apiko-tutorials

View GitHub Profile
const thread = spawn(function (input, done) {
// get all code from localpackage
const { calculateWavSpectrum } = require('fft-thread-worker');
calculateWavSpectrum(input.filePath, done);
...
})
...
const spawn = require('threads').spawn;
const { decode } = require('./src/utils/decoder');
const { fft, spliceSpectrum } = require('./src/utils/fft');
const decodeFiles = async (inFolder, outFolder) => {
// 1 read all file inside of in folder
const files = await getFilesInFolder(inFolder);
for (let i = 0; i < files.length; i++) {
const thread = spawn(function (input, done) {
const decodeFiles = async (inFolder, outFolder) => {
// 1 read all file inside of in folder
const files = await getFilesInFolder(inFolder);
for(let i=0; i < files.length; i++) {
// 2 decode .wav to float array and get spectrum by fff
decode(files[i].filePath).then(audioData => {
const { spectrum } = fft(audioData.channelData[0]);
const { splicedSpectrum } = spliceSpectrum(spectrum);
// 3 write result ito out folder
const config = {
rate: 44100,
channels: 2,
device: `plughw:${process.argv[2] || 0}`,
fileType: 'wav',
};
const micInstance = mic(config);
const stream = micInstance.getAudioStream();
import { Tracks } from '../../db';
export const createTrack = (_, { input }) =>
Tracks.create({
...input,
hypeCoefficient: 0,
rating: 0,
});
# In root.graphql
type Mutation {
createTrack(input: TrackInput!): Track
}
# In records.graphql
input TrackInput {
name: String!
genres: [Genre]!
releasedAt: Date!
// In app.js add context option to express middleware
graphqlExpress({ schema, context: { ...createLoaders() } }),
// Access dataloaders from context in resolvers
export const Album = {
tracks: ({ tracks }, args, { tracksLoader }) => tracksLoader.loadMany(tracks),
titleTrack: ({ titleTrack }, args, { tracksLoader }) => tracksLoader.load(titleTrack),
artist: ({ artist }, args, { artistsLoader }) => artistsLoader.load(artist),
};
import DataLoader from 'dataloader';
import { Artists, Tracks } from '../../db';
// Use toString to compare MongoID objects
const idsEq = k => d => d._id.toString() === k.toString();
// Make sure that the docs are in the same order as keys and have null value if nothing found
const mapLoaderRes = keys => data =>
keys.map(k => data.find(idsEq(k)) || null);
export const ArtistType = {
__resolveType: ({ type }) => {
if (type === 'SINGLE') return 'Single';
if (type === 'BAND') return 'Band';
if (type === 'BAND_PARTICIPANT') return 'BandParticipant';
return null;
},
};
export const BandParticipant = {
export const Artist = {
tracks: ({ _id }, args) => Tracks.find(...tracksQuery({ ...args, artists: [_id] })),
albums: ({ _id }) => Albums.find({ artist: _id }),
artistType: ({ artistType, _id }) => ({ ...artistType, _id }),
};