# @osbjs/spectrum-tiny-osbjs

> Create spectrum helpers for tiny-osbjs.

Latest version **1.4.0** (published 2022-07-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install @osbjs/spectrum-tiny-osbjs
pnpm add @osbjs/spectrum-tiny-osbjs
yarn add @osbjs/spectrum-tiny-osbjs
bun add @osbjs/spectrum-tiny-osbjs
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.0 |
| Published | 2022-07-20 |
| First published | 2022-06-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 25.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Nanachi |
| Maintainers | nanachi-code |
| Keywords | osb, osu, osbjs, audio, analyzer |

## Links

- npm: https://www.npmjs.com/package/@osbjs/spectrum-tiny-osbjs
- npm.io page: https://npm.io/package/@osbjs/spectrum-tiny-osbjs

## Dependencies (1)

- [ffmpeg-static](https://npm.io/package/ffmpeg-static.md) ^5.0.0

## Recent versions

- 1.4.0 (latest) — 2022-07-20
- 1.2.0 — 2022-07-12
- 1.1.1 — 2022-06-29
- 1.1.0 — 2022-06-28
- 1.0.0 — 2022-06-26
- 1.0.0-c — 2022-06-26
- 1.0.0-b — 2022-06-26
- 1.0.0-a — 2022-06-26

## README

# spectrum-tiny-osbjs
Create spectrum helpers for tiny-osbjs.

## Install
```bash
npm i @osbjs/spectrum-tiny-osbjs @osbjs/tiny-osbjs
```

## Usage
Create a schema file by calling `createSpectrumSchema` in a seperate file and exec it.
```js
// createSchema.js
#!/usr/bin/env node
const { createSpectrumSchema } = require('@osbjs/spectrum-tiny-osbjs')

createSpectrumSchema('path/to/audio/file')
// and then call `node createSchema` 1 time.
```

Spectrum schema contains info on start time, end time, amount of frame-per-second and spectrum "frames", each frame contains 64 numbers between 0 and 1 that can be used to scale the spectrum bar. Example of how to create a simple spectrum effect:
```js
const { loadSpectrumSchema } = require('@osbjs/spectrum-tiny-osbjs')

const { startTime, endTime, fps, spectrumFrames } = loadSpectrumSchema(schemaPath)

const timestep = 1000 / fps
const width = 20
const maxHeight = 50
const margin = 3
const barCount = 20

let x = -107

for (let i = 0; i < barCount; i++) {
	const frames = spectrumFrames.map((frame) => frame[i])

	createSprite('sb/pixel.png', 'Background', 'Centre', { x, y: 240 }, () => {
		fadeAtTime(startTime, 1)
		colorAtTime(startTime, { r: 255, g: 255, b: 255 })

		for (let j = 0; j < frames.length; j++) {
			scaleVec(
				startTime + timestep * j,
				startTime + timestep * (j + 1),
				{ x: width, y: frames[j] * maxHeight },
				{ x: width, y: frames[j + 1] * maxHeight }
			)
		}
	})

	x += width + margin
}
```

## API documentation
### createSpectrumSchema
```ts
async function createSpectrumSchema(
	audioFilePath: string,
	outFilePath: string = 'spectrum.json',
	startTime: number = 0,
	endTime: number = -1,
	fps: number = 24,
	barCount: number = 64
)
```
Create a json file that store the audio data needed to create the spectrum effect.

### loadSpectrumSchema
```ts
function loadSpectrumSchema(schemaPath: string): SpectrumSchema
type SpectrumSchema = {
	startTime: number
	endTime: number
	fps: number
	spectrumFrames: SpectrumFrame[]
}
type SpectrumFrame = number[]
```
Load schema from json file.

### extractFrames
```ts
function extractFrames(spectrumFrames: SpectrumFrame[], startTime: number, endTime: number, fps: number): SpectrumFrame[]
```
Extract frames from a specific time period.

---
_Source: https://npm.io/package/@osbjs/spectrum-tiny-osbjs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
