Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulydev committed May 5, 2016
0 parents commit 744de7b
Show file tree
Hide file tree
Showing 7 changed files with 523 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Ulydev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
154 changes: 154 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
wave
==============

wave is a LÖVE sound manager with advanced audio parsing functionalities.

![demo][demo]

Setup
----------------

```lua
local audio = require "wave" --require the library
```

Usage
----------------

Create a new source
```lua
local beep = audio:newSource("beep.wav", "static")
```

Play it
```lua
beep:play()
```

Parsing audio
----------------

wave allows you to parse audio files to get the "energy" of the song. This allows you to add visual effects that are synchronized with the music.

First, load and parse the audio, then set its intensity
```lua
music = audio:newSource("music.wav", "stream")
music:parse()
music:setIntensity(20)
```

Update it every frame
```lua
function love.update(dt)
music:update(dt)
end
```

Then, get the current energy of the song
```lua
function love.draw()
love.graphics.circle(100, 100, 100+music:getEnergy()*10)
end
```

Working with rhythm
----------------

wave lets you set the BPM of your song to add rhythm-based mechanics to your game.

Load the audio and set its BPM
```lua
music = audio:newSource("music.wav", "stream")
music:setBPM(120)
```

Set a beat callback
```lua
music:onBeat(function()
print("Beat!")
end)
```

Update the music every frame
```lua
function love.update(dt)
music:update(dt)
end
```

Smooth transitions
----------------

Both **pitch** and **volume** properties of a source can be smoothly transitioned.

Set targets
```lua
source:setPitchTarget(1.2)
source:setVolumeTarget(.5)
```

Update the source every frame
```lua
function love.update(dt)
source:update(dt)
end
```

Chaining functions
----------------

**Source** objects are passed through most of the methods. This allows you to chain function calls like so:
```lua
dog = audio
:newSource("dog.wav", "static")
:setVolume(.5)
:play()
```

Methods and aliases
----------------

Create a new source
```lua
source = audio:newSource(path, type)
```

Play, pause, resume and stop a source
```lua
source:play(pitched) --if pitched == true, source will play with a slightly random pitch (useful for recurrent sounds)
source:stop()

source:pause()
source:resume()
```

Parse source
```lua
source:parse()
```

Set/get source properties
```lua
source:setBPM(bpm)

source:setIntensity(intensity)

source:setPitch(pitch)
//-> source:getPitch()

source:setVolume(volume)
//-> source:getVolume()

source:setLooping(bool)
```

Set/get source target properties (will transition smoothly)
```lua
source:setTargetPitch(pitch)
//-> source:getTargetPitch()

source:setTargetVolume(volume)
//-> source:getTargetVolume()
```

[demo]: http://s32.postimg.org/8n00sidfn/out.gif
72 changes: 72 additions & 0 deletions main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
io.stdout:setvbuf'no'

love.window.setTitle("Left and Right to change pitch, Space to emit sound")

audio = require "wave" --require the library

local colors = {
{255, 255, 255},
{0, 0, 0}
}

function love.load()

music = audio
:newSource("music.wav", "stream")

:parse()

:setIntensity(20)
:setBPM(120)

:setPitch(0.1)
:setVolume(0)
:setTargetPitch(1)
:setTargetVolume(.5)

:setLooping(true)

:play() --call this when done configuring

music:onBeat(function()
colors[1], colors[2] = colors[2], colors[1]
end)

--

sound = audio:newSource("sound.wav")

end

function love.update(dt)

music:update(dt)

local dir = (love.keyboard.isDown("left") and -1) or (love.keyboard.isDown("right") and 1) or 0
music:setTargetPitch(math.min(math.max(music:getTargetPitch() + dt * dir, .1), 2)) --between 0.1 and 2

end

function love.draw()

love.graphics.setColor(colors[1])

local _width, _height = love.graphics.getDimensions()
local _energy = music:getEnergy()
local _beatTime = music:getBeatTime()
local _side = music:getBeat() % 2 == 1 and 1 or -1

love.graphics.circle("fill", _width*.5, _height*.5, 50+_energy*10)

love.graphics.rectangle("fill", _width*(.5-_side*.35)-20-_energy*5, _height*.5-5-100+_beatTime*200, 40+_energy*10, 10)
love.graphics.rectangle("fill", _width*(.5+_side*.35)-20-_energy*5, _height*.5-5+100-_beatTime*200, 40+_energy*10, 10)

love.graphics.setBackgroundColor(colors[2])

end

function love.keypressed(key)
if key == "space" then
sound:play(true)
end
end
Binary file added music.mp3
Binary file not shown.
Binary file added music.wav
Binary file not shown.
Binary file added sound.wav
Binary file not shown.
Loading

0 comments on commit 744de7b

Please sign in to comment.