Skip to content

Commit 2b32e50

Browse files
authored
Merge pull request #119 from aryanchoudharypro/main
Wrap wxSound for WAV playback
2 parents d517efb + f460ca3 commit 2b32e50

File tree

8 files changed

+175
-8
lines changed

8 files changed

+175
-8
lines changed

rust/wxdragon-sys/cpp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ endif()
118118

119119
# --- wxDragon Library Sources ---
120120
set(WXDRAGON_SOURCES
121+
${CMAKE_CURRENT_SOURCE_DIR}/src/core/sound.cpp
121122
${CMAKE_CURRENT_SOURCE_DIR}/src/core/timer.cpp
122123
${CMAKE_CURRENT_SOURCE_DIR}/src/about.cpp
123124
${CMAKE_CURRENT_SOURCE_DIR}/src/activity_indicator.cpp
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef WXD_SOUND_H
2+
#define WXD_SOUND_H
3+
4+
#include "../wxd_types.h"
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
// Sound play flags
11+
typedef enum {
12+
WXD_SOUND_SYNC = 0x0000,
13+
WXD_SOUND_ASYNC = 0x0001,
14+
WXD_SOUND_LOOP = 0x0002
15+
} wxd_SoundFlags;
16+
17+
// --- Sound Functions ---
18+
19+
WXD_EXPORTED wxd_Sound_t*
20+
wxd_Sound_Create(const char* fileName, bool isResource);
21+
22+
WXD_EXPORTED void
23+
wxd_Sound_Destroy(wxd_Sound_t* self);
24+
25+
WXD_EXPORTED bool
26+
wxd_Sound_IsOk(wxd_Sound_t* self);
27+
28+
WXD_EXPORTED bool
29+
wxd_Sound_Play(wxd_Sound_t* self, unsigned int flags);
30+
31+
WXD_EXPORTED bool
32+
wxd_Sound_PlayFile(const char* filename, unsigned int flags);
33+
34+
WXD_EXPORTED void
35+
wxd_Sound_Stop(void);
36+
37+
#ifdef __cplusplus
38+
}
39+
#endif
40+
41+
#endif // WXD_SOUND_H

rust/wxdragon-sys/cpp/include/wxd_types.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#ifndef WXD_TYPES_H
22
#define WXD_TYPES_H
33

4+
// Define WXDRAGON_API for export/import
5+
#define WXDRAGON_API
6+
7+
// Define export macro
8+
#ifndef WXD_EXPORTED
9+
#define WXD_EXPORTED WXDRAGON_API
10+
#endif
11+
412
// Use standard C types
513
#include <stdbool.h>
614
#include <stdint.h> // For integer types if needed
@@ -664,6 +672,7 @@ typedef struct wxd_StyledTextCtrl_t wxd_StyledTextCtrl_t;
664672

665673
// AppProgressIndicator type
666674
typedef struct wxd_AppProgressIndicator_t wxd_AppProgressIndicator_t;
675+
typedef struct wxd_Sound_t wxd_Sound_t;
667676

668677
// --- Appearance Support (wxWidgets 3.3.0+) ---
669678

rust/wxdragon-sys/cpp/include/wxdragon.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
#ifndef WXDRAGON_H
22
#define WXDRAGON_H
33

