• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang sound.Context类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/kierdavis/gosound/sound.Context的典型用法代码示例。如果您正苦于以下问题:Golang Context类的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Context类的18个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: Main

func Main(ctx sound.Context, channels ...chan float64) {
	flag.Parse()
	runtime.GOMAXPROCS(NumThreads)

	// Make a copy of the argument array before we modify it.
	channels2 := make([]chan float64, len(channels))
	copy(channels2, channels)
	channels = channels2

	// Measure the duration of the first channel
	var durationStream chan float64
	channels[0], durationStream = ctx.Fork2(channels[0])
	durationChan := make(chan time.Duration, 1)
	go func() {
		durationChan <- ctx.Duration(durationStream)
	}()

	so := getOutput(ctx)

	// Write the output
	startTime := time.Now()
	err := so.Write(ctx.SampleRate, channels)
	endTime := time.Now()

	if err != nil {
		fmt.Printf("Error: %s\n", err.Error())
	}

	outSecs := float64(<-durationChan) / float64(time.Second)
	realSecs := float64(endTime.Sub(startTime)) / float64(time.Second)
	fmt.Printf("Generated %.3f seconds of audio in %.3f seconds (ratio %.3f).\n", outSecs, realSecs, outSecs/realSecs)
}
开发者ID:kierdavis,项目名称:gosound,代码行数:32,代码来源:frontend.go


示例2: genSlideEnvelope

func genSlideEnvelope(ctx sound.Context, from, to music.Note, duration time.Duration) (stream chan float64) {
	return ctx.LinearEnvelope(
		from.Frequency(),
		duration*1/10,
		from.Frequency(),
		duration*8/10,
		to.Frequency(),
		duration*1/10,
		to.Frequency(),
	)
}
开发者ID:kierdavis,项目名称:gosound-demos,代码行数:11,代码来源:arp1.go


示例3: Generate

func Generate(ctx sound.Context) (left, right chan float64) {
	stream := ctx.TakeDuration(
		ctx.Mul(
			ctx.Sine(
				FrequencyEnvelope(ctx),
			),
			ctx.Const(0.7),
		),
		NoteDuration*8,
		true, // Wait for a zero crossing
	)
	return ctx.Fork2(stream)
}
开发者ID:kierdavis,项目名称:gosound-demos,代码行数:13,代码来源:scaledemo.go


示例4: SequenceBass

func SequenceBass(ctx sound.Context) (seq *sound.Sequencer) {
	melody := GenerateBassMelody()
	seq = sound.NewSequencer(ctx)

	var pos time.Duration

	for i := 0; i < NumBars; i++ {
		if rand.Float64() < 0.05 {
			freqInput := ctx.Const((<-melody).Frequency())
			note := PlayBassNote(ctx, freqInput, NoteDuration*3)
			seq.Add(pos, note)

		} else if rand.Float64() < 0.05 {
			freqInput1, freqInput2 := ctx.Fork2(ctx.Const((<-melody).Frequency()))
			note1 := PlayBassNote(ctx, freqInput1, NoteDuration)
			note2 := PlayBassNote(ctx, freqInput2, NoteDuration)
			seq.Add(pos, note1)
			seq.Add(pos+NoteDuration*2, note2)

		} else {
			freqInput := ctx.Const((<-melody).Frequency())
			note := PlayBassNote(ctx, freqInput, NoteDuration)
			seq.Add(pos, note)
		}

		pos += NoteDuration * 3
	}

	return seq
}
开发者ID:kierdavis,项目名称:gosound-demos,代码行数:30,代码来源:arp2.go


示例5: PlayBassNote

func PlayBassNote(ctx sound.Context, freqInput chan float64, duration time.Duration) (stream chan float64) {
	return ctx.TakeDuration(
		ctx.Add(
			ctx.Mul(
				ctx.Square(
					freqInput,
					ctx.Const(0.8),
				),
			),
		),
		duration,
		true,
	)
}
开发者ID:kierdavis,项目名称:gosound-demos,代码行数:14,代码来源:arp2.go


示例6: playBassSynth

