Very small OpenGL wrapper (before moggle was there)
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 

115 lines
2.0 KiB

//
// 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){
alGenSources(1, &source);
alSourcei(source, AL_BUFFER, buffer.buffer);
}
void set_pitch(float pitch){
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