Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
boytacean
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
João Magalhães
boytacean
Commits
01c26760
Verified
Commit
01c26760
authored
2 years ago
by
João Magalhães
Browse files
Options
Downloads
Patches
Plain Diff
feat: initial support for audio initialization
parent
dbb536b5
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!19
Initial tentative audio support 🔉
Pipeline
#2244
passed
2 years ago
Stage: build
Stage: test
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
frontends/sdl/src/audio.rs
+60
-0
60 additions, 0 deletions
frontends/sdl/src/audio.rs
frontends/sdl/src/graphics.rs
+2
-3
2 additions, 3 deletions
frontends/sdl/src/graphics.rs
frontends/sdl/src/main.rs
+16
-10
16 additions, 10 deletions
frontends/sdl/src/main.rs
with
78 additions
and
13 deletions
frontends/sdl/src/audio.rs
0 → 100644
+
60
−
0
View file @
01c26760
use
sdl2
::{
audio
::{
AudioCallback
,
AudioSpecDesired
},
AudioSubsystem
,
Sdl
,
};
use
std
::
time
::
Duration
;
struct
SquareWave
{
phase_inc
:
f32
,
phase
:
f32
,
volume
:
f32
,
}
impl
AudioCallback
for
SquareWave
{
type
Channel
=
f32
;
fn
callback
(
&
mut
self
,
out
:
&
mut
[
f32
])
{
for
x
in
out
.iter_mut
()
{
// this is a square wave with 50% of down
// and 50% of up values
*
x
=
if
self
.phase
<=
0.5
{
self
.volume
}
else
{
-
self
.volume
};
self
.phase
=
(
self
.phase
+
self
.phase_inc
)
%
1.0
;
}
}
}
pub
struct
Audio
{
pub
audio_subsystem
:
AudioSubsystem
,
}
impl
Audio
{
pub
fn
new
(
sdl
:
&
Sdl
)
->
Self
{
let
audio_subsystem
=
sdl
.audio
()
.unwrap
();
let
desired_spec
=
AudioSpecDesired
{
freq
:
Some
(
44100
),
channels
:
Some
(
1
),
samples
:
None
,
};
let
device
=
audio_subsystem
.open_playback
(
None
,
&
desired_spec
,
|
spec
|
SquareWave
{
phase_inc
:
440.0
/
spec
.freq
as
f32
,
phase
:
0.0
,
volume
:
0.25
,
})
.unwrap
();
// starts the playback by resuming the audio
// device's activity
device
.resume
();
std
::
thread
::
sleep
(
Duration
::
from_millis
(
2000
));
Self
{
audio_subsystem
}
}
}
This diff is collapsed.
Click to expand it.
frontends/sdl/src/
util
.rs
→
frontends/sdl/src/
graphics
.rs
+
2
−
3
View file @
01c26760
use
sdl2
::{
use
sdl2
::{
render
::
Canvas
,
rwops
::
RWops
,
surface
::
Surface
,
sys
::
image
,
ttf
::
Sdl2TtfContext
,
video
::
Window
,
render
::
Canvas
,
rwops
::
RWops
,
surface
::
Surface
,
sys
::
image
,
ttf
::
Sdl2TtfContext
,
video
::
Window
,
AudioSubsystem
,
EventPump
,
TimerSubsystem
,
VideoSubsystem
,
AudioSubsystem
,
EventPump
,
Sdl
,
TimerSubsystem
,
VideoSubsystem
,
};
};
/// Structure that provides the complete set of Graphics
/// Structure that provides the complete set of Graphics
...
@@ -19,10 +19,9 @@ impl Graphics {
...
@@ -19,10 +19,9 @@ impl Graphics {
/// Start the SDL sub-system and all of its structure and returns
/// Start the SDL sub-system and all of its structure and returns
/// a structure with all the needed stuff to handle SDL graphics
/// a structure with all the needed stuff to handle SDL graphics
/// and sound.
/// and sound.
pub
fn
new
(
title
:
&
str
,
width
:
u32
,
height
:
u32
,
scale
:
f32
)
->
Self
{
pub
fn
new
(
sdl
:
&
Sdl
,
title
:
&
str
,
width
:
u32
,
height
:
u32
,
scale
:
f32
)
->
Self
{
// initializes the SDL sub-system, making it ready to be
// initializes the SDL sub-system, making it ready to be
// used for display of graphics and audio
// used for display of graphics and audio
let
sdl
=
sdl2
::
init
()
.unwrap
();
let
video_subsystem
=
sdl
.video
()
.unwrap
();
let
video_subsystem
=
sdl
.video
()
.unwrap
();
let
timer_subsystem
=
sdl
.timer
()
.unwrap
();
let
timer_subsystem
=
sdl
.timer
()
.unwrap
();
let
audio_subsystem
=
sdl
.audio
()
.unwrap
();
let
audio_subsystem
=
sdl
.audio
()
.unwrap
();
...
...
This diff is collapsed.
Click to expand it.
frontends/sdl/src/main.rs
+
16
−
10
View file @
01c26760
#![allow(clippy::uninlined_format_args)]
#![allow(clippy::uninlined_format_args)]
pub
mod
audio
;
pub
mod
data
;
pub
mod
data
;
pub
mod
util
;
pub
mod
graphics
;
use
audio
::
Audio
;
use
boytacean
::{
use
boytacean
::{
gb
::
GameBoy
,
gb
::
GameBoy
,
pad
::
PadKey
,
pad
::
PadKey
,
ppu
::{
PaletteInfo
,
PpuMode
,
DISPLAY_HEIGHT
,
DISPLAY_WIDTH
},
ppu
::{
PaletteInfo
,
PpuMode
,
DISPLAY_HEIGHT
,
DISPLAY_WIDTH
},
};
};
use
graphics
::{
surface_from_bytes
,
Graphics
};
use
sdl2
::{
event
::
Event
,
keyboard
::
Keycode
,
pixels
::
PixelFormatEnum
};
use
sdl2
::{
event
::
Event
,
keyboard
::
Keycode
,
pixels
::
PixelFormatEnum
};
use
std
::{
cmp
::
max
,
time
::
SystemTime
};
use
std
::{
cmp
::
max
,
time
::
SystemTime
};
use
util
::
Graphics
;
use
crate
::
util
::
surface_from_bytes
;
/// The scale at which the screen is going to be drawn
/// The scale at which the screen is going to be drawn
/// meaning the ratio between Game Boy resolution and
/// meaning the ratio between Game Boy resolution and
...
@@ -41,6 +41,7 @@ impl Default for Benchmark {
...
@@ -41,6 +41,7 @@ impl Default for Benchmark {
pub
struct
Emulator
{
pub
struct
Emulator
{
system
:
GameBoy
,
system
:
GameBoy
,
graphics
:
Graphics
,
graphics
:
Graphics
,
audio
:
Audio
,
logic_frequency
:
u32
,
logic_frequency
:
u32
,
visual_frequency
:
f32
,
visual_frequency
:
f32
,
next_tick_time
:
f32
,
next_tick_time
:
f32
,
...
@@ -51,14 +52,19 @@ pub struct Emulator {
...
@@ -51,14 +52,19 @@ pub struct Emulator {
impl
Emulator
{
impl
Emulator
{
pub
fn
new
(
system
:
GameBoy
,
screen_scale
:
f32
)
->
Self
{
pub
fn
new
(
system
:
GameBoy
,
screen_scale
:
f32
)
->
Self
{
let
sdl
=
sdl2
::
init
()
.unwrap
();
let
graphics
=
Graphics
::
new
(
&
sdl
,
TITLE
,
DISPLAY_WIDTH
as
u32
,
DISPLAY_HEIGHT
as
u32
,
screen_scale
,
);
let
audio
=
Audio
::
new
(
&
sdl
);
Self
{
Self
{
system
,
system
,
graphics
:
Graphics
::
new
(
graphics
:
graphics
,
TITLE
,
audio
:
audio
,
DISPLAY_WIDTH
as
u32
,
DISPLAY_HEIGHT
as
u32
,
screen_scale
,
),
logic_frequency
:
GameBoy
::
CPU_FREQ
,
logic_frequency
:
GameBoy
::
CPU_FREQ
,
visual_frequency
:
GameBoy
::
VISUAL_FREQ
,
visual_frequency
:
GameBoy
::
VISUAL_FREQ
,
next_tick_time
:
0.0
,
next_tick_time
:
0.0
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment