Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
miniaudio
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
miniaudio
Commits
26ccabc8
Commit
26ccabc8
authored
Oct 21, 2016
by
David Reid
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add examples.
parent
bfc4d3ff
Changes
3
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
1951 additions
and
0 deletions
+1951
-0
examples/dr_wav.h
examples/dr_wav.h
+1815
-0
examples/simple_capture.c
examples/simple_capture.c
+79
-0
examples/simple_playback.c
examples/simple_playback.c
+57
-0
No files found.
examples/dr_wav.h
0 → 100644
View file @
26ccabc8
This diff is collapsed.
Click to expand it.
examples/simple_capture.c
0 → 100644
View file @
26ccabc8
// This example simply captures data from your default microphone until you press Enter, after
// which it plays back the captured audio.
#define MAL_IMPLEMENTATION
#include "../mini_al.h"
#include <stdlib.h>
#include <stdio.h>
mal_uint32
capturedSampleCount
=
0
;
float
*
pCapturedSamples
=
NULL
;
mal_uint32
playbackSample
=
0
;
void
on_recv_frames
(
mal_device
*
pDevice
,
mal_uint32
frameCount
,
const
void
*
pSamples
)
{
mal_uint32
sampleCount
=
frameCount
*
pDevice
->
channels
;
mal_uint32
newCapturedSampleCount
=
capturedSampleCount
+
sampleCount
;
float
*
pNewCapturedSamples
=
(
float
*
)
realloc
(
pCapturedSamples
,
newCapturedSampleCount
*
sizeof
(
float
));
if
(
pNewCapturedSamples
==
NULL
)
{
return
;
}
memcpy
(
pNewCapturedSamples
+
capturedSampleCount
,
pSamples
,
sampleCount
*
sizeof
(
float
));
pCapturedSamples
=
pNewCapturedSamples
;
capturedSampleCount
=
newCapturedSampleCount
;
}
mal_uint32
on_send_frames
(
mal_device
*
pDevice
,
mal_uint32
frameCount
,
void
*
pSamples
)
{
mal_uint32
samplesToRead
=
frameCount
*
pDevice
->
channels
;
if
(
samplesToRead
>
capturedSampleCount
-
playbackSample
)
{
samplesToRead
=
capturedSampleCount
-
playbackSample
;
}
if
(
samplesToRead
==
0
)
{
return
0
;
}
memcpy
(
pSamples
,
pCapturedSamples
+
playbackSample
,
samplesToRead
*
sizeof
(
float
));
playbackSample
+=
samplesToRead
;
return
samplesToRead
/
pDevice
->
channels
;
}
int
main
()
{
printf
(
"Recording...
\n
"
);
mal_device
captureDevice
;
if
(
mal_device_init
(
&
captureDevice
,
mal_device_type_capture
,
NULL
,
mal_format_f32
,
2
,
48000
,
0
,
0
,
NULL
))
{
printf
(
"Failed to initialize capture device.
\n
"
);
return
-
2
;
}
mal_device_set_recv_callback
(
&
captureDevice
,
on_recv_frames
);
mal_device_start
(
&
captureDevice
);
printf
(
"Press Enter to stop recording...
\n
"
);
getchar
();
mal_device_uninit
(
&
captureDevice
);
printf
(
"Playing...
\n
"
);
mal_device
playbackDevice
;
if
(
mal_device_init
(
&
playbackDevice
,
mal_device_type_playback
,
NULL
,
mal_format_f32
,
2
,
48000
,
0
,
0
,
NULL
))
{
printf
(
"Failed to initialize playback device.
\n
"
);
return
-
3
;
}
mal_device_set_send_callback
(
&
playbackDevice
,
on_send_frames
);
mal_device_start
(
&
playbackDevice
);
printf
(
"Press Enter to quit...
\n
"
);
getchar
();
mal_device_uninit
(
&
playbackDevice
);
return
0
;
}
\ No newline at end of file
examples/simple_playback.c
0 → 100644
View file @
26ccabc8
#define MAL_IMPLEMENTATION
#include "../mini_al.h"
#define DR_WAV_IMPLEMENTATION
#include "dr_wav.h"
#include <stdio.h>
// This is the function that's used for sending more data to the device for playback.
mal_uint32
on_send_frames_to_device
(
mal_device
*
pDevice
,
mal_uint32
frameCount
,
void
*
pSamples
)
{
drwav
*
pWav
=
(
drwav
*
)
pDevice
->
pUserData
;
if
(
pWav
==
NULL
)
{
return
0
;
}
return
(
mal_uint32
)
drwav_read_f32
(
pWav
,
frameCount
*
pDevice
->
channels
,
(
float
*
)
pSamples
)
/
pDevice
->
channels
;
}
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
<
2
)
{
printf
(
"No input file."
);
return
-
1
;
}
drwav
wav
;
if
(
!
drwav_init_file
(
&
wav
,
argv
[
1
]))
{
printf
(
"Not a valid WAV file."
);
return
-
2
;
}
// In this example we use the default playback device with a default buffer size and period count.
mal_device
device
;
if
(
mal_device_init
(
&
device
,
mal_device_type_playback
,
NULL
,
mal_format_f32
,
wav
.
channels
,
wav
.
sampleRate
,
0
,
0
,
NULL
)
!=
MAL_SUCCESS
)
{
printf
(
"Failed to open playback device."
);
drwav_uninit
(
&
wav
);
return
-
3
;
}
// The pUserData member of mal_device is reserved for you.
device
.
pUserData
=
&
wav
;
// This is the callback for sending data to a playback device when it needs more. Make sure
// it's set before playing the device otherwise you'll end up with silence for the first
// bunch of frames.
mal_device_set_send_callback
(
&
device
,
on_send_frames_to_device
);
mal_device_start
(
&
device
);
printf
(
"Press Enter to quit..."
);
getchar
();
mal_device_uninit
(
&
device
);
drwav_uninit
(
&
wav
);
return
0
;
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment