python 脚本从股票价格创建音符

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 脚本从股票价格创建音符相关的知识,希望对你有一定的参考价值。

#created by Austin Dixon
#script creates music from stock prices


class Music():

  def __init__(self, filename):
		self._filename = filename
		self._result = []
		self._value = []


	def getText(self):
		self._text = open(self._filename, 'r') # open file
		self._result.extend(line.split(' ') for line in self._text.readlines()) # get lines
		self._result = str(self._result).split(' ')  # split list
		#print("Result: ", self._result)
		return self._result

	def get_value(self):
		num = 0
		while (num < len(self._result)):
			txt = str(self._result[num]) # gets string version of one item in list
			txt = txt.replace(",","") # removes commas
			txt = txt.replace("!","") # removes exclaimations
			txt = txt.replace("[","") # removes left brackets
			txt = txt.replace("]","") # removes right brackets
			txt = txt.replace("\\n","") # removes new lines
			txt = txt.replace("\\r","") # removes new lines
			txt = txt.replace("'","") # removes whitespaces
			txt = txt.replace('"',"") # removes double quotes
			txt = txt.replace(";","") # removes semicolons
			self._value.append(txt)
			num = num + 1
		while '' in self._value:
			self._value.remove('') # get rid of empty tokens
		#print(self._value)
		return self._value

	def set_beat(self, beat):
		self._beat = beat
		return self._beat

	def header(self):
		print("RRAUDIO")
		#print("sampleRate:44100")
		print("%%")

	def play(self, note, octave, beat):
		self._note = note
		self._octave = octave
		self._beat = beat
		freq = []

		if (self._note == "A"):
			if (self._octave == 1):
				duration = (44100 / 110.00)
			elif (self._octave == 2):
				duration = (44100 / 220.00)
			elif (self._octave == 3):
				duration = (44100 / 440.00)

			num = 0
			while (num < duration):
				sample = self._value[num]
				freq.append(sample)
				num = num + 1
			num = 0
			beat = (self._beat * duration)
			beat = beat * self._beat
			while(num < beat):
				num2 = 0
				while (num2 < len(freq)) and (num < beat):
					print(freq[num2])
					num = num + 1
					num2 = num2 + 1
				num = num + 1

		elif (self._note == "B"):
			if (self._octave == 1):
				duration = (44100 / 123.47)
			elif (self._octave == 2):
				duration = (44100 / 246.94)
			elif (self._octave == 3):
				duration = (44100 / 493.88)
			
			num = 0
			while (num < duration):
				sample = self._value[num]
				freq.append(sample)
				num = num + 1
			num = 0
			beat = (self._beat * duration)
			beat = beat * self._beat
			while(num < beat):
				num2 = 0
				while (num2 < len(freq)) and (num < beat):
					print(freq[num2])
					num = num + 1
					num2 = num2 + 1
				num = num + 1

		elif (self._note == "C"):
			if (self._octave == 1):
				duration = (44100 / 130.81)
			elif (self._octave == 2):
				duration = (44100 / 261.63)
			elif (self._octave == 3):
				duration = (44100 / 523.25)
			
			num = 0
			while (num < duration):
				sample = self._value[num]
				freq.append(sample)
				num = num + 1
			num = 0
			beat = (self._beat * duration)
			beat = beat * self._beat
			while(num < beat):
				num2 = 0
				while (num2 < len(freq)) and (num < beat):
					print(freq[num2])
					num = num + 1
					num2 = num2 + 1
				num = num + 1

		elif (self._note == "D"):
			if (self._octave == 1):
				duration = (44100 / 146.83)
			elif (self._octave == 2):
				duration = (44100 / 293.66)
			elif (self._octave == 3):
				duration = (44100 / 587.33)
			
			num = 0
			while (num < duration):
				sample = self._value[num]
				freq.append(sample)
				num = num + 1
			num = 0
			beat = (self._beat * duration)
			beat = beat * self._beat
			while(num < beat):
				num2 = 0
				while (num2 < len(freq)) and (num < beat):
					print(freq[num2])
					num = num + 1
					num2 = num2 + 1
				num = num + 1

		elif (self._note == "E"):
			if (self._octave == 1):
				duration = (44100 / 164.81)
			elif (self._octave == 2):
				duration = (44100 / 329.63)
			elif (self._octave == 3):
				duration = (44100 / 659.26)
			
			num = 0
			while (num < duration):
				sample = self._value[num]
				freq.append(sample)
				num = num + 1
			num = 0
			beat = (self._beat * duration)
			beat = beat * self._beat
			while(num < beat):
				num2 = 0
				while (num2 < len(freq)) and (num < beat):
					print(freq[num2])
					num = num + 1
					num2 = num2 + 1
				num = num + 1

		elif (self._note == "F"):
			if (self._octave == 1):
				duration = (44100 / 146.83)
			elif (self._octave == 2):
				duration = (44100 / 293.66)
			elif (self._octave == 3):
				duration = (44100 / 698.46)
			
			num = 0
			while (num < duration):
				sample = self._value[num]
				freq.append(sample)
				num = num + 1
			num = 0
			beat = (self._beat * duration)
			beat = beat * self._beat
			while(num < beat):
				num2 = 0
				while (num2 < len(freq)) and (num < beat):
					print(freq[num2])
					num = num + 1
					num2 = num2 + 1
				num = num + 1

		elif (self._note == "G"):
			if (self._octave == 1):
				duration = (44100 / 174.61)
			elif (self._octave == 2):
				duration = (44100 / 392.00)
			elif (self._octave == 3):
				duration = (44100 / 783.99)
			
			num = 0
			while (num < duration):
				sample = self._value[num]
				freq.append(sample)
				num = num + 1
			num = 0
			beat = (self._beat * duration)
			beat = beat * self._beat
			while(num < beat):
				num2 = 0
				while (num2 < len(freq)) and (num < beat):
					print(freq[num2])
					num = num + 1
					num2 = num2 + 1
				num = num + 1



