Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
andystanton committed Jun 8, 2015
0 parents commit 184b1c5
Show file tree
Hide file tree
Showing 19 changed files with 681 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
Makefile
79 changes: 79 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.2.2)
PROJECT(sound-example)

INCLUDE(ExternalProject)

ExternalProject_Add(project_libsndfile
GIT_REPOSITORY https://github.com/erikd/libsndfile.git
PREFIX lib/libsndfile
CONFIGURE_COMMAND <SOURCE_DIR>/configure
BUILD_COMMAND make
BUILD_IN_SOURCE 1
INSTALL_COMMAND echo Skipping install step for libsndfile
)

ExternalProject_Add_Step(project_libsndfile autogen
COMMAND <SOURCE_DIR>/autogen.sh
DEPENDEES update
DEPENDERS configure
)

ExternalProject_Get_Property(project_libsndfile BINARY_DIR)
SET(libsndfile_lib_dir "${BINARY_DIR}/src/.libs")
SET(libsndfile_inc_dir "${BINARY_DIR}/src")

ADD_LIBRARY(libsndfile STATIC IMPORTED)
SET_PROPERTY(TARGET libsndfile PROPERTY IMPORTED_LOCATION ${libsndfile_lib_dir}/libsndfile.a)

SET(LIBSNDFILE_INCLUDE_PATH "${install_dir}/src/project_libsndfile-build/src/")

ExternalProject_Add(project_portaudio
SVN_REPOSITORY https://subversion.assembla.com/svn/portaudio/portaudio/trunk
SVN_TRUST_CERT 1
PREFIX lib/portaudio
CONFIGURE_COMMAND <SOURCE_DIR>/configure
BUILD_IN_SOURCE 1
BUILD_COMMAND make
INSTALL_COMMAND echo Skipping install step for portaudio
)

ExternalProject_Get_Property(project_portaudio BINARY_DIR)
ExternalProject_Get_Property(project_portaudio SOURCE_DIR)
SET(portaudio_lib_dir "${BINARY_DIR}/lib/.libs")
SET(portaudio_inc_dir "${SOURCE_DIR}/include")

ADD_LIBRARY(portaudio STATIC IMPORTED)
SET_PROPERTY(TARGET portaudio PROPERTY IMPORTED_LOCATION "${portaudio_lib_dir}/libportaudio.a")

IF(APPLE)
FIND_LIBRARY(CORE_AUDIO_LIBRARY CoreAudio)
FIND_LIBRARY(AUDIO_TOOLBOX_LIBRARY AudioToolbox)
FIND_LIBRARY(AUDIO_UNIT_LIBRARY AudioUnit)
FIND_LIBRARY(CARBON_LIBRARY Carbon)
SET(EXTRA_LIBS ${CORE_AUDIO_LIBRARY} ${AUDIO_TOOLBOX_LIBRARY} ${AUDIO_UNIT_LIBRARY} ${CARBON_LIBRARY})
ENDIF (APPLE)

IF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
SET(EXTRA_LIBS rt asound jack pthread)
ENDIF(CMAKE_SYSTEM_NAME STREQUAL "Linux")

SET(SOURCE_FILES
src/main.cpp
src/util.cpp
src/FileHandler.cpp
src/StreamHandler.cpp
src/AudioPlayer.cpp)

INCLUDE_DIRECTORIES(include ${libsndfile_inc_dir} ${portaudio_inc_dir})

ADD_EXECUTABLE(sound-example ${SOURCE_FILES})

SET_TARGET_PROPERTIES(sound-example
PROPERTIES
CXX_STANDARD 14)

TARGET_LINK_LIBRARIES(sound-example ${EXTRA_LIBS} libsndfile portaudio)

ADD_CUSTOM_COMMAND(TARGET sound-example PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E
copy_directory "${CMAKE_SOURCE_DIR}/sounds" "${CMAKE_BINARY_DIR}/sounds")
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 Andy Stanton

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.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Sound playback in C++

An example of asynchronous sound playback in C++ with Portaudio and libsndfile.

## Requirements

The plethora of build requirements are courtesy of the build process for libsndfile but are all easily obtained through your operating system's package manager.

### Source Control

- svn
- git

### Build

- c++14 compiler
- cmake >= 3.2.2
- make
- autoconf
- automake
- autogen
- libtool
- pkg-config
- python
3 changes: 3 additions & 0 deletions clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

rm -rf build Makefile
27 changes: 27 additions & 0 deletions configure
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

function createMakefile() {
root_makefile='
.PHONY: execute_example_make
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
current_dir := $(dir $(mkfile_path))
execute_example_make:
$(MAKE) -C ${current_dir}build
prerequisites: execute_example_make
target: prerequisites
'

echo "${root_makefile}" >Makefile
}

mkdir -p build && pushd build >/dev/null

if grep -qe '^MSYS_' <<<"$(uname -s)"; then
generator_override='-G "MSYS Makefiles"'
fi

cmake "${generator_override}" .. && popd && createMakefile
9 changes: 9 additions & 0 deletions include/AudioFile.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "sndfile.h"

struct AudioFile
{
SNDFILE * data;
SF_INFO info;
};
18 changes: 18 additions & 0 deletions include/AudioPlayer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "StreamHandler.hpp"
#include "FileHandler.hpp"

class AudioPlayer
{
public:
AudioPlayer();

void play(string soundfile);
void loop(string soundfile);
void stop();

private:
FileHandler fileHandler;
StreamHandler streamHandler;
};
24 changes: 24 additions & 0 deletions include/FileHandler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "AudioFile.hpp"
#include "util.hpp"

#include <map>
#include <sstream>
#include <string>

using std::map;
using std::string;
using std::stringstream;

class FileHandler
{
public:
FileHandler();
~FileHandler();

bool containsSound(string filename);
AudioFile & getSound(string filename);
private:
map<string, AudioFile> sounds;
};
54 changes: 54 additions & 0 deletions include/StreamHandler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

#include "AudioFile.hpp"

#include "sndfile.h"
#include "portaudio.h"

#if defined (__linux__)
#include <cstdlib> // required for putenv.
#endif

#include <cstring>
#include <sstream>
#include <vector>

using std::stringstream;
using std::vector;

struct Playback
{
AudioFile * audioFile;
int position;
bool loop;
};

enum AudioEventType
{
start, stop
};

class StreamHandler
{
public:
StreamHandler();
~StreamHandler();

void processEvent(AudioEventType audioEventType,
AudioFile * audioFile = nullptr,
bool loop = false);

static int PortAudioCallback(const void * input,
void * output,
unsigned long frameCount,
const PaStreamCallbackTimeInfo * paTimeInfo,
PaStreamCallbackFlags statusFlags,
void * userData);
private:
const int CHANNEL_COUNT = 2;
const int SAMPLE_RATE = 44000;
const PaStreamParameters * NO_INPUT = nullptr;

PaStream * stream;
vector<Playback *> data;
};
39 changes: 39 additions & 0 deletions include/util.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <string>

#if defined (__APPLE__) && defined (__MACH__)

#include <dirent.h>
#include <libproc.h>
#include <unistd.h>
#include <cstdio>
#include <termios.h>
#include <sys/types.h>
#include <sys/time.h>

#elif defined (__linux__)

#include <dirent.h>
#include <unistd.h>
#include <cstdio>
#include <termios.h>
#include <sys/types.h>
#include <sys/time.h>

#endif

using std::string;

namespace util
{
string getApplicationPathAndName();
string getApplicationPath();

#if not defined (_WIN32) && not defined (_WIN64)

void changemode(int dir);
int kbhit();

#endif
}
Binary file added sounds/Powerup14.wav
Binary file not shown.
Binary file added sounds/Powerup47.wav
Binary file not shown.
Binary file added sounds/loop.wav
Binary file not shown.
24 changes: 24 additions & 0 deletions src/AudioPlayer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "AudioPlayer.hpp"

AudioPlayer::AudioPlayer()
: fileHandler()
, streamHandler()
{

}

void AudioPlayer::play(string soundfile)
{
streamHandler.processEvent(AudioEventType::start, &fileHandler.getSound(soundfile));
}

void AudioPlayer::loop(string soundfile)
{
streamHandler.processEvent(AudioEventType::start, &fileHandler.getSound(soundfile), true);

}

void AudioPlayer::stop()
{
streamHandler.processEvent(AudioEventType::stop);
}
46 changes: 46 additions & 0 deletions src/FileHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "FileHandler.hpp"

FileHandler::FileHandler()
: sounds()
{

}

FileHandler::~FileHandler()
{
for (auto entry : sounds)
{
sf_close(entry.second.data);
}
}

bool FileHandler::containsSound(string filename)
{
return sounds.find(filename) != sounds.end();
}

AudioFile & FileHandler::getSound(string filename)
{
if (!containsSound(filename)) {
string fullFilename = util::getApplicationPath() + "/sounds/" + filename;
SF_INFO info;
info.format = 0;
SNDFILE * audioFile = sf_open(fullFilename.c_str(), SFM_READ, &info);

AudioFile sound {
audioFile,
info
};

if (!audioFile)
{
stringstream error;
error << "Unable to open audio file '"
<< filename << "' with full filename '"
<< fullFilename << "'";
throw error.str();
}
sounds[filename] = sound;
}
return sounds[filename];
}
Loading

0 comments on commit 184b1c5

Please sign in to comment.