summaryrefslogtreecommitdiff
path: root/autoaudio.go
blob: 5bd6fd396de8c4c9bd045f98567d416a5f6df4a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Copyright (C) 2019 Leo Tenenbaum

This file is part of AutoArt.

AutoArt is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

AutoArt is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with AutoArt.  If not, see <https://www.gnu.org/licenses/>.
*/

package main

import (
	"bufio"
	"fmt"
	"github.com/pommicket/autoart/autoart"
	"github.com/pommicket/autoart/autoutils"
	"math/rand"
	"os"
	"time"
)

func generateAudio(seed int64, length int64, sampleRate int64, functionLength int64, number int64) error {
	rand.Seed(seed)
	dir := fmt.Sprintf("autoaudio%v", seed)
	err := os.MkdirAll(dir, 0700)
	if err != nil {
		return err
	}
	err = autoutils.RunInBatches(number, "Generating audio...", func(n int64, errs chan<- error) {
		filename := fmt.Sprintf("%v/%09d.wav", dir, n)
		file, err := os.Create(filename)
		if err != nil {
			errs <- err
			return
		}
		err = autoart.GenerateAudio(file, float64(length), int32(sampleRate), int(functionLength), autoart.MOD)
		if err != nil {
			errs <- err
			return
		}
		errs <- file.Close()
	})
	if err != nil {
		return err
	}
	fmt.Println("Done. Your audio is in this directory:", dir)
	return nil
}

func autoAudio(reader *bufio.Reader) error {
	prompt := `How many options do you want?
1. None - Just make some audio
2. Some - Basic options
3. All  - Advanced options
Please enter 1, 2, or 3 (default: 1): `
	option, err := readInt64(reader, prompt, func(i int64) bool {
		return i >= 1 && i <= 3
	}, 1)
	if err != nil {
		return err
	}
	t := time.Now().UTC().UnixNano()
	if option == 1 {
		filename := fmt.Sprintf("autoaudio%v.wav", t)
		rand.Seed(t)
		file, err := os.Create(filename)
		if err != nil {
			return err
		}
		err = autoart.GenerateAudio(file, 60, 44100, 80, autoart.MOD)
		if err != nil {
			return err
		}
		fmt.Println("Generated audio:", filename)
		return nil
	}
	positive := func(i int64) bool { return i > 0 }
	length, err := readInt64(reader, "Length in seconds (default: 60)? ", positive, 60)
	if err != nil {
		return err
	}
	number, err := readInt64(reader, "Number (default: 1)? ", positive, 1)
	if err != nil {
		return err
	}
	if option == 2 {
		return generateAudio(t, length, 44100, 80, number)
	}
	sampleRate, err := readInt64(reader, "Sample rate (default: 44100)? ", positive, 44100)
	if err != nil {
		return err
	}
	functionLength, err := readInt64(reader, "Function length (default: 80)? ", positive, 80)
	if err != nil {
		return err
	}
	seed, err := readInt64(reader, "Random seed (default: current time)? ", positive, t)
	if err != nil {
		return err
	}
	return generateAudio(seed, length, sampleRate, functionLength, number)

}