Code Monkey home page Code Monkey logo

waver's Introduction

Waver CI codecov Crates.io Documentation

Waver is a simple no-std library to generate any waveform of a given frequency, amplitude and phase.

A waveform can be a simple sinusoidal wave or a complex waveform of varying frequency and amplitude. Waver is useful where there's a need to generate a simple sinusoidal sound wave or for constructing a frequency or amplitude modulated carrier wave in bare-metal Arduino or a Raspberry Pi.

Usage

To use Waver, add the following to your Cargo.toml file.

[dependencies]
waver = "0.1"

Example

use std::{vec::Vec, f32::consts::PI};
use waver::{Waveform, Wave, WaveFunc};

fn main() {
  // 44.1Khz sampling rate and 16-bit depth.
  let mut wf = waver::Waveform::<i16>::new(44100.0);

  // Superpose a sine wave, a cosine wave and a triangle function.
  wf.superpose(Wave { frequency: 2600.0, ..Default::default() })
    .superpose(Wave { frequency: 2600.0, phase: PI / 2.0, ..Default::default() })
    .superpose(Wave { frequency: 2600.0, func: WaveFunc::Triangle, ..Default::default() })
    .normalize_amplitudes();

  // Quantization of 100 samples
  let _output: Vec<i16> = wf.iter().take(100).collect();
}

Features

  • Arbitrary quantization levels. Specify the bit depth when constructing Waveform.
  • Online wave generation. No buffers, infinite iterators.
  • Wave superposition with weighted amplitudes.
  • Modulate signal's frequency, amplitude or phase.
  • Numerically stable, prevents clipping.

TODO

  • Implement checks to protect against aliasing (e.g., disallow frequencies above the Nyquist frequency).
  • Use fixed-point arithmetic for platforms that doesn't have an FPU.
  • Replace use of libm crate when math support moves to libcore.
  • Expand the crate features to also include signal analysis functionality.

Contributing

Thought of something you'd like to see in Waver? You can visit the issue tracker to check if it was reported or proposed before, and if not please feel free to create an issue or feature request. Ready to start contributing? The contributing guide is a good place to start. If you have questions please feel free to ask.

waver's People

Contributors

amrali avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

spitfire05

waver's Issues

Support for non-sine waves

It'd be useful if the lib allowed to superpose other types of waves (rectangle, triangle, sawtooth, etc.) as well.

Maybe even create a way for the consuming code to plug-in own periodic function implementations.

I already started some conceptual work on this, maybe will submit a PR soon(tm) up for discussion.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.