Giter Site home page Giter Site logo

Comments (7)

danfletcher1 avatar danfletcher1 commented on August 16, 2024

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.

djadala avatar djadala commented on August 16, 2024

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.

djadala avatar djadala commented on August 16, 2024

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.

danfletcher1 avatar danfletcher1 commented on August 16, 2024

from malgo.

djadala avatar djadala commented on August 16, 2024

actually nil here is variable name:
https://play.golang.org/p/yGW0r-fc3_K

from malgo.

danfletcher1 avatar danfletcher1 commented on August 16, 2024

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.

egonelbre avatar egonelbre commented on August 16, 2024

This should be fixed currently.

from malgo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.