-- This source file is part of HGamer3D -- (A project to enable 3D game development in Haskell) -- For the latest info, see http://www.althainz.de/HGamer3D.html -- -- (c) 2011 Peter Althainz -- -- Licensed under the Apache License, Version 2.0 (the "License"); -- you may not use this file except in compliance with the License. -- You may obtain a copy of the License at -- -- http://www.apache.org/licenses/LICENSE-2.0 -- -- Unless required by applicable law or agreed to in writing, software -- distributed under the License is distributed on an "AS IS" BASIS, -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -- See the License for the specific language governing permissions and -- limitations under the License. -- Audio.hs -- |This module is one sub module of the second attempt, to provide an -- abstracted interface to the HGamer3D API Bindings. It is named 'Two', -- submodule Audio and handles Music and Sound. module HGamer3D.APIs.Base.Audio ( -- Enums -- module HGamer3D.Bindings.SFML.EnumSoundSourceStatus, -- Data -- AudioSource (..), -- Listener Functions setAudioListenerVolume, getAudioListenerVolume, setAudioListenerPosition, setAudioListenerDirection, -- Source creation and delete Functions createMusic, createSound, deleteAudioSource, -- Source play function playAudioSource, pauseAudioSource, stopAudioSource, setAudioSourceLoop, getAudioSourceLoop, -- Source Functions setAudioSourcePitch, setAudioSourceVolume, setAudioSourcePosition, setAudioSourceAttenuation, getAudioSourcePitch, getAudioSourceVolume, getAudioSourceMinDistance, getAudioSourceAttenuation, attachAudioSourceToListener, isAudioSourceAttachedToListener, ) where import GHC.Ptr import HGamer3D.APIs.Base.Common import HGamer3D.Bindings.SFML.ClassPtr import HGamer3D.Bindings.SFML.Utils import qualified HGamer3D.Bindings.SFML.ClassListener as Listener import qualified HGamer3D.Bindings.SFML.ClassMusic as Music import qualified HGamer3D.Bindings.SFML.ClassSound as Sound import qualified HGamer3D.Bindings.SFML.ClassSoundBuffer as SoundBuffer import qualified HGamer3D.Bindings.SFML.ClassSoundSource as SoundSource import qualified HGamer3D.Bindings.SFML.ClassSoundStream as SoundStream import HGamer3D.Bindings.SFML.EnumSoundSourceStatus import HGamer3D.Data.HG3DClass import Control.Monad.Trans import Control.Monad.Reader import HGamer3D.Data.Vector -- Source types data AudioSource = Music HG3DClass | Sound HG3DClass HG3DClass -- Listener Functions setAudioListenerVolume :: Float -> MHGamer3D () setAudioListenerVolume vol = liftIO $ Listener.setGlobalVolume vol getAudioListenerVolume :: MHGamer3D Float getAudioListenerVolume = liftIO $ Listener.getGlobalVolume setAudioListenerPosition :: Vec3 -> MHGamer3D () setAudioListenerPosition (Vec3 x y z) = liftIO $ Listener.setPosition x y z setAudioListenerDirection :: Vec3 -> MHGamer3D () setAudioListenerDirection (Vec3 x y z) = liftIO $ Listener.setDirection x y z -- Music Functions, deletion and cretion of Audio types -- -- | Creates music with name createMusic :: String -> MHGamer3D (Maybe AudioSource) createMusic filename = do music <- liftIO $ Music.new (cs, es) <- ask let dir = (csHG3DPath cs) fOk <- liftIO $ Music.openFromFile music (dir ++ "\\media\\sound\\" ++ filename) if fOk then return (Just (Music music)) else do liftIO $ Music.delete music return Nothing createSound :: String -> MHGamer3D (Maybe AudioSource) createSound filename = do sound <- liftIO $ Sound.new buffer <- liftIO $ SoundBuffer.new (cs, es) <- ask let dir = (csHG3DPath cs) fOk <- liftIO $ SoundBuffer.loadFromFile buffer (dir ++ "\\media\\sound\\" ++ filename) if fOk then do liftIO $ Sound.setBuffer sound buffer return (Just (Sound sound buffer)) else do liftIO $ Sound.delete sound liftIO $ SoundBuffer.delete buffer return Nothing deleteAudioSource :: AudioSource -> MHGamer3D () deleteAudioSource (Music music) = do liftIO $ Music.delete music deleteSource (Sound sound buffer) = do liftIO $ Sound.delete sound liftIO $ SoundBuffer.delete buffer -- Source play, pause, stop -- playAudioSource :: AudioSource -> MHGamer3D () playAudioSource (Music music) = do liftIO $ SoundStream.play music playAudioSource (Sound sound buffer) = do liftIO $ Sound.play sound pauseAudioSource :: AudioSource -> MHGamer3D () pauseAudioSource (Music music) = do liftIO $ SoundStream.pause music pauseAudioSource (Sound sound buffer) = do liftIO $ Sound.pause sound stopAudioSource :: AudioSource -> MHGamer3D () stopAudioSource (Music music) = do liftIO $ SoundStream.stop music stopAudioSource (Sound sound buffer) = do liftIO $ Sound.stop sound getAudioSourceLoop :: AudioSource -> MHGamer3D Bool getAudioSourceLoop (Music music) = do fLoop <- liftIO $ SoundStream.getLoop music return fLoop getAudioSourceLoop (Sound sound buffer) = do fLoop <- liftIO $ Sound.getLoop sound return fLoop setAudioSourceLoop :: AudioSource -> Bool -> MHGamer3D () setAudioSourceLoop (Music music) fLoop = do liftIO $ SoundStream.setLoop music fLoop setAudioSourceLoop (Sound sound buffer) fLoop = do liftIO $ Sound.setLoop sound fLoop -- Sound Source Functions -- setAudioSourcePitch :: AudioSource -> Float -> MHGamer3D () setAudioSourcePitch (Music s) p = liftIO $ SoundSource.setPitch s p setAudioSourcePitch (Sound s b) p = liftIO $ SoundSource.setPitch s p setAudioSourceVolume :: AudioSource -> Float -> MHGamer3D () setAudioSourceVolume (Music s) v = liftIO $ SoundSource.setVolume s v setAudioSourceVolume (Sound s b) v = liftIO $ SoundSource.setVolume s v setAudioSourcePosition :: AudioSource -> Vec3 -> MHGamer3D () setAudioSourcePosition (Music s) (Vec3 x y z) = liftIO $ SoundSource.setPosition s x y z setAudioSourcePosition (Sound s b) (Vec3 x y z) = liftIO $ SoundSource.setPosition s x y z setAudioSourceAttenuation :: AudioSource -> Float -> MHGamer3D () setAudioSourceAttenuation (Music s) a = liftIO $ SoundSource.setAttenuation s a setAudioSourceAttenuation (Sound s b) a = liftIO $ SoundSource.setAttenuation s a getAudioSourcePitch :: AudioSource -> MHGamer3D Float getAudioSourcePitch (Music s) = liftIO $ SoundSource.getPitch s getAudioSourcePitch (Sound s b) = liftIO $ SoundSource.getPitch s getAudioSourceVolume :: AudioSource -> MHGamer3D Float getAudioSourceVolume (Music s) = liftIO $ SoundSource.getVolume s getAudioSourceVolume (Sound s b) = liftIO $ SoundSource.getVolume s getAudioSourceMinDistance :: AudioSource -> MHGamer3D Float getAudioSourceMinDistance (Music s) = liftIO $ SoundSource.getMinDistance s getAudioSourceMinDistance (Sound s b) = liftIO $ SoundSource.getMinDistance s getAudioSourceAttenuation :: AudioSource -> MHGamer3D Float getAudioSourceAttenuation (Music s) = liftIO $ SoundSource.getAttenuation s getAudioSourceAttenuation (Sound s b) = liftIO $ SoundSource.getAttenuation s attachAudioSourceToListener :: AudioSource -> Bool -> MHGamer3D () attachAudioSourceToListener (Music s) isR = liftIO $ SoundSource.setRelativeToListener s isR attachAudioSourceToListener (Sound s b) isR = liftIO $ SoundSource.setRelativeToListener s isR isAudioSourceAttachedToListener :: AudioSource -> MHGamer3D Bool isAudioSourceAttachedToListener (Music s) = liftIO $ SoundSource.isRelativeToListener s isAudioSourceAttachedToListener (Sound s b) = liftIO $ SoundSource.isRelativeToListener s