4-
// Define WXDRAGON_API for export/import
5-
#define WXDRAGON_API
6-
7-
// Define export macro (used by all sub-headers indirectly via wxd_types.h or if they need it themselves)
8-
#ifndef WXD_EXPORTED
9-
#define WXD_EXPORTED WXDRAGON_API
10-
#endif
11-
124
// Include all fundamental types first
135
#include "wxd_types.h" // Contains all basic C types and opaque struct typedefs
146
#include "array_string.h" // ArrayString helper functions
@@ -127,6 +119,9 @@ extern "C" {
127119
// IPC (Inter-Process Communication)
128120
#include "core/wxd_ipc.h"
129121

122+
// Sound support
123+
#include "core/wxd_sound.h"
124+
130125
// Other categories
131126
#include "sizers/wxd_sizers.h"
132127
#include "dialogs/wxd_dialogs.h"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <wx/sound.h>
2+
#include "../../include/core/wxd_sound.h"
3+
#include "../wxd_utils.h"
4+
5+
extern "C" {
6+
7+
wxd_Sound_t*
8+
wxd_Sound_Create(const char* fileName, bool isResource) {
9+
wxSound* sound = new wxSound(wxString::FromUTF8(fileName), isResource);
10+
return (wxd_Sound_t*)sound;
11+
}
12+
13+
void
14+
wxd_Sound_Destroy(wxd_Sound_t* self) {
15+
if (self) {
16+
delete (wxSound*)self;
17+
}
18+
}
19+
20+
bool
21+
wxd_Sound_IsOk(wxd_Sound_t* self) {
22+
if (self) {
23+
return ((wxSound*)self)->IsOk();
24+
}
25+
return false;
26+
}
27+
28+
bool
29+
wxd_Sound_Play(wxd_Sound_t* self, unsigned int flags) {
30+
if (self) {
31+
return ((wxSound*)self)->Play(flags);
32+
}
33+
return false;
34+
}
35+
36+
bool
37+
wxd_Sound_PlayFile(const char* filename, unsigned int flags) {
38+
return wxSound::Play(wxString::FromUTF8(filename), flags);
39+
}
40+
41+
void
42+
wxd_Sound_Stop(void) {
43+
wxSound::Stop();
44+
}
45+
46+
} // extern "C"

rust/wxdragon/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub mod prelude;
3030
pub mod scrollable;
3131
pub mod single_instance_checker;
3232
pub mod sizers;
33+
pub mod sound;
3334
pub mod sysopt;
3435
pub mod timer;
3536
pub mod translations;

rust/wxdragon/src/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub use crate::geometry::{Point, Rect, Size};
1919
pub use crate::id::{ID_ANY, ID_CANCEL, ID_HIGHEST, ID_NO, ID_OK, ID_YES, Id};
2020
pub use crate::language::Language;
2121
pub use crate::sizers::WxSizer;
22+
pub use crate::sound::{Sound, SoundFlags};
2223
pub use crate::sysopt::SystemOptions;
2324
pub use crate::types::Style;
2425
pub use crate::utils::{ArrayString, BrowserLaunchFlags, bell, launch_default_browser};

rust/wxdragon/src/sound.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use std::ffi::CString;
2+
use wxdragon_sys as ffi;
3+
4+
widget_style_enum!(
5+
name: SoundFlags,
6+
doc: "Flags for playing sounds.",
7+
variants: {
8+
Sync: ffi::wxd_SoundFlags_WXD_SOUND_SYNC as i64, "Play sound synchronously (waits for sound to finish).",
9+
Async: ffi::wxd_SoundFlags_WXD_SOUND_ASYNC as i64, "Play sound asynchronously (doesn't wait).",
10+
Loop: ffi::wxd_SoundFlags_WXD_SOUND_LOOP as i64, "Loop the sound until stopped."
11+
},
12+
default_variant: Async
13+
);
14+
15+
/// Represents a sound that can be played.
16+
///
17+
/// wxSound is typically limited to WAV files.
18+
pub struct Sound {
19+
ptr: *mut ffi::wxd_Sound_t,
20+
}
21+
22+
impl Sound {
23+
/// Creates a new sound from a file.
24+
///
25+
/// # Arguments
26+
/// * `file_name` - Path to the sound file (usually .wav).
27+
/// * `is_resource` - If true, file_name is a resource name (Windows only).
28+
pub fn new(file_name: &str, is_resource: bool) -> Self {
29+
let c_file = CString::new(file_name).expect("CString::new failed");
30+
let ptr = unsafe { ffi::wxd_Sound_Create(c_file.as_ptr(), is_resource) };
31+
Self { ptr }
32+
}
33+
34+
/// Returns true if the sound was created successfully.
35+
pub fn is_ok(&self) -> bool {
36+
if self.ptr.is_null() {
37+
return false;
38+
}
39+
unsafe { ffi::wxd_Sound_IsOk(self.ptr) }
40+
}
41+
42+
/// Plays the sound with given flags.
43+
pub fn play(&self, flags: SoundFlags) -> bool {
44+
if self.ptr.is_null() {
45+
return false;
46+
}
47+
unsafe { ffi::wxd_Sound_Play(self.ptr, flags.bits() as u32) }
48+
}
49+
50+
/// Plays a sound file directly without creating a Sound object.
51+
pub fn play_file(file_name: &str, flags: SoundFlags) -> bool {
52+
let c_file = match CString::new(file_name) {
53+
Ok(s) => s,
54+
Err(_) => return false,
55+
};
56+
unsafe { ffi::wxd_Sound_PlayFile(c_file.as_ptr(), flags.bits() as u32) }
57+
}
58+
59+
/// Stops any currently playing sound.
60+
pub fn stop() {
61+
unsafe { ffi::wxd_Sound_Stop() }
62+
}
63+
}
64+
65+
impl Drop for Sound {
66+
fn drop(&mut self) {
67+
if !self.ptr.is_null() {
68+
unsafe { ffi::wxd_Sound_Destroy(self.ptr) };
69+
}
70+
}
71+
}
72+
73+
unsafe impl Send for Sound {}

0 commit comments

Comments
 (0)