Giter Club home page Giter Club logo

sound-fader-ios's Introduction

iOS sound fader for AvAudioPlayer written in Swift

This is an iOS utility class that allows to fade sounds in and out with AvAudioPlayer. One can set duration and velocity of the fade. Velocity can vary from linear to logarithmic.

Installation

Copy iiFaderForAvAudioPlayer.swift file to your project.

Usage

Play an audio file

import AVFoundation

let soundURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), "my_lovely_horse.mp3", nil, nil)
let player = AVAudioPlayer(contentsOfURL: soundURL, error: nil)
player.play()
player.volume = 0

Instantiate a fader

let fader = iiFaderForAvAudioPlayer(player: player)
fader.fadeIn()
fader.fadeOut()

I would create a fader property somewhere in my app and keep a strong reference to the fader.

Set fade duration and velocity

fader.fadeIn(duration: 3, velocity: 2)

Supply fade start/end volume, completion callback

fader.fade(fromVolume: 0.3, toVolume: 0.7, duration: 3, velocity: 2) { finished in
  // fading finished
}

Set the quality of fade

fader.volumeAlterationsPerSecond = 20

Larger numbers will produce finer fade effect at expense of CPU juice.

Volume functions

The following graph shows how sound volume changes during the fade.

Sound fade logarithmic velocity function for iOS/Swift

Fade in formula

Sound fade out logarithmic formula

Fade out formula

Sound fade in logarithmic formula

Where x is time and v is velocity.

Velocity of 0 creates a linear fade. Values greater than zero produce more logarithmic fade affect. Logarithmic fade sounds more gradual to a human ear. I personally use velocity values from 2 to 5.

Live graph demo: https://www.desmos.com/calculator/wnstesdf0h

Demo app

Sound fader for iOS written in swift

Attribution

The lovely music was recorded by Carlos Vallejo and downloaded from http://www.flashkit.com/loops/Easy_Listening/Ill_Be_-Carlos_V-10012/index.php

Project home

https://github.com/evgenyneu/sound-fader-ios

sound-fader-ios's People

Contributors

evgenyneu avatar

Watchers

Carabineiro avatar

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.