Exemple #1
0
func appRun() {
	defer closer.Close()
	closer.Bind(func() {
		log.Println("Bye!")
	})

	if err := portaudio.Initialize(); paError(err) {
		log.Fatalln("PortAudio init error:", paErrorText(err))
	}
	closer.Bind(func() {
		if err := portaudio.Terminate(); paError(err) {
			log.Println("PortAudio term error:", paErrorText(err))
		}
	})

	var input io.Reader
	if strings.HasPrefix(*uri, "http://") || strings.HasPrefix(*uri, "https://") {
		resp, err := http.Get(*uri)
		if err != nil {
			log.Fatalln(err)
		}
		closer.Bind(func() {
			resp.Body.Close()
		})
		input = resp.Body
	} else {
		f, err := os.Open(*uri)
		if err != nil {
			log.Fatalln(err)
		}
		closer.Bind(func() {
			f.Close()
		})
		input = f
	}

	dec, err := decoder.New(input, samplesPerChannel)
	if err != nil {
		log.Fatalln(err)
	}
	closer.Bind(dec.Close)

	info := dec.Info()
	log.Println(fileInfoTable(info))

	dec.SetErrorHandler(func(err error) {
		log.Println("[WARN]", err)
	})
	go func() {
		dec.Decode()
		dec.Close()
	}()

	var wg sync.WaitGroup
	var stream *portaudio.Stream
	callback := paCallback(&wg, int(info.Channels), dec.SamplesOut())
	if err := portaudio.OpenDefaultStream(&stream, 0, int32(info.Channels), sampleFormat, info.SampleRate,
		samplesPerChannel, callback, nil); paError(err) {
		log.Fatalln("PortAudio error:", paErrorText(err))
	}
	closer.Bind(func() {
		if err := portaudio.CloseStream(stream); paError(err) {
			log.Println("[WARN] PortAudio error:", paErrorText(err))
		}
	})

	if err := portaudio.StartStream(stream); paError(err) {
		log.Fatalln("PortAudio error:", paErrorText(err))
	}
	closer.Bind(func() {
		if err := portaudio.StopStream(stream); paError(err) {
			log.Fatalln("[WARN] PortAudio error:", paErrorText(err))
		}
	})

	log.Println("Playing...")
	wg.Wait()
}
//Run starts the application run loop
func (a *App) Run() {
	closer.Bind(a.cleanup)
	closer.Checked(a.internalRun, true)
	defer closer.Close()
}