Skip to main content

fyrox_sound/
error.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! Contains all possible errors that can occur in the engine.
22
23use std::fmt::{Display, Error, Formatter};
24
25/// Decoder specific error.
26#[derive(Debug)]
27pub enum DecoderError {
28    /// Error coming from Symphonia
29    SymphoniaError(symphonia::core::errors::Error),
30}
31
32/// Generic error enumeration for each error in this engine.
33#[derive(Debug)]
34pub enum SoundError {
35    /// Generic input error.
36    Io(std::io::Error),
37
38    /// No backend is provided for current OS.
39    NoBackend,
40
41    /// Unable to initialize device, exact reason stored in inner value.
42    FailedToInitializeDevice(String),
43
44    /// Invalid header of sound file.
45    InvalidHeader,
46
47    /// Unsupported format of sound file.
48    UnsupportedFormat,
49
50    /// It means that some thread panicked while holding a MutexGuard, the data mutex
51    /// protected can be corrupted.
52    PoisonedMutex,
53
54    /// An error occurred during math calculations, i.e. there was an attempt to
55    /// normalize a vector with length `|v| == 0.0`.
56    MathError(String),
57
58    /// You tried to create a source with streaming buffer that is currently being
59    /// used by some other source. This is wrong because only one source can play
60    /// sound from streaming buffer.
61    StreamingBufferAlreadyInUse,
62
63    /// Decoder specific error, can occur in the decoder by any reason (invalid format,
64    /// insufficient data, etc.). Exact reason stored in inner value.
65    DecoderError(DecoderError),
66
67    /// A buffer is invalid (for example it is LoadError state)
68    BufferFailedToLoad,
69
70    /// A buffer is not loaded yet, consider to `await` it before use.
71    BufferIsNotLoaded,
72}
73
74impl From<std::io::Error> for SoundError {
75    fn from(e: std::io::Error) -> Self {
76        SoundError::Io(e)
77    }
78}
79
80impl<'a, T> From<std::sync::PoisonError<std::sync::MutexGuard<'a, T>>> for SoundError {
81    fn from(_: std::sync::PoisonError<std::sync::MutexGuard<'a, T>>) -> Self {
82        SoundError::PoisonedMutex
83    }
84}
85
86impl From<symphonia::core::errors::Error> for SoundError {
87    fn from(e: symphonia::core::errors::Error) -> Self {
88        SoundError::DecoderError(DecoderError::SymphoniaError(e))
89    }
90}
91
92impl Display for SoundError {
93    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
94        match self {
95            SoundError::Io(io) => write!(f, "io error: {io}"),
96            SoundError::NoBackend => write!(f, "no backend implemented for current platform"),
97            SoundError::FailedToInitializeDevice(reason) => {
98                write!(f, "failed to initialize device. reason: {reason}")
99            }
100            SoundError::InvalidHeader => write!(f, "invalid header of sound file"),
101            SoundError::UnsupportedFormat => write!(f, "unsupported format of sound file"),
102            SoundError::PoisonedMutex => write!(f, "attempt to use poisoned mutex"),
103            SoundError::MathError(reason) => {
104                write!(f, "math error has occurred. reason: {reason}")
105            }
106            SoundError::StreamingBufferAlreadyInUse => {
107                write!(f, "streaming buffer in already in use")
108            }
109            SoundError::DecoderError(de) => write!(f, "internal decoder error: {de:?}"),
110            SoundError::BufferFailedToLoad => write!(f, "a buffer failed to load"),
111            SoundError::BufferIsNotLoaded => write!(f, "a buffer is not loaded yet"),
112        }
113    }
114}
115
116impl std::error::Error for SoundError {}