LoginSignup
10
9

More than 5 years have passed since last update.

GLFW3 サウンド再生 簡易プログラム公開

Last updated at Posted at 2015-10-22

GLFWとは?

  • http://www.glfw.org/download.html
  • OpenGLを簡易的に扱えるようにしたライブラリーです。
  • ウィンドウを作成し, OpenGL のコンテキストを作って, 入力 (デバイス) を管理する、無料のオープンソースのマルチプラットフォームのライブラリだと書いてあります。
  • ライセンスは unmodified zlib/libpng license を採用しています。
  • GLFW3は、GLFWの最新バージョンになっています。

特徴

  • 非常にコンパクトである
  • マルチプラットホームである
  • OpenGL のバージョンやプロファイルが指定できる
  • 最初からダブルバッファリングになっている
  • イベントループを自分で書ける。
  • ポーリング方式とコールバック方式のどちらにも対応している
  • マルチウィンドウやマルチモニタに対応している
  • 入力デバイスの取り扱い方法が異なる
  • etc...

使用したライブラリー

プログラム

  • 今回は、GLFWの内容は省略しています。
  • メインは、オーディオです。
Audio.h

#pragma once
#include <string>
#include <OpenAL/al.h>

class Audio
{
public:
    explicit Audio(const std::string& filePath);

    // ループ設定
    void Loop(const bool isLoop);

    // 再生
    void Play();

    // 一時停止
    void Pause();

    // 停止
    void Stop();

    // 再生中かどうか
    const bool IsPlaying()const;

    // 音声データの尺を秒単位で取得する
    const float GetDuration()const;

    // 現在の再生位置を秒単位で取得する
    const float GetOffset()const;

private:
    ALuint buffer;
    ALuint source;

};


Audio.cpp

#include "Audio.h"
#include <OpenAL\alut.h>
#include <assert.h>

Audio::Audio(const std::string& filePath) :
buffer(alutCreateBufferFromFile(filePath.c_str())),
source(0)
{
    assert(buffer != 0);

    alGenSources(1, &source);
    alSourcei(source, AL_BUFFER, buffer); // 音データのバッファを指定

    Loop(false);
}


void Audio::Loop(const bool isLoop)
{
    alSourcei(source, AL_LOOPING, isLoop ? AL_TRUE : AL_FALSE); // ループの設定
}

void Audio::Play()
{
    alSourcePlay(source);
}


void Audio::Stop()
{
    alSourceStop(source);
}

void Audio::Pause()
{
    alSourcePause(source);
}

const bool Audio::IsPlaying()const
{
    return GetOffset() != 0;
}

const float Audio::GetDuration()const
{
    ALint size, frequency, channels, bits;

    alGetBufferi(buffer, AL_SIZE, &size);
    alGetBufferi(buffer, AL_FREQUENCY, &frequency);
    alGetBufferi(buffer, AL_CHANNELS, &channels);
    alGetBufferi(buffer, AL_BITS, &bits);

    return static_cast<float>(size) / static_cast<float>(frequency*channels*(bits / 8));

}

const float Audio::GetOffset()const
{
    float offset;

    alGetSourcef(source, AL_SEC_OFFSET, &offset);

    return offset;

}

main.cpp
#include <GLFW\glfw3.h>
#include <OpenAL\al.h>
#include <OpenAL\alut.h>
#include "Audio\Audio.h"

#pragma comment(lib,"glfw3.lib")
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"OpenAL32.lib")
#pragma comment(lib,"alut.lib")

int main()
{
    // glfwを初期化
    if (!glfwInit()) return 0;

    // alutを初期化
    if (!alutInit(nullptr, nullptr)) return 0;

    const int width = 500;
    const int height = 500;

    // ウィンドウを生成
    GLFWwindow *window = glfwCreateWindow(width, height, "Title", NULL, NULL);

    if (window == nullptr) return 0;

    // Audioの初期化
    Audio audio("Resources/Sound.wav");

    audio.Play();

    while (!glfwWindowShouldClose(window))
    {
        // 省略
    }

    alutExit();

    glfwTerminate();
    glfwDestroyWindow(window);


    return 0;
}

対応している形式

  • .wav
  • 残念ながら、1つしか対応していませんが、他のライブラリーを組み合わせれば、様々な形式が対応することが可能です。
10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9