func playBassSynth(ctx sound.Context, freqInput chan float64) (stream chan float64) {
	return ctx.Add(
		ctx.Mul(
			ctx.Square(
				freqInput,
				ctx.Const(0.8),
			),
		),
	)
}
开发者ID:kierdavis,项目名称:gosound-demos,代码行数:10,代码来源:arp1.go


示例7: FilterFormant

func FilterFormant(ctx sound.Context, input chan float64, centre, q, gain float64) (output chan float64) {
	lowerCutoff := (centre - (centre / q)) / 0.772
	upperCutoff := (centre + (centre / q)) / 1.29

	input, formant := ctx.Fork2(input)
	formant = filter.Chebyshev(ctx, formant, filter.HighPass, lowerCutoff, 0.5, 2)
	formant = filter.Chebyshev(ctx, formant, filter.LowPass, upperCutoff, 0.5, 2)
	formant = ctx.Mul(formant, ctx.Const(gain-1.0))
	return ctx.Add(input, formant)
}
开发者ID:kierdavis,项目名称:gosound-demos,代码行数:10,代码来源:karplusstrongdemo.go


示例8: genMelodyArpeggio

func genMelodyArpeggio(ctx sound.Context, scale music.Scale, n int) (stream chan float64) {
	var parts []chan float64

	root := scale.Root

	for i := 0; i < n; i++ {
		if i+1 < n && rand.Float64() < 0.01 {
			from := scale.Root
			nextNote(&scale, root)
			to := scale.Root
			nextNote(&scale, root)

			part := genSlideEnvelope(ctx, from, to, time.Second/4)
			parts = append(parts, part)

		} else {
			note := scale.Root
			nextNote(&scale, root)

			part := ctx.TakeDuration(ctx.Const(note.Frequency()), time.Second/8, false)
			parts = append(parts, part)
		}
	}

	return ctx.Append(parts...)
}
开发者ID:kierdavis,项目名称:gosound-demos,代码行数:26,代码来源:arp1.go


示例9: FrequencyEnvelope

func FrequencyEnvelope(ctx sound.Context) (stream chan float64) {
	var parts []chan float64
	for _, note := range Notes {
		part := ctx.TakeDuration(ctx.Const(note.Frequency()), NoteDuration, false)
		parts = append(parts, part)
	}
	return ctx.Append(parts...)
}
开发者ID:kierdavis,项目名称:gosound-demos,代码行数:8,代码来源:scaledemo.go


示例10: SequenceTreble

func SequenceTreble(ctx sound.Context) (seq *sound.Sequencer) {
	melody := GenerateTrebleMelody()
	seq = sound.NewSequencer(ctx)

	var pos time.Duration

	for i := 0; i < NumBars*3; i++ {
		var freqInput chan float64
		var duration time.Duration

		if rand.Float64() < 0.01 {
			from := (<-melody).Frequency()
			to := (<-melody).Frequency()

			freqInput = ctx.LinearEnvelope(
				from,
				NoteDuration*1/5,
				from,
				NoteDuration*8/5,
				to,
				NoteDuration*1/5+time.Millisecond,
				to,
			)
			duration = NoteDuration * 2

		} else {
			freqInput = ctx.Const((<-melody).Frequency())
			duration = NoteDuration
		}

		note := PlayTrebleNote(ctx, freqInput, duration)
		seq.Add(pos, note)
		pos += duration
	}

	return seq
}
开发者ID:kierdavis,项目名称:gosound-demos,代码行数:37,代码来源:arp2.go


示例11: KarplusStrong

