Comments (7)
Do you want to try these changes.
// This example simply captures data from your default microphone until you press Enter, after which it plays back the captured audio.
package main
import (
"fmt"
"os"
"github.com/gen2brain/malgo"
)
func main() {
ctx, err := malgo.InitContext(nil, malgo.ContextConfig{}, func(message string) {
fmt.Printf("LOG <%v>\n", message)
})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer func() {
_ = ctx.Uninit()
ctx.Free()
}()
deviceConfig := malgo.DefaultDeviceConfig()
deviceConfig.DeviceType = 3
deviceConfig.Capture.Format = malgo.FormatS16
deviceConfig.Capture.Channels = 1
deviceConfig.Playback.Format = malgo.FormatS16
deviceConfig.Playback.Channels = 1
deviceConfig.SampleRate = 8000
deviceConfig.Alsa.NoMMap = 1
var playbackSampleCount uint32
var capturedSampleCount uint32
pCapturedSamples := make([]byte, 0)
sizeInBytes := uint32(malgo.SampleSizeInBytes(deviceConfig.Capture.Format))
onRecvFrames := func(pSample2, pSample []byte, framecount uint32) {
sampleCount := framecount * deviceConfig.Capture.Channels * sizeInBytes
newCapturedSampleCount := capturedSampleCount + sampleCount
pCapturedSamples = append(pCapturedSamples, pSample...)
capturedSampleCount = newCapturedSampleCount
}
fmt.Println("Recording...")
captureCallbacks := malgo.DeviceCallbacks{
Data: onRecvFrames,
}
device, err := malgo.InitDevice(ctx.Context, deviceConfig, captureCallbacks)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = device.Start()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Press Enter to stop recording...")
fmt.Scanln()
device.Uninit()
onSendFrames := func(pSample, nil []byte, framecount uint32) {
samplesToRead := framecount * deviceConfig.Playback.Channels * sizeInBytes
if samplesToRead > capturedSampleCount-playbackSampleCount {
samplesToRead = capturedSampleCount - playbackSampleCount
}
copy(pSample, pCapturedSamples[playbackSampleCount:playbackSampleCount+samplesToRead])
playbackSampleCount += samplesToRead
if playbackSampleCount == uint32(len(pCapturedSamples)) {
playbackSampleCount = 0
}
}
fmt.Println("Playing...")
playbackCallbacks := malgo.DeviceCallbacks{
Data: onSendFrames,
}
device, err = malgo.InitDevice(ctx.Context, deviceConfig, playbackCallbacks)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = device.Start()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Press Enter to quit...")
fmt.Scanln()
device.Uninit()
}
from malgo.
Thank you (Хвала ?).
At least it compile, i test it later when i have mic ...
may be s/deviceConfig.DeviceType = 3/deviceConfig.DeviceType = malgo.Duplex/
?
from malgo.
Yes, it works,
can i afford to recommend some style ?
s/
nSendFrames := func(pSample, nil []byte, framecount uint32) {
/
nSendFrames := func(pSample, _ []byte, framecount uint32) {
/
s/
onRecvFrames := func(pSample2, pSample []byte, framecount uint32) {
/
onRecvFrames := func(_, pSample []byte, framecount uint32) {
/
from malgo.
from malgo.
actually nil here is variable name:
https://play.golang.org/p/yGW0r-fc3_K
from malgo.
Oh yes, so it is.
I tried 'true', 'error', '5' also. Good clarification of the language.
As expected 5 is 5, but the reserved keywords of true/nil/error will become variable name, and true will no longer mean true.
from malgo.
This should be fixed currently.
from malgo.
Related Issues (20)
- Q: Why is iOS not supported? HOT 15
- please update to v0.10.35 HOT 1
- Documentation issue for InitDevice HOT 2
- DeviceInfo.Name function returns a string with null bytes in it HOT 1
- Warnings on macOS 12.1
- Documentation/Examples of using loopback. HOT 1
- Microphone Not Working on built and notarized binary - MacOS HOT 6
- Q: how to encode input audio bytes to PCM properly HOT 1
- Having trouble specifying device dynamically. HOT 1
- Q: Is it possible to get individual audio streams from applications? HOT 1
- Extend WasapiDeviceConfig to support loopbackProcessID and loopbackProcessExclude
- SIGSEGV from `ma_device_uninit` HOT 4
- test segfault after upgrade to v0.11.10 HOT 1
- Dynamic Linking fails on OSX OSX 13.5.2 and 14.0 (Sonoma) HOT 2
- NULL Audio device on MacOS 14.1.1 (23B81) HOT 3
- Q: Support for High Level API HOT 1
- Release v0.11.11 HOT 2
- How is Capture device set supposed to work? HOT 2
- Runtime selection between PulseAudio and ALSA? HOT 1
- Capture on mac HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from malgo.