Пример #1
0
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...)
}
Пример #2
0
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)
}