// input should be finite and preferably short (e.g. one cycle of a triangle wave)
func KarplusStrong(ctx sound.Context, input chan float64, delaySamples uint, cutoff float64, decay float64) (output chan float64) {
	feedback := make(chan float64, ctx.StreamBufferSize)

	// Mix the input with the feedback.
	output = ctx.Add(input, feedback)

	// Fork off a copy of the output.
	output, outputCopy := ctx.Fork2(output)

	// The copy is first passed through a delay line...
	outputCopy = delay(ctx, outputCopy, delaySamples)

	// ...then filtered...
	//outputCopy = filter.Chebyshev(ctx, outputCopy, filter.LowPass, cutoff, 0.5, 2)
	outputCopy = filter.RC(ctx, outputCopy, filter.LowPass, ctx.Const(cutoff))

	// ...and finally attenuated slightly.
	outputCopy = ctx.Mul(outputCopy, ctx.Const(decay))

	// The filtered output copy is fed back into the system.
	go pipe(outputCopy, feedback)

	return output
}
开发者ID:kierdavis,项目名称:gosound,代码行数:25,代码来源:karplusstrong.go


示例12: genBassArpeggio

func genBassArpeggio(ctx sound.Context, scale music.Scale, n int) (stream chan float64) {
	var parts []chan float64

	root := scale.Root

	for i := 0; i < n; i++ {
		note := scale.Root
		nextNote(&scale, root)

		if rand.Float64() < 0.05 {
			part := ctx.TakeDuration(
				ctx.Mul(
					ctx.Const(note.Frequency()),
					ctx.Add( // Add a slight vibrato effect
						ctx.Mul(
							ctx.Sine(ctx.Const(24)),
							ctx.Const(0.01),
						),
						ctx.Const(1.0),
					),
				),
				time.Second*3/8,
				false,
			)

			parts = append(parts, part)

		} else if rand.Float64() < 0.05 {
			part1 := ctx.TakeDuration(
				ctx.Const(note.Frequency()),
				time.Second/8,
				false,
			)

			part2 := ctx.TakeDuration(
				ctx.Const(note.Frequency()),
				time.Second/8,
				false,
			)

			parts = append(parts, part1)
			parts = append(parts, ctx.TakeDuration(ctx.Silence(), time.Second/8, false))
			parts = append(parts, part2)

		} else {
			part := ctx.TakeDuration(
				ctx.Const(note.Frequency()),
				time.Second/8,
				false,
			)

			parts = append(parts, part)
			parts = append(parts, ctx.TakeDuration(ctx.Silence(), time.Second/4, false))
		}
	}

	return ctx.Append(parts...)
}
开发者ID:kierdavis,项目名称:gosound-demos,代码行数:58,代码来源:arp1.go


示例13: Generate

func Generate(ctx sound.Context) (left, right chan float64) {
	rand.Seed(time.Now().UnixNano())

	melodyParts := make(chan chan float64)
	bassParts := make(chan chan float64)

	go func() {
		for {
			var octave int
			x := rand.Float64()
			if x < 0.3 {
				octave = 4
			} else {
				octave = 5
			}

			root := music.MakeNote(music.D, octave)
			scale := music.Scale{Root: root, Intervals: music.HarmonicMinor}

			var n int
			x = rand.Float64()
			if x < 0.2 {
				n = 3
			} else if x < 0.4 {
				n = 6
			} else if x < 0.6 {
				n = 12
			} else if x < 0.8 {
				n = 18
			} else {
				n = 24
			}

			melodyParts <- genMelodyArpeggio(ctx, scale, n)
		}
	}()

	go func() {
		for {
			var octave int
			x := rand.Float64()
			if x < 0.3 {
				octave = 3
			} else {
				octave = 2
			}

			root := music.MakeNote(music.D, octave)
			scale := music.Scale{Root: root, Intervals: music.HarmonicMinor}

			var n int
			x = rand.Float64()
			if x < 0.2 {
				n = 3
			} else if x < 0.4 {
				n = 6
			} else if x < 0.6 {
				n = 9
			} else if x < 0.8 {
				n = 12
			} else {
				n = 18
			}

			bassParts <- genBassArpeggio(ctx, scale, n)
		}
	}()

	melody := playMelodySynth(ctx, ctx.AppendStream(melodyParts))
	bass := playBassSynth(ctx, ctx.AppendStream(bassParts))

	melodyLeft, melodyRight := ctx.Fork2(melody)
	bassLeft, bassRight := ctx.Fork2(bass)

	left = ctx.TakeDuration(
		ctx.Add(
			ctx.Mul(melodyLeft, ctx.Const(0.4)),
			ctx.Mul(bassLeft, ctx.Const(0.6)),
		),
		time.Second*300,
		true,
	)

	right = ctx.TakeDuration(
		ctx.Add(
			ctx.Mul(melodyRight, ctx.Const(0.6)),
			ctx.Mul(bassRight, ctx.Const(0.4)),
		),
		time.Second*300,
		true,
	)

	return left, right
}
开发者ID:kierdavis,项目名称:gosound-demos,代码行数:94,代码来源:arp1.go