#sharps

		if (self._note == "A#"):
			if (self._octave == 1):
				duration = (44100 / 116.54)
			elif (self._octave == 2):
				duration = (44100 / 233.08)
			elif (self._octave == 3):
				duration = (44100 / 466.16)

			num = 0
			while (num < duration):
				sample = self._value[num]
				freq.append(sample)
				num = num + 1
			num = 0
			beat = (self._beat * duration)
			beat = beat * self._beat
			while(num < beat):
				num2 = 0
				while (num2 < len(freq)) and (num < beat):
					print(freq[num2])
					num = num + 1
					num2 = num2 + 1
				num = num + 1

		elif (self._note == "C#"):
			if (self._octave == 1):
				duration = (44100 / 138.59)
			elif (self._octave == 2):
				duration = (44100 / 277.18)
			elif (self._octave == 3):
				duration = (44100 / 554.37)
			
			num = 0
			while (num < duration):
				sample = self._value[num]
				freq.append(sample)
				num = num + 1
			num = 0
			beat = (self._beat * duration)
			beat = beat * self._beat
			while(num < beat):
				num2 = 0
				while (num2 < len(freq)) and (num < beat):
					print(freq[num2])
					num = num + 1
					num2 = num2 + 1
				num = num + 1

		elif (self._note == "D#"):
			if (self._octave == 1):
				duration = (44100 / 155.56)
			elif (self._octave == 2):
				duration = (44100 / 311.13)
			elif (self._octave == 3):
				duration = (44100 / 622.25)
			
			num = 0
			while (num < duration):
				sample = self._value[num]
				freq.append(sample)
				num = num + 1
			num = 0
			beat = (self._beat * duration)
			beat = beat * self._beat
			while(num < beat):
				num2 = 0
				while (num2 < len(freq)) and (num < beat):
					print(freq[num2])
					num = num + 1
					num2 = num2 + 1
				num = num + 1

		elif (self._note == "F#"):
			if (self._octave == 1):
				duration = (44100 / 185.00)
			elif (self._octave == 2):
				duration = (44100 / 369.99)
			elif (self._octave == 3):
				duration = (44100 / 739.99)
			
			num = 0
			while (num < duration):
				sample = self._value[num]
				freq.append(sample)
				num = num + 1
			num = 0
			beat = (self._beat * duration)
			beat = beat * self._beat
			while(num < beat):
				num2 = 0
				while (num2 < len(freq)) and (num < beat):
					print(freq[num2])
					num = num + 1
					num2 = num2 + 1
				num = num + 1

		elif (self._note == "G#"):
			if (self._octave == 1):
				duration = (44100 / 207.65)
			elif (self._octave == 2):
				duration = (44100 / 415.30)
			elif (self._octave == 3):
				duration = (44100 / 830.61)
			
			num = 0
			while (num < duration):
				sample = self._value[num]
				freq.append(sample)
				num = num + 1
			num = 0
			beat = (self._beat * duration)
			beat = beat * self._beat
			while(num < beat):
				num2 = 0
				while (num2 < len(freq)) and (num < beat):
					print(freq[num2])
					num = num + 1
					num2 = num2 + 1
				num = num + 1



以上是关于python 脚本从股票价格创建音符的主要内容,如果未能解决你的问题,请参考以下文章

抓取当前股票价格并创建数据框

如何使用 Python 下载股票价格数据?

python 取台湾场外交易股票价格。

python 股票盯盘v2.0

python 设计一个名为Stock的类来表示一个公司的股票?

从 yfinance 操纵日期