Example #1
0
func (m Meter) OpenStream() *Stream {
	var p portaudio.StreamParameters
	p = portaudio.LowLatencyParameters(m.Fast["i"].Dev[0].DevInfo, m.Fast["o"].Dev[0].DevInfo)
	numSamples := 256
	p.FramesPerBuffer = numSamples // It *might* be better to leave this unspecified.
	p.Input.Channels = 1
	p.Output.Channels = 1
	osxWarningText := `OS X 10.11 portaudio known issue (you can safely ignore the following WARNING):
        https://www.assembla.com/spaces/portaudio/tickets/243-portaudio-support-for-os-x-10-11-el-capitan
        https://lists.columbia.edu/pipermail/portaudio/2015-October/000092.html
        `
	if runtime.GOOS == "darwin" {
		out, err := exec.Command("sw_vers", "-productVersion").Output()
		if err != nil {
			fmt.Printf("sw_vers -productVersion failed to get OS X version: %v\n", err)
		} else {
			ver := strings.Split(string(out), ".")
			if ver[0] == "10" && ver[1] == "11" {
				fmt.Printf("%s", osxWarningText)
			}
		}
	}

	s := &Stream{}
	var err error
	if s.Stream, err = portaudio.OpenStream(p, s.process); err != nil {
		fmt.Printf("portaudio.OpenStream failed: ", err)
		panic(err)
	}
	if err = s.Start(); err != nil {
		fmt.Printf("portaudio.Stream.Start failed: ", err)
		panic(err)
	}
	return s
}
Example #2
0
func (self *Server) openAudioStream(in []int32) (*portaudio.Stream, error) {
	var device *portaudio.DeviceInfo
	var stream *portaudio.Stream
	var err error

	if self.Config.Input != nil {
		devices, err := portaudio.Devices()
		for _, deviceInfo := range devices {
			if deviceInfo.Name == self.Config.Input.Name {
				device = deviceInfo
				break
			}
		}

		if device != nil {
			log.Printf("Use %v", device)
			stream, err = portaudio.OpenStream(portaudio.StreamParameters{
				Input: portaudio.StreamDeviceParameters{
					Device:   device,
					Channels: 2,
					Latency:  device.DefaultHighInputLatency,
				},
				Output: portaudio.StreamDeviceParameters{
					Device:   nil,
					Channels: 0,
					Latency:  0,
				},
				SampleRate:      self.Config.Input.SampleRate,
				FramesPerBuffer: len(in),
				Flags:           portaudio.NoFlag,
			}, in)
			if err != nil {
				return nil, err
			}
		} else {
			log.Printf("No matched devices: (required: '%s')", self.Config.Input.Name)
			for _, deviceInfo := range devices {
				log.Printf("Found Device... %s %.1f", deviceInfo.Name, deviceInfo.DefaultSampleRate)
			}
			log.Printf("Fallback to default input device")
		}
	}

	if device == nil {
		device, err = portaudio.DefaultInputDevice()
		log.Printf("Use %v", device)
		if err != nil {
			return nil, err
		}

		stream, err = portaudio.OpenDefaultStream(2, 0, device.DefaultSampleRate, len(in), in)
		if err != nil {
			return nil, err
		}
	}

	return stream, nil
}
Example #3
0
func newEcho(delay time.Duration) *echo {
	h, err := portaudio.DefaultHostApi()
	chk(err)
	p := portaudio.LowLatencyParameters(h.DefaultInputDevice, h.DefaultOutputDevice)
	p.Input.Channels = 1
	p.Output.Channels = 1
	e := &echo{buffer: make([]float32, int(p.SampleRate*delay.Seconds()))}
	e.Stream, err = portaudio.OpenStream(p, e.processAudio)
	chk(err)
	return e
}
Example #4
0
func (m Meter) OpenStream() *Stream {
	var p portaudio.StreamParameters
	p = portaudio.LowLatencyParameters(m.Fast["i"].Dev[0].DevInfo, m.Fast["o"].Dev[0].DevInfo)
	numSamples := SAMPLES
	p.FramesPerBuffer = numSamples // It *might* be better to leave this unspecified.
	p.Input.Channels = 1
	p.Output.Channels = 1
	osxWarningText := `OS X 10.11 portaudio known issue (you can safely ignore the following WARNING):
        https://www.assembla.com/spaces/portaudio/tickets/243-portaudio-support-for-os-x-10-11-el-capitan
        https://lists.columbia.edu/pipermail/portaudio/2015-October/000092.html
        `
	if runtime.GOOS == "darwin" {
		out, err := exec.Command("sw_vers", "-productVersion").Output()
		if err != nil {
			fmt.Printf("sw_vers -productVersion failed to get OS X version: %v\n", err)
		} else {
			ver := strings.Split(string(out), ".")
			if ver[0] == "10" && ver[1] == "11" {
				fmt.Printf("%s", osxWarningText)
			}
		}
	}

	s := &Stream{}
	var err error
	if s.Stream, err = portaudio.OpenStream(p, s.process); err != nil {
		fmt.Printf("portaudio.OpenStream failed: %v", err)
		panic(err)
	}
	fmt.Printf("sample rate: %g Hz\n", s.Stream.Info().SampleRate)
	var noiseprev int32
	for i := range s.noise {
		v := int32(rand.Uint32()) >> 1
		noiseprev = noiseprev - noiseprev>>FILTER_FREQ_BITS + v>>FILTER_FREQ_BITS
		s.noise[i] = v - noiseprev
	}
	for i := range s.fir {
		sinci := float64(i-len(s.fir)/2) * math.Pi / (FILTER_FREQ * 4)
		sinc := float64(1)
		if sinci != 0 {
			sinc = math.Sin(sinci) / sinci
		}
		wingain := 4.0 / FILTER_FREQ
		hammingwin := math.Sin(float64(i+len(s.fir)/2) * math.Pi / (FIR_NUM_TAPS * 2))
		s.fir[i] = int32(wingain * hammingwin * sinc * float64(1<<32))
	}
	if err = s.Start(); err != nil {
		fmt.Printf("portaudio.Stream.Start failed: %v", err)
		panic(err)
	}
	return s
}
Example #5
0
func openStream(cbk interface{}) (*portaudio.Stream, error) {
	outDev, err := portaudio.DefaultOutputDevice()
	if err != nil {
		return nil, err
	}

	p := portaudio.LowLatencyParameters(nil, outDev)

	p.Input.Channels = 0
	p.Output.Channels = 2
	p.SampleRate = sampleRate
	p.FramesPerBuffer = 0
	return portaudio.OpenStream(p, cbk)
}
Example #6
0
func (a *Audio) Start() error {
	host, err := portaudio.DefaultHostApi()
	if err != nil {
		return err
	}
	parameters := portaudio.HighLatencyParameters(nil, host.DefaultOutputDevice)
	stream, err := portaudio.OpenStream(parameters, a.Callback)
	if err != nil {
		return err
	}
	if err := stream.Start(); err != nil {
		return err
	}
	a.stream = stream
	return nil
}
Example #7
0
func main() {
	portaudio.Initialize()
	defer portaudio.Terminate()
	h, err := portaudio.DefaultHostApi()
	chk(err)
	stream, err := portaudio.OpenStream(portaudio.HighLatencyParameters(nil, h.DefaultOutputDevice), func(out []int32) {
		for i := range out {
			out[i] = int32(rand.Uint32())
		}
	})
	chk(err)
	defer stream.Close()
	chk(stream.Start())
	time.Sleep(time.Second)
	chk(stream.Stop())
}