Beispiel #1
0
func main() {
	flag.Parse()

	if *file != "" {
		in, err := os.Open(*file)
		if err != nil {
			log.Fatal(err)
		}
		h := hasher.New(in)
		sng := h.Hash()
		in.Close()

		wav := wave.New(wave.Stereo, 22000)
		ks := karplus.Song{
			Song:         *sng,
			SamplingRate: 22000,
		}
		ks.Sound(&wav)

		out, err := os.Create(strings.TrimSuffix(*file, filepath.Ext(*file)) + ".wav")
		if err != nil {
			log.Fatal(err)
		}
		io.Copy(out, wav.Reader())
		return
	}

	if *seed != 0 {
		in := NewRandReader(2*1024*1024, *seed)
		h := hasher.New(in)
		sng := h.Hash()

		wav := wave.New(wave.Stereo, 22000)
		ks := karplus.Song{
			Song:         *sng,
			SamplingRate: 22000,
		}
		ks.Sound(&wav)

		out, err := os.Create(fmt.Sprintf("%v.wav", *seed))
		if err != nil {
			log.Fatal(err)
		}
		io.Copy(out, wav.Reader())
		return
	}

	port := os.Getenv("PORT")
	if port == "" {
		port = "8000"
	}

	r := mux.NewRouter()
	r.StrictSlash(true)
	r.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/"))).Methods("GET")
	r.HandleFunc("/upload", api.FileUploadHandler).Methods("POST")
	http.ListenAndServe(":"+port, handlers.LoggingHandler(os.Stdout, r))
}
Beispiel #2
0
func FileUploadHandler(w http.ResponseWriter, r *http.Request) {
	if r.ContentLength > 2*1024*1024 {
		http.Error(w, "Request too large, I'm too tired to be smart right now", http.StatusExpectationFailed)
		return
	}

	file, header, err := r.FormFile("file")
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}

	reader := io.LimitReader(file, 2*1024*1024)
	wav := wave.New(wave.Stereo, 22000)
	h := hasher.New(reader)
	sng := h.Hash()
	file.Close()

	if len(sng.Notes) == 0 {
		http.Error(w, "The file is too small, at least 17 bytes are required, and yes I'm that lazy!", 400)
		return
	}

	ks := karplus.Song{
		Song:         *sng,
		SamplingRate: 22000,
	}
	ks.Sound(&wav)

	w.Header().Add("Content-Type", "audio/x-wav")

	filename := url.QueryEscape(
		strings.TrimSuffix(header.Filename, filepath.Ext(header.Filename)) + ".wav")

	w.Header().Add("Content-Disposition", "attachment; filename="+filename)
	io.Copy(w, wav.Reader())
}