示例14: Generate

func Generate(ctx sound.Context) (left, right chan float64) {
	treble := SequenceTreble(ctx).Play()
	bass := SequenceBass(ctx).Play()

	trebleLeft, trebleRight := ctx.Fork2(treble)
	bassLeft, bassRight := ctx.Fork2(bass)

	left = ctx.TakeDuration(
		ctx.Add(
			ctx.Mul(trebleLeft, ctx.Const(0.3)),
			ctx.Mul(bassLeft, ctx.Const(0.4)),
		),
		NoteDuration*NumBars*3+time.Second*2,
		true,
	)

	right = ctx.TakeDuration(
		ctx.Add(
			ctx.Mul(trebleRight, ctx.Const(0.4)),
			ctx.Mul(bassRight, ctx.Const(0.3)),
		),
		NoteDuration*NumBars*3+time.Second*2,
		true,
	)

	return left, right
}
开发者ID:kierdavis,项目名称:gosound-demos,代码行数:27,代码来源:arp2.go


示例15: PlayTrebleNote

func PlayTrebleNote(ctx sound.Context, freqInput chan float64, duration time.Duration) (stream chan float64) {
	freqInput1, freqInput2 := ctx.Fork2(freqInput)

	return ctx.TakeDuration(
		ctx.Add(
			ctx.Mul(
				ctx.Square(
					freqInput1,
					ctx.Const(0.5),
				),
				ctx.Const(0.75),
			),
			ctx.Mul(
				ctx.Saw(
					ctx.Mul(freqInput2, ctx.Const(2)),
				),
				ctx.Const(0.25),
			),
		),
		duration,
		true,
	)
}
开发者ID:kierdavis,项目名称:gosound-demos,代码行数:23,代码来源:arp2.go


示例16: playMelodySynth

func playMelodySynth(ctx sound.Context, freqInput chan float64) (stream chan float64) {
	freqInput1, freqInput2 := ctx.Fork2(freqInput)

	return ctx.Add(
		ctx.Mul(
			ctx.Square(
				freqInput1,
				ctx.Const(0.5),
			),
			ctx.Const(0.75),
		),
		ctx.Mul(
			ctx.Saw(
				ctx.Mul(freqInput2, ctx.Const(2)),
			),
			ctx.Const(0.25),
		),
	)
}
开发者ID:kierdavis,项目名称:gosound-demos,代码行数:19,代码来源:arp1.go


示例17: KarplusStrongNoise

func KarplusStrongNoise(ctx sound.Context, seed int64, length uint, cutoff, decay float64) (output chan float64) {
	input := ctx.Take(ctx.RandomNoise(seed), length, true)
	return KarplusStrong(ctx, input, length, cutoff, decay)
}
开发者ID:kierdavis,项目名称:gosound-demos,代码行数:4,代码来源:karplusstrongdemo.go


示例18: KarplusStrongSaw

func KarplusStrongSaw(ctx sound.Context, frequency, cutoff, decay float64) (output chan float64) {
	delaySamples := uint((1.0 / frequency) * ctx.SampleRate)
	wave := ctx.Saw(ctx.Const(frequency))
	input := ctx.Take(wave, delaySamples, true)
	return KarplusStrong(ctx, input, delaySamples, cutoff, decay)
}
开发者ID:kierdavis,项目名称:gosound-demos,代码行数:6,代码来源:karplusstrongdemo.go



注:本文中的github.com/kierdavis/gosound/sound.Context类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang shadow.Application类代码示例发布时间:2022-05-23
下一篇:
Golang authmodel.Manager类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap