You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
2.1 KiB
116 lines
2.1 KiB
11 years ago
|
//
|
||
|
// Sound.h
|
||
|
// SoundDrop2
|
||
|
//
|
||
|
// Created by Joshua Moerman on 1/9/13.
|
||
|
// Copyright (c) 2013 Vadovas. All rights reserved.
|
||
|
//
|
||
|
|
||
|
#ifndef SoundDrop2_Sound_h
|
||
|
#define SoundDrop2_Sound_h
|
||
|
|
||
|
#include <iostream>
|
||
|
|
||
|
#include <OpenAL/al.h>
|
||
|
#include <OpenAL/alc.h>
|
||
|
|
||
|
#include "CDOpenALSupport.h"
|
||
|
|
||
|
|
||
|
namespace J {
|
||
|
|
||
|
struct AL {
|
||
|
ALCdevice * device{nullptr};
|
||
|
ALCcontext * context{nullptr};
|
||
|
|
||
|
AL(){
|
||
|
// Initializing sound:
|
||
|
// NULL for the default device
|
||
|
device = alcOpenDevice(NULL);
|
||
|
if (device) {
|
||
|
// NULL for no attributes
|
||
|
context = alcCreateContext(device, NULL);
|
||
|
alcMakeContextCurrent(context);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
~AL(){
|
||
|
alcMakeContextCurrent(NULL);
|
||
|
alcDestroyContext(context);
|
||
|
alcCloseDevice(device);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
struct Buffer {
|
||
|
ALuint buffer;
|
||
|
|
||
|
Buffer(Buffer const &) = delete;
|
||
|
Buffer & operator=(Buffer const &) = delete;
|
||
|
|
||
|
Buffer(Buffer && b){
|
||
|
std::swap(buffer, b.buffer);
|
||
|
}
|
||
|
|
||
|
Buffer & operator=(Buffer && b){
|
||
|
std::swap(buffer, b.buffer);
|
||
|
return *this;
|
||
|
}
|
||
|
|
||
|
Buffer(){
|
||
|
alGenBuffers(1, &buffer);
|
||
|
}
|
||
|
|
||
|
~Buffer(){
|
||
|
alDeleteBuffers(1, &buffer);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
inline Buffer load_wav(std::string filename){
|
||
|
Buffer b;
|
||
|
|
||
|
void *data;
|
||
|
ALenum format;
|
||
|
ALsizei sizeInBytes;
|
||
|
ALsizei frequencyInHertz;
|
||
|
|
||
|
data = CDGetOpenALAudioData((CFURLRef)[NSURL fileURLWithPath: [NSString stringWithUTF8String: filename.c_str()]], &sizeInBytes, &format, &frequencyInHertz);
|
||
|
alBufferData(b.buffer, format, data, sizeInBytes, frequencyInHertz);
|
||
|
free(data);
|
||
|
|
||
|
return b;
|
||
|
}
|
||
|
|
||
|
struct Source {
|
||
|
ALuint source;
|
||
|
|
||
|
Source(Source const &) = delete;
|
||
|
Source & operator=(Source const &) = delete;
|
||
|
|
||
|
Source(Buffer const & buffer){
|
||
|
int r = rand() % 10;
|
||
|
float pitch = 0.5 + r*0.5;
|
||
|
|
||
|
alGenSources(1, &source);
|
||
|
alSourcei(source, AL_BUFFER, buffer.buffer);
|
||
|
alSourcef(source, AL_PITCH, pitch); //?
|
||
|
}
|
||
|
|
||
|
void play(){
|
||
|
// will restart if already playing
|
||
|
alSourcePlay(source);
|
||
|
}
|
||
|
|
||
|
~Source(){
|
||
|
// TODO: find out if this is needed
|
||
|
// NOTE: Buffers which are attached to a source can not be deleted
|
||
|
alSourcei(source, AL_BUFFER, 0);
|
||
|
|
||
|
// this will stop the playing
|
||
|
alDeleteSources(1, &source);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
} // namespace J
|
||
|
|
||
|
#endif
|